mirror of
https://github.com/simonw/sqlite-utils.git
synced 2026-07-23 17:34:32 +02:00
Implemented sqlite-utils insert --lines
This commit is contained in:
parent
d1ed2f423d
commit
f1569c9f7f
3 changed files with 26 additions and 0 deletions
|
|
@ -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 <cli_convert>`::
|
||||
|
||||
$ sqlite-utils insert logs.db loglines logfile.log --lines
|
||||
|
||||
.. _cli_insert_replace:
|
||||
|
||||
Insert-replacing data
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
|
|
|
|||
|
|
@ -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"))
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue