Wednesday, May 17, 2023
HomeSoftware EngineeringMethods to Delete MySQL Data in Python

Methods to Delete MySQL Data in Python


First, you have to the mysql.connector. In case you are uncertain of methods to get this setup, discuss with Methods to Set up MySQL Driver in Python.

Methods to Delete MySQL Data in Python

import mysql.connector

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

mycursor = mydb.cursor()
sql = "DELETE FROM prospects WHERE tackle = 'The Rockies'"
mycursor.execute(sql)

mydb.commit()

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

Stop SQL Injection in MySQL queries by way 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 prospects WHERE tackle = %s"
adr = ("The Rockies", )

mycursor.execute(sql, adr)

mydb.commit()

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

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments