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 the “.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.
I have a “test” CSV file at the location where our sqlite3.exe is existed 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 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;
This is how we can import SQLite data to CSV or excel or other external files based on our requirements.