sqlite-utils insert ... --nl option, closes #6

This commit is contained in:
Simon Willison 2019-01-27 18:17:38 -08:00
commit 5309c5c775
3 changed files with 29 additions and 7 deletions

View file

@ -71,12 +71,17 @@ def optimize(path, no_vacuum):
@click.argument("table")
@click.argument("json_file", type=click.File(), required=True)
@click.option("--pk", help="Column to use as the primary key, e.g. id")
def insert(path, table, json_file, pk):
@click.option("--nl", is_flag=True, help="Expect newline-delimited JSON")
def insert(path, table, json_file, pk, nl):
"Insert records from JSON file into the table, create table if it is missing"
db = sqlite_utils.Database(path)
docs = json_std.load(json_file)
if isinstance(docs, dict):
docs = [docs]
if nl:
# TODO: Use a generator once #7 is solved
docs = [json_std.loads(line) for line in json_file]
else:
docs = json_std.load(json_file)
if isinstance(docs, dict):
docs = [docs]
db[table].insert_all(docs, pk=pk)