Software Engineering

The best way to Replace a MySQL Desk in Python

Written by admin


First, you’ll need the mysql.connector. If you’re not sure of tips on how to get this setup, check with The best way to Set up MySQL Driver in Python.

The best way to Replace a MySQL Desk in Python

import mysql.connector

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

mycursor = mydb.cursor()
sql = "UPDATE clients SET handle = 'Stoneleigh Place' WHERE handle = 'Abbey Highway'"
mycursor.execute(sql)
mydb.commit()

print(mycursor.rowcount, "document(s) affected")

Forestall SQL Injection in your Replace Clause in Python

import mysql.connector

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

mycursor = mydb.cursor()

sql = "UPDATE clients SET handle = %s WHERE handle = %s"
val = ("Stoneleigh Place", "Abbey Highway")

mycursor.execute(sql, val)
mydb.commit()

print(mycursor.rowcount, "document(s) affected")

About the author

admin

Leave a Comment