Here we will learn how to import data from CSV or Excel external files and insert data into database tables with examples.
In SQLite we can easily import data from external files like csv, excel to database tables by using “.import” command.
Following is the syntax of SQLite “import” command to import data from external files.
.import filename table-name
In 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 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.
I have “test” CSV file at the location where our sqlite3.exe is exist like as shown following.
The test.csv file contains data like as shown below.
Now to import data from CSV file first create tabled called “test” in database using following query statement.
CREATE TABLE test (id INTEGER PRIMARY KEY, value text);
Once we done with table creation, now we will import test.csv data to test table like as shown below.
sqlite> .import test.csv test
If you oserve above example we are importing data from test.csv file to test table. Now, we will check the records of test table using following statement.
sqlite> SELECT * FROM test;
This is how we can import SQLite data to csv or excel or other external files based on our requirements.