Implemented and documented sqlite-utils insert --all

This commit is contained in:
Simon Willison 2022-01-05 18:16:51 -08:00
commit e66299c6ed
3 changed files with 38 additions and 3 deletions

View file

@ -876,15 +876,35 @@ If your file does not include this row, you can use the ``--no-headers`` option
If you do this, the table will be created with column names called ``untitled_1`` and ``untitled_2`` and so on. You can then rename them using the ``sqlite-utils transform ... --rename`` command, see :ref:`cli_transform_table`.
.. _cli_insert_lines:
.. _cli_insert_unstructured:
Inserting newline-delimited data
================================
Inserting unstructured data with \-\-lines and \-\-all
======================================================
If you have an unstructured file you can insert its contents into a table with a single ``line`` column containing each line from the file using ``--lines``. This can be useful if you intend to further analyze those lines using SQL string functions or :ref:`sqlite-utils convert <cli_convert>`::
$ sqlite-utils insert logs.db loglines logfile.log --lines
This will produce the following schema:
.. code-block:: sql
CREATE TABLE [loglines] (
[line] TEXT
);
You can also insert the entire contents of the file into a single column called ``all`` using ``--all``::
$ sqlite-utils insert content.db content file.txt --all
The schema here will be:
.. code-block:: sql
CREATE TABLE [content] (
[all] TEXT
);
.. _cli_insert_replace:
Insert-replacing data

View file

@ -785,6 +785,8 @@ def insert_upsert_implementation(
docs = tracker.wrap(docs)
elif lines:
docs = ({"line": line.strip()} for line in decoded)
elif all:
docs = ({"all": decoded.read()},)
elif convert:
fn = _compile_code(convert, imports)
docs = (fn(line) for line in decoded)

View file

@ -337,3 +337,16 @@ def test_insert_lines(db_path):
{"line": "Second line"},
{"line": '{"foo": "baz"}'},
] == list(db.query("select line from from_lines"))
def test_insert_all(db_path):
result = CliRunner().invoke(
cli.cli,
["insert", db_path, "from_all", "-", "--all"],
input='First line\nSecond line\n{"foo": "baz"}',
)
assert 0 == result.exit_code, result.output
db = Database(db_path)
assert [{"all": 'First line\nSecond line\n{"foo": "baz"}'}] == list(
db.query("select [all] from from_all")
)