Skip to content

Commit 324ebc3

Browse files
committed
sqlite-utils rows --limit and --offset options, closes #381
1 parent 1d44b0c commit 324ebc3

File tree

4 files changed

+30
-1
lines changed

4 files changed

+30
-1
lines changed

docs/cli-reference.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -585,6 +585,8 @@ See :ref:`cli_rows`.
585585

586586
Options:
587587
-c, --column TEXT Columns to return
588+
--limit INTEGER Number of rows to return - defaults to everything
589+
--offset INTEGER SQL offset to use
588590
--nl Output newline-delimited JSON
589591
--arrays Output rows as arrays instead of objects
590592
--csv Output CSV

docs/cli.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -449,6 +449,8 @@ 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+
Use ``--limit N`` to only return the first ``N`` rows. Use ``--offset N`` to return rows starting from the specified offset.
453+
452454
.. _cli_tables:
453455

454456
Listing tables

sqlite_utils/cli.py

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1723,6 +1723,16 @@ def search(
17231723
)
17241724
@click.argument("dbtable")
17251725
@click.option("-c", "--column", type=str, multiple=True, help="Columns to return")
1726+
@click.option(
1727+
"--limit",
1728+
type=int,
1729+
help="Number of rows to return - defaults to everything",
1730+
)
1731+
@click.option(
1732+
"--offset",
1733+
type=int,
1734+
help="SQL offset to use",
1735+
)
17261736
@output_options
17271737
@load_extension_option
17281738
@click.pass_context
@@ -1731,6 +1741,8 @@ def rows(
17311741
path,
17321742
dbtable,
17331743
column,
1744+
limit,
1745+
offset,
17341746
nl,
17351747
arrays,
17361748
csv,
@@ -1745,10 +1757,15 @@ def rows(
17451757
columns = "*"
17461758
if column:
17471759
columns = ", ".join("[{}]".format(c) for c in column)
1760+
sql = "select {} from [{}]".format(columns, dbtable)
1761+
if limit:
1762+
sql += " limit {}".format(limit)
1763+
if offset:
1764+
sql += " offset {}".format(offset)
17481765
ctx.invoke(
17491766
query,
17501767
path=path,
1751-
sql="select {} from [{}]".format(columns, dbtable),
1768+
sql=sql,
17521769
nl=nl,
17531770
arrays=arrays,
17541771
csv=csv,

tests/test_cli.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -851,6 +851,14 @@ 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+
(
855+
["-c", "name", "--limit", "1"],
856+
'[{"name": "Cleo"}]',
857+
),
858+
(
859+
["-c", "name", "--limit", "1", "--offset", "1"],
860+
'[{"name": "Pancakes"}]',
861+
),
854862
],
855863
)
856864
def test_rows(db_path, args, expected):

0 commit comments

Comments
 (0)