mirror of
https://github.com/simonw/sqlite-utils.git
synced 2026-10-28 06:07:09 +01:00
Allow table.get() to look up rows by column values
table.get() now accepts column=value keyword arguments and returns the first matching row, so a row can be fetched by a unique column rather than only by its primary key. Passing both a primary key and keyword arguments raises ValueError, and calling get() with neither raises TypeError. Refs #588
This commit is contained in:
parent
6bc1d33d58
commit
65b3fc8789
3 changed files with 106 additions and 1 deletions
|
|
@ -652,6 +652,13 @@ If the table has a compound primary key you can pass in the primary key values a
|
|||
|
||||
>>> db.table("compound_dogs").get(("mixed", 3))
|
||||
|
||||
You can also look a record up by one or more column values, passed as keyword arguments. This returns the first matching row, which is useful for fetching a record by a column with a unique constraint rather than by its primary key::
|
||||
|
||||
>>> db.table("dogs").get(name="Cleo")
|
||||
{'id': 1, 'age': 4, 'name': 'Cleo'}
|
||||
|
||||
Passing more than one keyword argument matches rows against all of them. You cannot combine a primary key value with keyword arguments in the same call.
|
||||
|
||||
If the record does not exist a ``NotFoundError`` will be raised:
|
||||
|
||||
.. code-block:: python
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue