sqlite-utils rows -c, closes #200

This commit is contained in:
Simon Willison 2020-11-06 16:28:41 -08:00
commit 6863dc2677
3 changed files with 16 additions and 1 deletions

View file

@ -198,6 +198,12 @@ You can return every row in a specified table using the ``rows`` command::
This command accepts the same output options as ``query`` - so you can pass ``--nl``, ``--csv``, ``--tsv``, ``--no-headers``, ``--table`` and ``--fmt``.
You can use the ``-c`` option to specify a subset of columns to return::
$ sqlite-utils rows dogs.db dogs -c age -c name
[{"age": 4, "name": "Cleo"},
{"age": 2, "name": "Pancakes"}]
.. _cli_tables:
Listing tables

View file

@ -1061,6 +1061,7 @@ def search(
required=True,
)
@click.argument("dbtable")
@click.option("-c", "--column", type=str, multiple=True, help="Columns to return")
@output_options
@load_extension_option
@click.pass_context
@ -1068,6 +1069,7 @@ def rows(
ctx,
path,
dbtable,
column,
nl,
arrays,
csv,
@ -1079,10 +1081,13 @@ def rows(
load_extension,
):
"Output all rows in the specified table"
columns = "*"
if column:
columns = ", ".join("[{}]".format(c) for c in column)
ctx.invoke(
query,
path=path,
sql="select * from [{}]".format(dbtable),
sql="select {} from [{}]".format(columns, dbtable),
nl=nl,
arrays=arrays,
csv=csv,

View file

@ -1039,6 +1039,10 @@ def test_query_memory_does_not_create_file(tmpdir):
),
(["--arrays"], '[[1, "Cleo", 4],\n [2, "Pancakes", 2]]'),
(["--arrays", "--nl"], '[1, "Cleo", 4]\n[2, "Pancakes", 2]'),
(
["--nl", "-c", "age", "-c", "name"],
'{"age": 4, "name": "Cleo"}\n{"age": 2, "name": "Pancakes"}',
),
],
)
def test_rows(db_path, args, expected):