This article demonstrates how to execute a MySQL UPDATE query from Python to modify the MySQL table’s data.
Goals of this lesson. You’ll learn the following MySQL UPDATE operations from Python using a ‘MySQL Connector’ module.
Before executing the following program, make sure you have the following in place −
For this article, I am using a Laptop table present in my MySQL server.
If a table is not present in your MySQL server, you can refer to our article to create a MySQL table from Python.
You can also download a SQL query file, which contains SQL queries for table creation and data so that you can use this table for your UPDATE operations.
MySQL Laptop table with data Example to Update a row of MySQL TableTo perform a SQL UPDATE query from Python, you need to follow the following steps: –
How to Update MySQL Table in Python
Refer to Python MySQL database connection to connect to MySQL database from Python using MySQL Connector module
Prepare an update statement query with data to update. FOr example, UPDATE table_name SET column1 = value1, column2 = value2 WHERE condition;
Execute the UPDATE query using cursor.execute()
method. This method execute the operation stored in the UPDATE query.
Make modification persistent into a database using the commit()
of a connection class.
After a successful update operation, use a cursor.rowcount
method to get the number of rows affected. The count depends on how many rows you are updating.
Execute a MySQL select query from Python to see the new changes
use cursor.clsoe()
and connection.clsoe()
method to close open connections after your work completes.
Let’s see the program now. In this program, we are updating a Laptop table by changing the price column of the first row.
Output:
Before updating a row (1, 'Lenovo ThinkPad P71', 6459.0, datetime.date(2019, 8, 14)) Record Updated successfully After updating row (1, 'Lenovo ThinkPad P71', 7000.0, datetime.date(2019, 8, 14)) MySQL connection is closedMySQL Laptop table after updating record
Note:
Sometimes we need input from the user, such as when users update their password or any other details through User Interface. Or when you want to update details dynamically by passing Python variables into a query. Such as setting column value using the variable.
It is always best practice to use parameterized query and prepared statement, i.e., placeholders ( %s
) inside any SQL statements that contain input from users. This helps us prevent SQL injection and other SQL issues.
Read more on What is a Parameterized Query and its performance benefits.
Let’ s see the example program now.
Output: –
Record Updated successfully MySQL connection is closed Record Updated successfully MySQL connection is closed
Let’s understand the above program: –
cursor.execute()
method. Remember tuple contains user data in the sequential order of placeholders.connection.commit()
.It is possible to update multiple rows in a single SQL Query. You can also call it a bulk update. Use the cursor.executemany()
method of cursor object to update multiple rows of a table.
The syntax of the executemany() method
cursor.executemany(operation, seq_of_params)
Code language: Python (python)
This method executes operation
against all parameter sequences in the sequence seq_of_params
argument.
Output:
2 Records of a laptop table updated successfully connection is closed
Let’s understand the above program
cursor.executemany()
method to update multiple rows of a database table.cursor.rowcount
we can find how many rows are updated successfully.We can also update multiple columns of the MySQL table in a single query. Use parameterized query using a placeholder to update multiple columns. Let see this with an example program.
Output:
Multiple column updated successfully MySQL connection is closedUpdate Datetime and timestamp column of a MySQL table from Python
Suppose you have a date column in a MySQL table and you want to update a datetime.datetime()
object into this column. Let’s see how to prepare an update query to update the datetime
column of a table
You should get the following output after the execution of the above code.
Purchased Date Updated successfully connection is closedNext Steps
To practice what you learned in this lesson, Please solve a Python Database Exercise project to practice and master the Python Database operations.
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4