HomeSoftware EngineeringEasy methods to Delete MySQL Information in Python

Easy methods to Delete MySQL Information in Python


First, you’ll need the mysql.connector. If you’re uncertain of how you can get this setup, check with Easy methods to Set up MySQL Driver in Python.

Easy methods to 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 prospects WHERE handle = 'The Rockies'"
mycursor.execute(sql)

mydb.commit()

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

Stop SQL Injection in MySQL queries via 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 handle = %s"
adr = ("The Rockies", )

mycursor.execute(sql, adr)

mydb.commit()

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

Most Popular

Recent Comments