sqlite-utils csv data.db "select ..." command

This commit is contained in:
Simon Willison 2019-01-25 07:50:20 -08:00
commit c83dd8a5eb
3 changed files with 55 additions and 0 deletions

View file

@ -6,6 +6,17 @@
The ``sqlite-utils`` command-line tool can be used to manipulate SQLite databases in a number of different ways.
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 docs.db "select id, title, author from documents"
This will default to including the column names as a header row. To exclude the headers, use ``--no-headers``:
$ sqlite-utils csv docs.db "select id, title, author from documents" --no-headers
Listing tables
==============

View file

@ -1,6 +1,8 @@
import click
import sqlite_utils
import json
import sys
import csv as csv_std
@click.group()
@ -92,3 +94,24 @@ def upsert(path, table, json_file, pk):
if isinstance(docs, dict):
docs = [docs]
db[table].upsert_all(docs, pk=pk)
@cli.command()
@click.argument(
"path",
type=click.Path(file_okay=True, dir_okay=False, allow_dash=False),
required=True,
)
@click.argument("sql")
@click.option(
"--no-headers", help="Exclude headers from CSV output", is_flag=True, default=False
)
def csv(path, sql, no_headers):
"Execute SQL query and return the results as CSV"
db = sqlite_utils.Database(path)
cursor = db.conn.execute(sql)
writer = csv_std.writer(sys.stdout)
if not no_headers:
writer.writerow([c[0] for c in cursor.description])
for row in cursor:
writer.writerow(row)

View file

@ -130,3 +130,24 @@ def test_upsert(db_path, tmpdir):
assert upsert_dogs == db.execute_returning_dicts(
"select * from dogs where id in (1, 2, 21) order by id"
)
def test_csv(db_path):
db = Database(db_path)
with db.conn:
db["dogs"].insert_all(
[
{"id": 1, "age": 4, "name": "Cleo"},
{"id": 2, "age": 2, "name": "Pancakes"},
]
)
result = CliRunner().invoke(
cli.cli, ["csv", db_path, "select id, name, age from dogs"]
)
assert 0 == result.exit_code
assert "id,name,age\n1,Cleo,4\n2,Pancakes,2\n" == result.output
# Test the no-headers option:
result = CliRunner().invoke(
cli.cli, ["csv", db_path, "select id, name, age from dogs", "--no-headers"]
)
assert "1,Cleo,4\n2,Pancakes,2\n" == result.output