In this example we will show how to update existing records in a table by using the “UPDATE” statement in Python MySQL.
Source Code
import mysql.connector
mydb = mysql.connector.connect(
host="localhost",
port=3306,
user="yourusername",
password="yourpassword",
db="mydatabase"
)
mycursor = mydb.cursor()
sql = "UPDATE us_state SET State_name = 'Ohio' WHERE State_name = 'California'"
mycursor.execute(sql)
mydb.commit()
print(mycursor.rowcount, "row affected")
mydb.close()
Output:
1 row affected