Import CSV to Sqlite DB from Commandline

SQlite Import from commandline

Sqlite is an amazingly powerful cute little database, which comes in handy when processing standalone jobs.

The feature I use the most is importing csv into table on the fly and use the table in my scripting.

Let see how to import csv into sqlite database from command line. This example was tested on linux (centos) but it should be the same in windows / mac.

Lets create a load.sql with following lines.

delete from yourtablename;
.import /yourpath/input.csv yourtablename

First line deletes the existing contents of the table, if you to append the data then remove or comment that line.

Second line is  dot (.) import command. Given path of your csv file followed by tablename.  As this is a dot command it has no semi colon (;).

Now from $ command prompt lets execute the following

$ sqlite3 yourdatabase.db < load.sql

In my testing I found, it takes 2 seconds to load 500,000 rows with handful of columns.

Boom !! you can login to your database to verify the result

$ sqlite3 yourdatabase.db

sql>select * from yourtable limit 5;

sql> .exit

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: