python-mysqlHow do I update a row in a MySQL database using Python?
Updating a row in a MySQL database using Python is a relatively straightforward process. The following example code block will update a row in the 'customers' table of a database named 'mydatabase' with the customer's name changed from 'John Doe' to 'John Smith':
import mysql.connector
mydb = mysql.connector.connect(
host="localhost",
user="root",
passwd="password",
database="mydatabase"
)
mycursor = mydb.cursor()
sql = "UPDATE customers SET name = 'John Smith' WHERE name = 'John Doe'"
mycursor.execute(sql)
mydb.commit()
print(mycursor.rowcount, "record(s) affected")
Output example
1 record(s) affected
Code explanation
import mysql.connector
: imports the necessary modules to connect to and query a MySQL database.mydb = mysql.connector.connect(...)
: establishes a connection to the MySQL database.mycursor = mydb.cursor()
: creates a cursor object used to execute SQL queries on the database.sql = "UPDATE customers SET name = 'John Smith' WHERE name = 'John Doe'"
: creates a SQL query string to update the 'name' field in the 'customers' table from 'John Doe' to 'John Smith'.mycursor.execute(sql)
: executes the SQL query on the database.mydb.commit()
: commits the changes to the database.print(mycursor.rowcount, "record(s) affected")
: prints the number of affected records in the database.
Helpful links
More of Python Mysql
- How can I connect Python to a MySQL database?
- How can I connect to MySQL using Python?
- How can I convert data from a MySQL database to XML using Python?
- How can I use Python and MySQL to generate a PDF?
- ¿Cómo conectar Python a MySQL usando ejemplos?
- How can I connect Python and MySQL?
- How do I connect Python with MySQL using XAMPP?
- How do I use a cursor to interact with a MySQL database in Python?
- How do I check the version of MySQL I am using with Python?
- How do I fix a bad MySQL handshake error in Python?
See more codes...