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")) address column बदलता है, पर सिर्फ उन row(s) में जहाँ पुराना address अभी "Valley 345" से मेल खाता है -- बाकी हर row अछूती रहती है।
उदाहरण: 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")
बिना WHERE Update करने का खतरा
बिना किसी WHERE clause वाला UPDATE customers SET address = "Unknown" syntactically valid SQL है जो table की हर एक row में address column को उसी value से overwrite कर देता है -- एक unqualified DELETE के साथ-साथ, database काम में यह सबसे costly संभावित गलतियों में से एक है।
उदाहरण: 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)
एक साथ कई Columns Update करना
SET clause में हर column = %s assignment को comma से अलग करके एक ही UPDATE statement में कई columns set किए जा सकते हैं -- सूचीबद्ध सभी columns एक ही statement में एक साथ, atomically बदल दिए जाते हैं।
उदाहरण: 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")
एक साथ कई Rows Update करना
UPDATE का WHERE clause सिर्फ एक row से मेल खाने तक सीमित नहीं है -- एक broader condition (जैसे comparison operator या LIKE pattern) एक ही statement में कई rows से मेल खाकर उन्हें साथ update कर सकता है, ठीक वैसे ही जैसे एक broader WHERE किसी SELECT में कई rows return करता।
उदाहरण: 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")
Update Errors को Safely Handle करना
किसी UPDATE को try-except block में wrap करके mysql.connector.Error catch करना -- और except branch में conn.rollback() call करना -- आपको constraint violation जैसी failures को बिना crash किए साफ तरीके से handle करने देता है, database को एक consistent state में रखते हुए।
उदाहरण: 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)
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