dbcli / litecli

CLI for SQLite Databases with auto-completion and syntax highlighting
https://litecli.com
BSD 3-Clause "New" or "Revised" License
2.12k stars 68 forks source link

Support CSV import #77

Closed ccharles closed 4 years ago

ccharles commented 4 years ago

SQLite allows users to import data from CSV files:

$ cat foo.csv
one,two,three
1,2,3
4,5,6

$ sqlite3 foo.db
SQLite version 3.22.0 2018-01-22 18:45:57
Enter ".help" for usage hints.
sqlite> .mode csv
sqlite> .import foo.csv foo
sqlite> .schema foo
CREATE TABLE foo(
  "one" TEXT,
  "two" TEXT,
  "three" TEXT
);
sqlite> select * from foo;
1,2,3
4,5,6
sqlite>

However, LiteCLI gives a syntax error:

$ litecli foo.db
Version: 1.2.0
Mail: https://groups.google.com/forum/#!forum/litecli-users
GitHub: https://github.com/dbcli/litecli
foo.db> .mode csv
Changed table format to csv
Time: 0.000s
foo.db> .import foo.csv foo
near ".": syntax error
foo.db>

Aside from ., CSV paths may include other unexpected characters like : and /, e.g. in this example from the official documentation:

sqlite> .mode csv
sqlite> .import C:/work/somedata.csv tab1

This likely affects CSV export as well.

Can CSV import (and, ideally, output) support be added to LiteCLI?

zzl0 commented 4 years ago

@c0psrul3 Thanks for your feedback, because .import is a part of the command line program interface (shell.c.in), it's not part of SQLite library, so we need to implement this kind of commands in litecli.

I added .import (Usage: .import filename table) command in litecli (#79 ). Currently,

  1. It requires that the number of values in each row matches the number of columns in the provided table.
  2. The provided table must exist before running this command.

Please let me know if this works for you.