ROBERT RICHARDSON
ROBERT RICHARDSON

Reputation: 2279

How can I import a CSV file into an SQLite table from the command line?

I need to get data from a PostgreSQL table into an SQLite table. I am exporting the data from PostgreSQL into a CSV file, and then trying to import it into SQLite from a command-line prompt. First I tried this:

c:> /sqlite3/sqlite3 sample.trd ".import source_file.csv trend_data"

For every line in the file, I got an error message saying SQLite expected three values and there was only one. Not really surprising because I never told SQLite that the file was a CSV file. So then I tried this:

c:> /sqlite3/sqlite3 sample.trd ".mode csv .import source_file.csv trend_data"

I got no errors, but I also got no data.

Upvotes: 1

Views: 1929

Answers (1)

CL.
CL.

Reputation: 180020

Each dot command must be alone in a single line.

You could pipe multiple commands into sqlite3, but in this case, it would be easier to set CSV mode with a parameter:

sqlite3 -csv sample.trd ".import source_file.csv trend_data"

Upvotes: 6

Related Questions