Skip to content

Commit 3b632f0

Browse files
committed
sqlite-utils rows --where and -p options, closes #382
1 parent 324ebc3 commit 3b632f0

File tree

4 files changed

+57
-18
lines changed

4 files changed

+57
-18
lines changed

docs/cli-reference.rst

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -584,24 +584,26 @@ See :ref:`cli_rows`.
584584
Output all rows in the specified table
585585

586586
Options:
587-
-c, --column TEXT Columns to return
588-
--limit INTEGER Number of rows to return - defaults to everything
589-
--offset INTEGER SQL offset to use
590-
--nl Output newline-delimited JSON
591-
--arrays Output rows as arrays instead of objects
592-
--csv Output CSV
593-
--tsv Output TSV
594-
--no-headers Omit CSV headers
595-
-t, --table Output as a table
596-
--fmt TEXT Table format - one of fancy_grid, fancy_outline,
597-
github, grid, html, jira, latex, latex_booktabs,
598-
latex_longtable, latex_raw, mediawiki, moinmoin,
599-
orgtbl, pipe, plain, presto, pretty, psql, rst, simple,
600-
textile, tsv, unsafehtml, youtrack
601-
--json-cols Detect JSON cols and output them as JSON, not escaped
602-
strings
603-
--load-extension TEXT SQLite extensions to load
604-
-h, --help Show this message and exit.
587+
-c, --column TEXT Columns to return
588+
--where TEXT Optional where clause
589+
-p, --param <TEXT TEXT>... Named :parameters for where clause
590+
--limit INTEGER Number of rows to return - defaults to everything
591+
--offset INTEGER SQL offset to use
592+
--nl Output newline-delimited JSON
593+
--arrays Output rows as arrays instead of objects
594+
--csv Output CSV
595+
--tsv Output TSV
596+
--no-headers Omit CSV headers
597+
-t, --table Output as a table
598+
--fmt TEXT Table format - one of fancy_grid, fancy_outline,
599+
github, grid, html, jira, latex, latex_booktabs,
600+
latex_longtable, latex_raw, mediawiki, moinmoin,
601+
orgtbl, pipe, plain, presto, pretty, psql, rst,
602+
simple, textile, tsv, unsafehtml, youtrack
603+
--json-cols Detect JSON cols and output them as JSON, not
604+
escaped strings
605+
--load-extension TEXT SQLite extensions to load
606+
-h, --help Show this message and exit.
605607

606608

607609
triggers

docs/cli.rst

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -449,6 +449,16 @@ You can use the ``-c`` option to specify a subset of columns to return::
449449
[{"age": 4, "name": "Cleo"},
450450
{"age": 2, "name": "Pancakes"}]
451451

452+
You can filter rows using a where clause with the ``--where`` option::
453+
454+
$ sqlite-utils rows dogs.db dogs -c name --where 'name = "Cleo"'
455+
[{"name": "Cleo"}]
456+
457+
Or pass named parameters using ``--where`` in combination with ``-p``::
458+
459+
$ sqlite-utils rows dogs.db dogs -c name --where 'name = :name' -p name Cleo
460+
[{"name": "Cleo"}]
461+
452462
Use ``--limit N`` to only return the first ``N`` rows. Use ``--offset N`` to return rows starting from the specified offset.
453463

454464
.. _cli_tables:

sqlite_utils/cli.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1723,6 +1723,14 @@ def search(
17231723
)
17241724
@click.argument("dbtable")
17251725
@click.option("-c", "--column", type=str, multiple=True, help="Columns to return")
1726+
@click.option("--where", help="Optional where clause")
1727+
@click.option(
1728+
"-p",
1729+
"--param",
1730+
multiple=True,
1731+
type=(str, str),
1732+
help="Named :parameters for where clause",
1733+
)
17261734
@click.option(
17271735
"--limit",
17281736
type=int,
@@ -1741,6 +1749,8 @@ def rows(
17411749
path,
17421750
dbtable,
17431751
column,
1752+
where,
1753+
param,
17441754
limit,
17451755
offset,
17461756
nl,
@@ -1758,6 +1768,8 @@ def rows(
17581768
if column:
17591769
columns = ", ".join("[{}]".format(c) for c in column)
17601770
sql = "select {} from [{}]".format(columns, dbtable)
1771+
if where:
1772+
sql += " where " + where
17611773
if limit:
17621774
sql += " limit {}".format(limit)
17631775
if offset:
@@ -1773,6 +1785,7 @@ def rows(
17731785
no_headers=no_headers,
17741786
table=table,
17751787
fmt=fmt,
1788+
param=param,
17761789
json_cols=json_cols,
17771790
load_extension=load_extension,
17781791
)

tests/test_cli.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -851,6 +851,7 @@ def test_query_memory_does_not_create_file(tmpdir):
851851
["--nl", "-c", "age", "-c", "name"],
852852
'{"age": 4, "name": "Cleo"}\n{"age": 2, "name": "Pancakes"}',
853853
),
854+
# --limit and --offset
854855
(
855856
["-c", "name", "--limit", "1"],
856857
'[{"name": "Cleo"}]',
@@ -859,6 +860,19 @@ def test_query_memory_does_not_create_file(tmpdir):
859860
["-c", "name", "--limit", "1", "--offset", "1"],
860861
'[{"name": "Pancakes"}]',
861862
),
863+
# --where
864+
(
865+
["-c", "name", "--where", "id = 1"],
866+
'[{"name": "Cleo"}]',
867+
),
868+
(
869+
["-c", "name", "--where", "id = :id", "-p", "id", "1"],
870+
'[{"name": "Cleo"}]',
871+
),
872+
(
873+
["-c", "name", "--where", "id = :id", "--param", "id", "1"],
874+
'[{"name": "Cleo"}]',
875+
),
862876
],
863877
)
864878
def test_rows(db_path, args, expected):

0 commit comments

Comments
 (0)