mirror of
https://github.com/simonw/sqlite-utils.git
synced 2026-07-23 09:24:31 +02:00
sqlite-utils insert ... --nl option, closes #6
This commit is contained in:
parent
57fc8ce55e
commit
5309c5c775
3 changed files with 29 additions and 7 deletions
|
|
@ -123,6 +123,11 @@ You can import all three records into an automatically created ``dogs`` table an
|
|||
|
||||
$ sqlite-utils insert dogs.db dogs dogs.json --pk=id
|
||||
|
||||
You can also import newline-delimited JSON using the ``--nl`` option. Since [Datasette](https://datasette.readthedocs.io/) can export newline-delimited JSON, you can combine the two tools like so::
|
||||
|
||||
$ curl -L "https://latest.datasette.io/fixtures/facetable.json?_shape=array&_nl=on" \
|
||||
| sqlite-utils insert nl-demo.db facetable - --pk=id --nl
|
||||
|
||||
Upserting data
|
||||
==============
|
||||
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -104,13 +104,25 @@ def test_insert_multiple_with_primary_key(db_path, tmpdir):
|
|||
cli.cli, ["insert", db_path, "dogs", json_path, "--pk", "id"]
|
||||
)
|
||||
assert 0 == result.exit_code
|
||||
assert dogs == Database(db_path).execute_returning_dicts(
|
||||
"select * from dogs order by id"
|
||||
)
|
||||
db = Database(db_path)
|
||||
assert dogs == db.execute_returning_dicts("select * from dogs order by id")
|
||||
assert ["id"] == db["dogs"].pks
|
||||
|
||||
|
||||
def test_insert_newline_delimited(db_path):
|
||||
result = CliRunner().invoke(
|
||||
cli.cli,
|
||||
["insert", db_path, "from_json_nl", "-", "--nl"],
|
||||
input='{"foo": "bar", "n": 1}\n{"foo": "baz", "n": 2}',
|
||||
)
|
||||
assert 0 == result.exit_code, result.output
|
||||
db = Database(db_path)
|
||||
assert [
|
||||
{"foo": "bar", "n": 1},
|
||||
{"foo": "baz", "n": 2},
|
||||
] == db.execute_returning_dicts("select foo, n from from_json_nl")
|
||||
|
||||
|
||||
def test_upsert(db_path, tmpdir):
|
||||
test_insert_multiple_with_primary_key(db_path, tmpdir)
|
||||
json_path = str(tmpdir / "upsert.json")
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue