SQLite Import Data from CSV File to Table

Here we will learn how to import data from CSV or Excel external files and insert data into database tables with examples.

SQLite Import CSV File Data to Table

In SQLite we can easily import data from external files like CSV, excel to database tables by using the “.import” command.

Syntax of SQLite Import Command

Following is the syntax of the SQLite “import” command to import data from external files.

 

.import filename table-name

In the above syntax, the .import command is used to import data from an external file and insert it into the specified table. To import data from the CSV file, you must have to execute command .mode CSV before .import command, and the table must already exist.

 

Now we will see how to import data from CSV file to database table with examples.

SQLite Import Data from CSV to Table

I have a “test” CSV file at the location where our sqlite3.exe is existed like as shown following.

 

SQLite Import Data from CSV File from Project Folder

 

The test.csv file contains data like as shown below.

 

1,Shweta

2,Vinay

3,Lax

Now to import data from CSV file first create tabled called “test” in the database using the following query statement.

 

CREATE TABLE test (id INTEGER PRIMARY KEY, value text);

Once we did with table creation, now we will import test.csv data to test table like as shown below.

 

sqlite> .import test.csv test

If you observe the above example we are importing data from test.csv file to test table. Now, we will check the records of the test table using following statement.

 

sqlite> SELECT * FROM test;

 

id          value

----------  ----------

1           Shweta

2           Vinay

3           Lax

This is how we can import SQLite data to CSV or excel or other external files based on our requirements.