Skip to content

Commit 5986083

Browse files
committed
Documentation for table.update() method
1 parent bc9c4db commit 5986083

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

docs/python-api.rst

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -331,6 +331,30 @@ The function can accept an iterator or generator of rows and will commit them ac
331331
332332
You can skip inserting any records that have a primary key that already exists using ``ignore=True``. This works with both ``.insert({...}, ignore=True)`` and ``.insert_all([...], ignore=True)``.
333333

334+
.. _python_api_update:
335+
336+
Updating a specific record
337+
==========================
338+
339+
You can update a record by its primary key using ``table.update()``::
340+
341+
>>> db = sqlite_utils.Database("dogs.db")
342+
>>> print(db["dogs"].get(1))
343+
{'id': 1, 'age': 4, 'name': 'Cleo'}
344+
>>> db["dogs"].update(1, {"age": 5})
345+
>>> print(db["dogs"].get(1))
346+
{'id': 1, 'age': 5, 'name': 'Cleo'}
347+
348+
The first argument to ``update()`` is the primary key. This can be a single value, or a tuple if that table has a compound primary key::
349+
350+
>>> db["compound_dogs"].update((5, 3), {"name": "Updated"})
351+
352+
The second argument is a dictonary of columns that should be updated, along with their new values.
353+
354+
You can cause any missing columns to be added automatically using ``alter=True``::
355+
356+
>>> db["dogs"].update(1, {"breed": "Mutt"}, alter=True)
357+
334358
Upserting data
335359
==============
336360

0 commit comments

Comments
 (0)