Delete record from MySQL table using python

Delete record from MySQL table using python is the third article in this series of Python and  MySQL, In the previous articles, we learn how to install python package MySQL client to make a connection between Python and MySQLInsert record in MySQL table using python and now we are going to learn how to delete a record from MySQL table using python.

Mysql syntax to delete a record is as follow

Delete from TableName  [ where <condition> ]

If the condition is missing from the delete command- Delete command will remove all the rows from the table. Thus condition was introduced in delete command.

Delete from  STUDENT where admno = 10

The above MySQL command removes only those rows from the table that have 10 as its admission number.

The same we are going to implement in this python program to remove a record from the table. The table structure of the table student is as follows

For example suppose, we have a table of users that have the following structure

Student table contains the following records

So the command that we will use to delete a record will be

Delete from student where name like ‘%ramji%’;

The command will erase a single record from the table whose admission no is 111.

Python Program to delete a record from MySQL Table

import MySQLdb

db = MySQLdb.connect(“localhost”,”root”,””,”cable”)

cursor = db.cursor()

name = input(“Enter any name : “)

sql =”delete from customer where name like ‘%” + name + “‘;”




print(“Row deleted successfully”)

Remember, we are using the same command to execute all type of mysql command from python.

The most important command in the above code is  db.commit()  , if you forgot to fire this statement then the changes / deletion in your table will not be permanent.

Related Posts

If you like CBSEToaday and would like to contribute, you can also write an article using submit article or mail your article to See your article appearing on the main page and help other students/teachers.