diff --git a/docs/cli.rst b/docs/cli.rst index 6482c46..f93fc57 100644 --- a/docs/cli.rst +++ b/docs/cli.rst @@ -876,6 +876,15 @@ 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: + +Inserting newline-delimited data +================================ + +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 + .. _cli_insert_replace: Insert-replacing data diff --git a/sqlite_utils/cli.py b/sqlite_utils/cli.py index 319dee1..6d3f8eb 100644 --- a/sqlite_utils/cli.py +++ b/sqlite_utils/cli.py @@ -783,6 +783,8 @@ def insert_upsert_implementation( if detect_types: tracker = TypeTracker() docs = tracker.wrap(docs) + elif lines: + docs = ({"line": line.strip()} for line in decoded) 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 b01b2cf..2922a2f 100644 --- a/tests/test_cli_insert.py +++ b/tests/test_cli_insert.py @@ -322,3 +322,18 @@ def test_insert_alter(db_path, tmpdir): {"foo": "baz", "n": 2, "baz": None}, {"foo": "bar", "baz": 5, "n": None}, ] == list(db.query("select foo, n, baz from from_json_nl")) + + +def test_insert_lines(db_path): + result = CliRunner().invoke( + cli.cli, + ["insert", db_path, "from_lines", "-", "--lines"], + input='First line\nSecond line\n{"foo": "baz"}', + ) + assert 0 == result.exit_code, result.output + db = Database(db_path) + assert [ + {"line": "First line"}, + {"line": "Second line"}, + {"line": '{"foo": "baz"}'}, + ] == list(db.query("select line from from_lines"))