Improved .rows_where() documentation, added test for :named parameters

This commit is contained in:
Simon Willison 2021-06-02 11:57:05 -07:00
commit 2dad4f583c
2 changed files with 8 additions and 1 deletions

View file

@ -202,7 +202,13 @@ You can filter rows by a WHERE clause using ``.rows_where(where, where_args)``::
... print(row)
{'id': 1, 'age': 4, 'name': 'Cleo'}
To return custom columns (instead of using ``select *``) pass ``select=``::
The first argument is a fragment of SQL. The second, optional argument is values to be passed to that fragment - you can use ``?`` placeholders and pass an array, or you can use ``:named`` parameters and pass a dictionary, like this::
>>> for row in db["dogs"].rows_where("age > :age", {"age": 3}):
... print(row)
{'id': 1, 'age': 4, 'name': 'Cleo'}
To return custom columns (instead of the default that uses ``select *``) pass ``select="column1, column2"``::
>>> db = sqlite_utils.Database("dogs.db")
>>> for row in db["dogs"].rows_where(select='name, age'):

View file

@ -13,6 +13,7 @@ def test_rows(existing_db):
[
("name = ?", ["Pancakes"], {2}),
("age > ?", [3], {1}),
("age > :age", {"age": 3}, {1}),
("name is not null", [], {1, 2}),
("is_good = ?", [True], {1, 2}),
],