SQLite Import and Export
Export to a CSV File
CSV files can be opened in spreadsheet programs like Excel. In sqlite3, set the output to CSV, choose a file, and run a query:
sqlite> .headers on
sqlite> .mode csv
sqlite> .output shippers.csv
sqlite> SELECT * FROM Shippers;
sqlite> .output stdout
The commands print nothing on the screen. The result is written to the file shippers.csv, which looks like this:
ShipperID,ShipperName,Phone
1,"Speedy Express","(503) 555-9831"
2,"United Package","(503) 555-3199"
3,"Federal Shipping","(503) 555-9931"
Texts with spaces or special characters are put in double quotes.
You can also do it in one line from the command line:
sqlite3 -header -csv northwind.db "SELECT * FROM Shippers;" > shippers.csv
Import a CSV File
.import reads a CSV file into a table. If the table does not exist, sqlite3 creates it, and uses the first line of the file as the column names. Here, the file shippers.csv is imported into a new table, and then the new table is selected:
sqlite> .import --csv shippers.csv NewShippers
sqlite> SELECT * FROM NewShippers;
1|Speedy Express|(503) 555-9831
2|United Package|(503) 555-3199
3|Federal Shipping|(503) 555-9931
If the table already exists, the first line is imported as data. Add --skip 1 to skip the line with the column names. Here, the same file is imported into NewShippers once more, so the table now has twice as many rows:
sqlite> .import --csv --skip 1 shippers.csv NewShippers
sqlite> SELECT COUNT(*) FROM NewShippers;
6
Export to JSON
Set the mode to json to write the result as JSON:
sqlite> .mode json
sqlite> .output shippers.json
sqlite> SELECT * FROM Shippers;
sqlite> .output stdout
The file shippers.json looks like this:
[{"ShipperID":1,"ShipperName":"Speedy Express","Phone":"(503) 555-9831"},
{"ShipperID":2,"ShipperName":"United Package","Phone":"(503) 555-3199"},
{"ShipperID":3,"ShipperName":"Federal Shipping","Phone":"(503) 555-9931"}]
Back Up a Database
The .backup command makes a copy of the database in a new file:
sqlite> .backup backup.db
The copy has the same tables and data. Here, the number of customers is selected from the copy:
sqlite3 backup.db "SELECT COUNT(*) FROM Customers;"
91
With SQL, you can make a copy with VACUUM INTO. It works in SQLite 3.27.0 and later:
VACUUM INTO 'backup.db';
Tip: When no program is using the database, you can also just copy the database file.
Save the Database as SQL
.dump writes the whole database as SQL statements. The file can be used to create the same database again, also in another database system:
sqlite3 northwind.db .dump > northwind.sql
You can also dump only one table. This is what the SQL for the Shippers table looks like:
sqlite3 northwind.db ".dump Shippers"
PRAGMA foreign_keys=OFF;
BEGIN TRANSACTION;
CREATE TABLE Shippers (
ShipperID INTEGER PRIMARY KEY,
ShipperName TEXT,
Phone TEXT
);
INSERT INTO Shippers VALUES(1,'Speedy Express','(503) 555-9831');
INSERT INTO Shippers VALUES(2,'United Package','(503) 555-3199');
INSERT INTO Shippers VALUES(3,'Federal Shipping','(503) 555-9931');
COMMIT;
Run the file to create a new database with the same tables and data. Here, the new database copy.db is created, and the number of customers is selected from it:
sqlite3 copy.db < northwind.sql
sqlite3 copy.db "SELECT COUNT(*) FROM Customers;"
91
Note: Windows PowerShell does not support < and >. There, run the commands in the Command Prompt (cmd) instead.