Python MySQL – Deleting data

MySQL is an open-source relational database management system. In this post we will be deleting MySQL data using a Python script. You may also be interested in inserting data, updating data and retrieving data.

The script expects two columns which are ‘Name’ and ‘Age’ to already exist as part of our database table. In this example we are assuming the users name and age already exist within the database, using their name as a reference point we enable them with the ability to delete their entry entirely.

Full source code for this project can be found below:

import mysql.connector
import time

def InsertToDB(Name):
    mydb = mysql.connector.connect(
    host="",
    user="",
    password="",
    database="")
    mycursor = mydb.cursor()
    sql = "Delete FROM Table_1 WHERE Name = '" +Name + "'"
    mycursor.execute(sql)
    mydb.commit()
    mycursor.close()
    mydb.close()
   
while 1 == 1:
    Name = input("Please Enter your Name: ")
    InsertToDB(Name)
    print("Thank you! Next person please\n\n")
    time.sleep(2)

Leave a Reply