Skip to main content

SQLite Cheatsheet

By Dejan Panovski •Updated on • Download PDF

SQLite command-line reference for opening databases, running SQL, formatting results, importing CSV files, and making backups.

This cheatsheet covers opening SQLite databases, running queries, formatting results, importing CSV files, and making backups with sqlite3. Run sqlite3 commands in your terminal, and enter SQL and dot commands in the SQLite shell.

Open and Inspect

For worked examples, see our sqlite3 command guide .

CommandDescription
sqlite3 tasks.dbOpen a database; create the file on the first write if it is missing
sqlite3 -readonly tasks.dbInspect an existing database without allowing writes
sqlite3 :memory:Open a database that disappears when the shell exits
sqlite3 --versionShow the installed SQLite version
.databasesShow open databases and their file paths
.tablesList tables and views
.schema tasksShow the SQL that defines a table
.helpShow available dot commands
.quitExit the SQLite shell

Run SQL

End SQL statements with a semicolon. Dot commands do not need one. Use WHERE to limit which rows an update changes.

SQLDescription
CREATE TABLE tasks (id INTEGER PRIMARY KEY, title TEXT NOT NULL, done INTEGER DEFAULT 0);Create a task table with automatically assigned IDs
INSERT INTO tasks (title) VALUES ('Check backups');Add a task with the default done value of 0
SELECT * FROM tasks LIMIT 10;Preview up to ten rows
SELECT title FROM tasks WHERE done = 0;List unfinished tasks
SELECT count(*) FROM tasks;Count all rows
UPDATE tasks SET done = 1 WHERE id = 1;Mark one task as completed

Output Formats

Set the mode before changing headers. Column spacing and alignment can vary by SQLite version.

CommandDescription
.mode listSeparate columns with pipes
.mode columnAlign values in columns
.mode boxAdd table borders
.mode jsonPrint results as JSON
.mode csvFormat results as CSV
.headers onInclude column names where the mode supports them
.headers offHide column names where the mode supports it

CSV Import and Export

Imports append rows. Use --skip 1 for a CSV header when the target table already exists. To export, set .mode csv and .headers on before .once. Choose an unused output filename.

CommandDescription
.import --csv contacts.csv contactsImport CSV; a new table uses the first row as column names
.import --csv --skip 1 contacts.csv contactsSkip the header when importing into an existing table
.once report.csvSend the next query result to a file
SELECT id, title FROM tasks;Run after .once to export the selected columns
.output report.csvSend query results to a file until output is changed
.outputSend query results back to the terminal

Transactions and Locks

Back up first and preview the matching rows before deleting. Use ROLLBACK to discard changes or COMMIT to keep them.

CommandDescription
BEGIN;Start a transaction
SELECT * FROM tasks WHERE done = 1;Preview completed tasks before deletion
DELETE FROM tasks WHERE done = 1;Delete completed tasks inside the transaction
ROLLBACK;Undo the uncommitted changes
COMMIT;Keep the changes; they can no longer be rolled back
.timeout 5000Wait up to five seconds for a lock; does not release another connection’s lock

Backup and Restore

Run these commands in your terminal. Choose unused backup filenames and restore SQL into a new database. Use .backup for a live database, including committed data still in its WAL file.

CommandDescription
sqlite3 -readonly tasks.db ".backup backup.db"Create a consistent database copy
sqlite3 -readonly tasks.db .dump > backup.sqlExport the database as SQL statements
sqlite3 -bail restored.db < backup.sqlRestore SQL into a new database and stop on errors
sqlite3 -readonly backup.db "PRAGMA integrity_check;"Check database structure; ok means the check passed