From e66299c6eda3091557504526aaf0f64fb321cb35 Mon Sep 17 00:00:00 2001 From: Simon Willison Date: Wed, 5 Jan 2022 18:16:51 -0800 Subject: [PATCH] Implemented and documented sqlite-utils insert --all --- docs/cli.rst | 26 +++++++++++++++++++++++--- sqlite_utils/cli.py | 2 ++ tests/test_cli_insert.py | 13 +++++++++++++ 3 files changed, 38 insertions(+), 3 deletions(-) diff --git a/docs/cli.rst b/docs/cli.rst index f93fc57..5bf02d2 100644 --- a/docs/cli.rst +++ b/docs/cli.rst @@ -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 `:: $ 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 diff --git a/sqlite_utils/cli.py b/sqlite_utils/cli.py index 6d3f8eb..a5b979f 100644 --- a/sqlite_utils/cli.py +++ b/sqlite_utils/cli.py @@ -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) diff --git a/tests/test_cli_insert.py b/tests/test_cli_insert.py index 2922a2f..063c43f 100644 --- a/tests/test_cli_insert.py +++ b/tests/test_cli_insert.py @@ -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") + )