更新表中的已有记录

  1. import mysql.connector
  2.  
  3. mydb = mysql.connector.connect(
  4. host="localhost",
  5. user="myusername",
  6. passwd="mypassword",
  7. database="mydatabase"
  8. )
  9.  
  10. mycursor = mydb.cursor()
  11.  
  12. sql = "UPDATE customers SET address = 'Canyon 123' WHERE address = 'Valley 345'"
  13.  
  14. mycursor.execute(sql)
  15.  
  16. mydb.commit()
  17.  
  18. print(mycursor.rowcount, "record(s) affected")