Friday, March 17, 2023
HomeSoftware EngineeringDelete MySQL Information in Python

Delete MySQL Information in Python


First, you will have the mysql.connector. If you’re not sure of the best way to get this setup, confer with Set up MySQL Driver in Python.

Delete MySQL Information in Python

import mysql.connector

mydb = mysql.connector.join(
  host = "localhost",
  person = "username",
  password = "YoUrPaSsWoRd",
  database = "your_database"
)

mycursor = mydb.cursor()
sql = "DELETE FROM clients WHERE deal with = 'The Rockies'"
mycursor.execute(sql)

mydb.commit()

print(mycursor.rowcount, "file(s) deleted")

Forestall SQL Injection in MySQL queries by means of Python

Specify the injected variable because the second argument to the execute command as under.

import mysql.connector

mydb = mysql.connector.join(
  host = "localhost",
  person = "username",
  password = "YoUrPaSsWoRd",
  database = "your_database"
)

mycursor = mydb.cursor()

sql = "DELETE FROM clients WHERE deal with = %s"
adr = ("The Rockies", )

mycursor.execute(sql, adr)

mydb.commit()

print(mycursor.rowcount, "file(s) deleted")
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments