Delete record from the database with where condition using python

DELETE keyword is used to delete the records from the table.

DELETE FROM table_name WHERE condition statement is used to delete records. If you don’t specify the condition, then all of the records will be deleted.

Let’s delete a record from the user table with named Siddharth.

import mysql.connector as mysql

db = mysql.connect(
    host="localhost",
    user="root",
    passwd="[email protected]",
    database="realprogrammer"
)

cursor = db.cursor()

## defining the Query

query = "DELETE FROM users WHERE name = 'Siddharth'"

## executing the query
cursor.execute(query)

## final step to tell the database that we have changed the table data
db.commit()
Leave a Comment