Default command now executes queries, --csv or --json

I replaced the following commands:

    sqlite-utils json db.db "select * from table"
    sqlite-utils csv db.db "select * from table"

With a unified 'query' command, which is now set as the default:

    sqlite-utils db.db "select * from table"
    sqlite-utils db.db "select * from table" --csv
This commit is contained in:
Simon Willison 2019-02-22 17:40:21 -08:00
commit f2ca48c0da
5 changed files with 76 additions and 60 deletions

View file

@ -48,7 +48,7 @@ The ``Database()`` constructor can now accept a ``pathlib.Path`` object in addit
Two new commands: ``sqlite-utils csv`` and ``sqlite-utils json``
These commands execute a SQL query and return the results as CSV or JSON. See :ref:`cli_csv` and :ref:`cli_json` for more details.
These commands execute a SQL query and return the results as CSV or JSON. See :ref:`cli_query_csv` and :ref:`cli_query_json` for more details.
::

View file

@ -6,56 +6,38 @@
The ``sqlite-utils`` command-line tool can be used to manipulate SQLite databases in a number of different ways.
.. _cli_csv:
Running queries and returning CSV
=================================
You can execute a SQL query against a database and get the results back as CSV like this::
$ sqlite-utils csv dogs.db "select * from dogs"
id,age,name
1,4,Cleo
2,2,Pancakes
This will default to including the column names as a header row. To exclude the headers, use ``--no-headers``::
$ sqlite-utils csv dogs.db "select * from dogs" --no-headers
1,4,Cleo
2,2,Pancakes
.. _cli_json:
.. _cli_query_json:
Running queries and returning JSON
==================================
You can execute a SQL query against a database and get the results back as JSON like this::
$ sqlite-utils json dogs.db "select * from dogs"
$ sqlite-utils dogs.db "select * from dogs"
[{"id": 1, "age": 4, "name": "Cleo"},
{"id": 2, "age": 2, "name": "Pancakes"}]
Use ``--nl`` to get back newline-delimited JSON objects::
$ sqlite-utils json --nl dogs.db "select * from dogs"
$ sqlite-utils dogs.db "select * from dogs" --nl
{"id": 1, "age": 4, "name": "Cleo"}
{"id": 2, "age": 2, "name": "Pancakes"}
You can use ``--arrays`` to request ararys instead of objects::
$ sqlite-utils json --arrays dogs.db "select * from dogs"
$ sqlite-utils dogs.db "select * from dogs" --arrays
[[1, 4, "Cleo"],
[2, 2, "Pancakes"]]
You can also combine ``--arrays`` and ``--nl``::
$ sqlite-utils json --arrays --nl dogs.db "select * from dogs"
$ sqlite-utils dogs.db "select * from dogs" --arrays --nl
[1, 4, "Cleo"]
[2, 2, "Pancakes"]
If you want to pretty-print the output further, you can pipe it through ``python -mjson.tool``::
$ sqlite-utils json dogs.db "select * from dogs" | python -mjson.tool
$ sqlite-utils dogs.db "select * from dogs" | python -mjson.tool
[
{
"id": 1,
@ -69,6 +51,24 @@ If you want to pretty-print the output further, you can pipe it through ``python
}
]
.. _cli_query_csv:
Running queries and returning CSV
=================================
You can use the ``--csv`` option to return results as CSV::
$ sqlite-utils dogs.db "select * from dogs" --csv
id,age,name
1,4,Cleo
2,2,Pancakes
This will default to including the column names as a header row. To exclude the headers, use ``--no-headers``::
$ sqlite-utils dogs.db "select * from dogs" --csv --no-headers
1,4,Cleo
2,2,Pancakes
Listing tables
==============