Python MySQL Update Table
In this page:
sql = "UPDATE table_name SET column = %s WHERE condition_column = %s"
cursor.execute(sql, (new_value, condition_value))
connection.commit()
Basic UPDATE Statement
cursor.execute("UPDATE customers SET address = %s WHERE address = %s", ("Canyon 123", "Valley 345")) changes the address column, but only in the row(s) where the old address currently matches "Valley 345" -- every other row remains untouched.
Example: Basic UPDATE Statement
from unittest.mock import MagicMock
conn = MagicMock()
cursor = conn.cursor()
cursor.execute(
"UPDATE customers SET address = %s WHERE address = %s", # only rows matching the WHERE are changed
("Canyon 123", "Valley 345"),
)
conn.commit()
print("Matching row(s) updated")
The Danger of Updating Without WHERE
UPDATE customers SET address = "Unknown" with no WHERE clause is syntactically valid SQL that overwrites the address column in every single row of the table with the same value -- one of the costliest possible mistakes in database work, alongside an unqualified DELETE.
Example: The Danger of Updating Without WHERE
from unittest.mock import MagicMock
conn = MagicMock()
cursor = conn.cursor()
# DANGER: no WHERE clause -- this overwrites every row's address
sql = "UPDATE customers SET address = %s"
print("Always double-check for a WHERE clause before running:", sql)
Updating Multiple Columns at Once
Several columns can be set in a single UPDATE statement by separating each column = %s assignment with a comma inside the SET clause -- all listed columns are changed together, atomically, in the same statement.
Example: Updating Multiple Columns at Once
from unittest.mock import MagicMock
conn = MagicMock()
cursor = conn.cursor()
cursor.execute(
"UPDATE customers SET address = %s, city = %s WHERE id = %s", # both columns changed together
("Canyon 123", "Denver", 1),
)
conn.commit()
print("Two columns updated in one statement")
Updating Multiple Rows at Once
An UPDATE's WHERE clause is not limited to matching a single row -- a broader condition (like a comparison operator or LIKE pattern) can match and update several rows together in one statement, exactly as a broader WHERE would return several rows in a SELECT.
Example: Updating Multiple Rows at Once
from unittest.mock import MagicMock
conn = MagicMock()
cursor = conn.cursor()
cursor.execute("UPDATE customers SET city = %s WHERE city = %s", ("Austin", "austin")) # broader WHERE can match several rows
conn.commit()
print("All rows matching the WHERE clause updated together")
Handling Update Errors Safely
Wrapping an UPDATE in a try-except block catching mysql.connector.Error -- and calling conn.rollback() in the except branch -- lets you handle failures like a constraint violation cleanly without crashing, keeping the database in a consistent state.
Example: Handling Update Errors Safely
from unittest.mock import MagicMock
class Error(Exception):
pass
conn = MagicMock()
cursor = conn.cursor()
cursor.execute.side_effect = Error("Constraint violation")
try:
cursor.execute("UPDATE customers SET id = %s WHERE id = %s", (2, 1))
conn.commit()
except Error as e:
conn.rollback() # keeps the database consistent after a failed update
print("Update failed, rolled back:", e)
- Running UPDATE tableName SET column = value with no WHERE clause, which changes that column in every single row of the table instead of just the intended one.
- Forgetting conn.commit() after an UPDATE, leaving the change unsaved once the connection closes.
- Swapping the SET and WHERE clause values by mistake, accidentally setting a column to the value meant for filtering, or vice versa.
- UPDATE table SET column = %s WHERE condition changes a column's value only in rows matching the condition.
- Omitting the WHERE clause updates every row in the table -- always double-check it is present.
- conn.commit() must be called after UPDATE to permanently save the change.
UPDATE syntax is standard SQL, executed identically across all mysql-connector-python versions.
Chapter Quiz — Complete all 12 topics to unlock
0/12 topics done
Complete these topics first:
- Python MySQL Get Started
- Python MySQL Create Database
- Python MySQL Create Table
- Python MySQL Insert Into Table
- Python MySQL Select From Table
- Python MySQL Select With a Filter
- Python MySQL Order By
- Python MySQL Delete Record
- Python MySQL Drop Table
- Python MySQL Update Table
- Python MySQL Limit
- Python MySQL Join