Updating Data
Use update() to modify existing rows. You pass the table name, the new values as a
dictionary, and an optional WHERE condition.
db.update(table, data, where=None) Parameters
| Parameter | Type | Description |
|---|---|---|
table | str | Name of the table |
data | dict | Column-value pairs to set |
where | dict or str | Optional filter. Dict keys are ANDed; or pass a raw SQL string. |
Update with a dictionary WHERE clause
When where is a dict, all keys are joined with AND:
# UPDATE products SET price = 899.99, stock = 45 WHERE id = 1
db.update('products', {
'price': 899.99,
'stock': 45,
}, {
'id': 1,
})
Update with a SQL string WHERE clause
# Mark all pending orders older than 7 days as expired
db.update('orders',
{'status': 'expired'},
"status = 'pending' AND created_at < DATE_SUB(NOW(), INTERVAL 7 DAY)"
)
Update all rows (no WHERE)
Omitting where updates every row in the table — use with caution.
db.update('settings', {'maintenance_mode': 0})
Generated SQL
# dict where:
db.update('products', {'price': 50}, {'category': 'sale', 'active': 1})
# → UPDATE products SET price=50 WHERE category='sale' AND active=1
# string where:
db.update('products', {'price': 50}, "category='sale' OR category='clearance'")
# → UPDATE products SET price=50 WHERE category='sale' OR category='clearance'