logo

Updating and Deleting Data

UPDATE modifies existing rows. Always include a WHERE clause to limit what gets changed.

cursor.execute(
    "UPDATE users SET age = ? WHERE name = ?",
    (31, "Alice")
)
conn.commit()

Without WHERE, every row gets updated - usually not what you want.

DELETE removes rows:

cursor.execute(
    "DELETE FROM users WHERE name = ?",
    ("Alice",)
)
conn.commit()

Again, without WHERE, all rows are deleted.

Check how many rows were affected:

print(cursor.rowcount)  # Number of rows updated/deleted

This helps verify your operation did what you expected.

I explain update and delete patterns in my SQL with Python course.