Menu
×
   ❮     
HTML CSS JAVASCRIPT SQL PYTHON JAVA PHP C C++ C# AWS W3.CSS HOW TO BOOTSTRAP REACT MYSQL JQUERY EXCEL XML DJANGO NUMPY PANDAS NODEJS DSA TYPESCRIPT ANGULAR ANGULARJS GIT POSTGRESQL SQLITE MONGODB ASP AI R GO KOTLIN SWIFT SASS VUE GEN AI SCIPY CYBERSECURITY DATA SCIENCE INTRO TO PROGRAMMING INTRO TO HTML & CSS BASH RUST DART PLAYWRIGHT TOOLS MATH SHEETS POWER BI

SQLite Tutorial

SQLite HOME SQLite Intro SQLite Get Started

SQLite SQL

SQLite SQL SQLite SELECT SQLite SELECT DISTINCT SQLite WHERE SQLite ORDER BY SQLite AND SQLite OR SQLite NOT SQLite INSERT INTO SQLite NULL Values SQLite UPDATE SQLite DELETE SQLite LIMIT SQLite Aggregate SQLite MIN() and MAX() SQLite COUNT() SQLite SUM() SQLite AVG() SQLite LIKE SQLite Wildcards SQLite GLOB SQLite IN SQLite BETWEEN SQLite Aliases SQLite Joins SQLite INNER JOIN SQLite LEFT JOIN SQLite RIGHT JOIN SQLite FULL JOIN SQLite CROSS JOIN SQLite Self Join SQLite UNION SQLite UNION ALL SQLite INTERSECT and EXCEPT SQLite GROUP BY SQLite HAVING SQLite EXISTS SQLite Subqueries SQLite INSERT INTO SELECT SQLite CASE SQLite NULL Functions SQLite Comments SQLite Operators

SQLite Advanced

SQLite UPSERT SQLite RETURNING SQLite WITH (CTE) SQLite Window Functions SQLite JSON

SQLite Database

SQLite Create DB SQLite Attach DB SQLite Create Table SQLite Drop Table SQLite Alter Table SQLite Data Types SQLite STRICT Tables SQLite Constraints SQLite NOT NULL SQLite UNIQUE SQLite PRIMARY KEY SQLite FOREIGN KEY SQLite CHECK SQLite DEFAULT SQLite AUTOINCREMENT SQLite Generated Columns SQLite Create Index SQLite Views SQLite Triggers SQLite Dates SQLite Transactions SQLite PRAGMA SQLite Injection

SQLite Tools

SQLite Command Line SQLite Import and Export SQLite with Python

SQLite References

SQLite Functions

SQLite Exercises

SQLite Editor SQLite Quiz SQLite Exercises

SQLite Import and Export


Share

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.




Free account

Track your progress

XP earned 0 Day streak 0 Spaces 0 League —

New

Learn in Adventure Mode W3Schools Adventure App

Coding fundamentals as bite-sized lessons and challenges.

×

Contact Sales

If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail:
sales@w3schools.com

Report Error

If you want to report an error, or if you want to make a suggestion, send us an e-mail:
help@w3schools.com

W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookies and privacy policy.

Copyright 1999-2026 by W3Schools. All Rights Reserved. W3Schools is Powered by W3.CSS.