mirror of
https://github.com/simonw/sqlite-utils.git
synced 2026-11-26 20:09:42 +01:00
sqlite-utils rows --where and -p options, closes #382
This commit is contained in:
parent
324ebc3130
commit
3b632f0a7e
4 changed files with 57 additions and 18 deletions
|
|
@ -585,6 +585,8 @@ See :ref:`cli_rows`.
|
||||||
|
|
||||||
Options:
|
Options:
|
||||||
-c, --column TEXT Columns to return
|
-c, --column TEXT Columns to return
|
||||||
|
--where TEXT Optional where clause
|
||||||
|
-p, --param <TEXT TEXT>... Named :parameters for where clause
|
||||||
--limit INTEGER Number of rows to return - defaults to everything
|
--limit INTEGER Number of rows to return - defaults to everything
|
||||||
--offset INTEGER SQL offset to use
|
--offset INTEGER SQL offset to use
|
||||||
--nl Output newline-delimited JSON
|
--nl Output newline-delimited JSON
|
||||||
|
|
@ -596,10 +598,10 @@ See :ref:`cli_rows`.
|
||||||
--fmt TEXT Table format - one of fancy_grid, fancy_outline,
|
--fmt TEXT Table format - one of fancy_grid, fancy_outline,
|
||||||
github, grid, html, jira, latex, latex_booktabs,
|
github, grid, html, jira, latex, latex_booktabs,
|
||||||
latex_longtable, latex_raw, mediawiki, moinmoin,
|
latex_longtable, latex_raw, mediawiki, moinmoin,
|
||||||
orgtbl, pipe, plain, presto, pretty, psql, rst, simple,
|
orgtbl, pipe, plain, presto, pretty, psql, rst,
|
||||||
textile, tsv, unsafehtml, youtrack
|
simple, textile, tsv, unsafehtml, youtrack
|
||||||
--json-cols Detect JSON cols and output them as JSON, not escaped
|
--json-cols Detect JSON cols and output them as JSON, not
|
||||||
strings
|
escaped strings
|
||||||
--load-extension TEXT SQLite extensions to load
|
--load-extension TEXT SQLite extensions to load
|
||||||
-h, --help Show this message and exit.
|
-h, --help Show this message and exit.
|
||||||
|
|
||||||
|
|
|
||||||
10
docs/cli.rst
10
docs/cli.rst
|
|
@ -449,6 +449,16 @@ You can use the ``-c`` option to specify a subset of columns to return::
|
||||||
[{"age": 4, "name": "Cleo"},
|
[{"age": 4, "name": "Cleo"},
|
||||||
{"age": 2, "name": "Pancakes"}]
|
{"age": 2, "name": "Pancakes"}]
|
||||||
|
|
||||||
|
You can filter rows using a where clause with the ``--where`` option::
|
||||||
|
|
||||||
|
$ sqlite-utils rows dogs.db dogs -c name --where 'name = "Cleo"'
|
||||||
|
[{"name": "Cleo"}]
|
||||||
|
|
||||||
|
Or pass named parameters using ``--where`` in combination with ``-p``::
|
||||||
|
|
||||||
|
$ sqlite-utils rows dogs.db dogs -c name --where 'name = :name' -p name Cleo
|
||||||
|
[{"name": "Cleo"}]
|
||||||
|
|
||||||
Use ``--limit N`` to only return the first ``N`` rows. Use ``--offset N`` to return rows starting from the specified offset.
|
Use ``--limit N`` to only return the first ``N`` rows. Use ``--offset N`` to return rows starting from the specified offset.
|
||||||
|
|
||||||
.. _cli_tables:
|
.. _cli_tables:
|
||||||
|
|
|
||||||
|
|
@ -1723,6 +1723,14 @@ def search(
|
||||||
)
|
)
|
||||||
@click.argument("dbtable")
|
@click.argument("dbtable")
|
||||||
@click.option("-c", "--column", type=str, multiple=True, help="Columns to return")
|
@click.option("-c", "--column", type=str, multiple=True, help="Columns to return")
|
||||||
|
@click.option("--where", help="Optional where clause")
|
||||||
|
@click.option(
|
||||||
|
"-p",
|
||||||
|
"--param",
|
||||||
|
multiple=True,
|
||||||
|
type=(str, str),
|
||||||
|
help="Named :parameters for where clause",
|
||||||
|
)
|
||||||
@click.option(
|
@click.option(
|
||||||
"--limit",
|
"--limit",
|
||||||
type=int,
|
type=int,
|
||||||
|
|
@ -1741,6 +1749,8 @@ def rows(
|
||||||
path,
|
path,
|
||||||
dbtable,
|
dbtable,
|
||||||
column,
|
column,
|
||||||
|
where,
|
||||||
|
param,
|
||||||
limit,
|
limit,
|
||||||
offset,
|
offset,
|
||||||
nl,
|
nl,
|
||||||
|
|
@ -1758,6 +1768,8 @@ def rows(
|
||||||
if column:
|
if column:
|
||||||
columns = ", ".join("[{}]".format(c) for c in column)
|
columns = ", ".join("[{}]".format(c) for c in column)
|
||||||
sql = "select {} from [{}]".format(columns, dbtable)
|
sql = "select {} from [{}]".format(columns, dbtable)
|
||||||
|
if where:
|
||||||
|
sql += " where " + where
|
||||||
if limit:
|
if limit:
|
||||||
sql += " limit {}".format(limit)
|
sql += " limit {}".format(limit)
|
||||||
if offset:
|
if offset:
|
||||||
|
|
@ -1773,6 +1785,7 @@ def rows(
|
||||||
no_headers=no_headers,
|
no_headers=no_headers,
|
||||||
table=table,
|
table=table,
|
||||||
fmt=fmt,
|
fmt=fmt,
|
||||||
|
param=param,
|
||||||
json_cols=json_cols,
|
json_cols=json_cols,
|
||||||
load_extension=load_extension,
|
load_extension=load_extension,
|
||||||
)
|
)
|
||||||
|
|
|
||||||
|
|
@ -851,6 +851,7 @@ def test_query_memory_does_not_create_file(tmpdir):
|
||||||
["--nl", "-c", "age", "-c", "name"],
|
["--nl", "-c", "age", "-c", "name"],
|
||||||
'{"age": 4, "name": "Cleo"}\n{"age": 2, "name": "Pancakes"}',
|
'{"age": 4, "name": "Cleo"}\n{"age": 2, "name": "Pancakes"}',
|
||||||
),
|
),
|
||||||
|
# --limit and --offset
|
||||||
(
|
(
|
||||||
["-c", "name", "--limit", "1"],
|
["-c", "name", "--limit", "1"],
|
||||||
'[{"name": "Cleo"}]',
|
'[{"name": "Cleo"}]',
|
||||||
|
|
@ -859,6 +860,19 @@ def test_query_memory_does_not_create_file(tmpdir):
|
||||||
["-c", "name", "--limit", "1", "--offset", "1"],
|
["-c", "name", "--limit", "1", "--offset", "1"],
|
||||||
'[{"name": "Pancakes"}]',
|
'[{"name": "Pancakes"}]',
|
||||||
),
|
),
|
||||||
|
# --where
|
||||||
|
(
|
||||||
|
["-c", "name", "--where", "id = 1"],
|
||||||
|
'[{"name": "Cleo"}]',
|
||||||
|
),
|
||||||
|
(
|
||||||
|
["-c", "name", "--where", "id = :id", "-p", "id", "1"],
|
||||||
|
'[{"name": "Cleo"}]',
|
||||||
|
),
|
||||||
|
(
|
||||||
|
["-c", "name", "--where", "id = :id", "--param", "id", "1"],
|
||||||
|
'[{"name": "Cleo"}]',
|
||||||
|
),
|
||||||
],
|
],
|
||||||
)
|
)
|
||||||
def test_rows(db_path, args, expected):
|
def test_rows(db_path, args, expected):
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue