Skip to content

Commit 8425188

Browse files
committed
Add .import command
1 parent cd5d4e0 commit 8425188

File tree

4 files changed

+57
-2
lines changed

4 files changed

+57
-2
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
/src
77
/test/behave.ini
88
/litecli_env
9+
/.venv
910
/.eggs
1011

1112
.vagrant

changelog.md

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
Upcomming:
2+
==========
3+
4+
Features:
5+
---------
6+
* Added `.import` command for importing data from file into table. (Thanks: [Zhaolong Zhu])
7+
18
1.2.0
29
=====
310

@@ -34,4 +41,5 @@ Features:
3441
[Amjith]: https://blog.amjith.com
3542
[Zhiming Wang]: https://github.com/zmwangx
3643
[Irina Truong]: https://github.com/j-bennet
37-
[Shawn Chapla]: https://github.com/shwnchpl
44+
[Shawn Chapla]: https://github.com/shwnchpl
45+
[Zhaolong Zhu]: https://github.com/zzl0

litecli/packages/completion_engine.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ def suggest_special(text):
110110
if cmd in ["\\f", "\\fs", "\\fd"]:
111111
return [{"type": "favoritequery"}]
112112

113-
if cmd in ["\\d", "\\dt", "\\dt+", ".schema"]:
113+
if cmd in ["\\d", "\\dt", "\\dt+", ".schema", ".import"]:
114114
return [
115115
{"type": "table", "schema": []},
116116
{"type": "view", "schema": []},

litecli/packages/special/dbcommands.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
from __future__ import unicode_literals
2+
import csv
23
import logging
34
import os
5+
import sys
46
import platform
57
import shlex
68
from sqlite3 import ProgrammingError
@@ -216,3 +218,47 @@ def read_script(cur, arg, **_):
216218
script = f.read()
217219
cur.executescript(script)
218220
return [(None, None, None, "")]
221+
222+
223+
@special_command(
224+
".import",
225+
".import filename table",
226+
"Import data from filename into table",
227+
arg_type=PARSED_QUERY,
228+
case_sensitive=True,
229+
)
230+
def import_file(cur, arg=None, **_):
231+
args = shlex.split(arg)
232+
if len(args) != 2:
233+
raise TypeError("Usage: .import filename table")
234+
235+
filename, table = args
236+
cur.execute("PRAGMA table_info(%s)" % table)
237+
ncols = len(cur.fetchall())
238+
insert_tmpl = 'INSERT INTO "%s" VALUES (?%s)' % (table, ',?' * (ncols - 1))
239+
240+
with open(filename, "r") as csvfile:
241+
dialect = csv.Sniffer().sniff(csvfile.read(1024))
242+
csvfile.seek(0)
243+
reader = csv.reader(csvfile, dialect)
244+
245+
cur.execute("BEGIN")
246+
ninserted, nignored = 0, 0
247+
for i, row in enumerate(reader):
248+
if len(row) != ncols:
249+
print(
250+
"%s:%d expected %d columns but found %d - ignored" % (
251+
filename, i, ncols, len(row)
252+
),
253+
file=sys.stderr
254+
)
255+
nignored += 1
256+
continue
257+
cur.execute(insert_tmpl, row)
258+
ninserted += 1
259+
cur.execute("COMMIT")
260+
261+
status = "Inserted %d rows into %s" % (ninserted, table)
262+
if nignored > 0:
263+
status += " (%d rows are ignored)" % nignored
264+
return [(None, None, None, status)]

0 commit comments

Comments
 (0)