diff --git a/docs/cli.rst b/docs/cli.rst index 9044039..a122234 100644 --- a/docs/cli.rst +++ b/docs/cli.rst @@ -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 diff --git a/sqlite_utils/cli.py b/sqlite_utils/cli.py index e75d514..b3df70a 100644 --- a/sqlite_utils/cli.py +++ b/sqlite_utils/cli.py @@ -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, diff --git a/tests/test_cli.py b/tests/test_cli.py index 00efdf1..c82a342 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -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):