SQLite Cheat Sheet: sqlite3 CLI, STRICT Tables, Pragmas and WAL
Checked against SQLite SQLite 3.54.1 docs (2026-10-16); examples run on sqlite3 3.51.0 on 28 November 2026. Each section links to the official docs it was checked against.
This sheet is for developers who use SQLite as an app database, a test fixture or a quick place to query a CSV. It follows the sqlite.org documentation as of release 3.54.1 (16 October 2026). The examples were run on the sqlite3 3.51.0 shell that ships with macOS, so rows about features newer than that are marked untested.
When to use what
Two settings decide whether SQLite behaves the way you expect from other databases. Foreign keys are off by default and must be switched on for every connection with PRAGMA foreign_keys = ON. Column types are hints, not rules: a column declared INTEGER will happily keep the text 'abc'. If you want type errors, declare the table STRICT, which needs 3.37.0 or newer to open.
For anything with more than one process touching the file, such as a web app with a background worker, switch to WAL with PRAGMA journal_mode = WAL. Readers stop blocking the writer, and the setting sticks to the file, unlike the other journal modes. Pair it with a busy_timeout so a second writer waits instead of failing straight away. The SQLite in production post covers where that setup stops being enough, and edge databases with Turso and libSQL covers the hosted forks.
In the shell, use .mode box for reading and .mode csv or .once when you need a file. For loading data, .import --csv is fine for a one-off, but it makes every column TEXT when it creates the table, so create the table first if types matter. The CSV to SQL tool is another way to get a typed CREATE TABLE plus inserts.
Dot-commands are handled by the shell itself, take no semicolon and must start at the beginning of a line.
sqlite3 app.dbOpen the database file app.db in the interactive shell, or start a new one with that name
.tablesList tables and views in the open database
.schema peoplePrint the CREATE statement for a table and its indexes
.mode boxDraw results as a boxed table with column headers
.mode csvPrint results as CSV, ready to redirect into a file
.once out.csvSend only the next query's output to a file, then go back to the screen
.read seed.sqlRun every statement in a SQL file
.timer onPrint run time after each statement
.dumpWrite the whole database as SQL text, useful for backups and diffs
.expertAsk the shell to suggest indexes for the next query you type
If the target table does not exist, .import creates it, takes column names from the first row and makes every column TEXT. If it does exist, the first row is treated as data unless you pass --skip 1.
.import --csv people.csv peopleLoad a CSV into a new table named after the header row
.import --csv --skip 1 people.csv peopleLoad into an existing table and skip the header line
.import --csv --schema temp data.csv tab1Load into a table in another schema such as TEMP or an attached database
sqlite3 -csv -header app.db 'SELECT * FROM people' > people.csvExport a query result to CSV from the command line in one step
Affinity rules are on https://sqlite.org/datatype3.html: a declared type containing INT gets INTEGER affinity; CHAR, CLOB or TEXT gets TEXT; BLOB or no type gets BLOB; REAL, FLOA or DOUB gets REAL; anything else NUMERIC.
CREATE TABLE loose (n INTEGER);Ordinary table: inserting 'abc' is stored as text, '42' is converted to the integer 42
SELECT n, typeof(n) FROM loose;Show the storage class each value actually got
CREATE TABLE s (id INTEGER PRIMARY KEY, n INT, t TEXT) STRICT;Enforce declared types; only INT, INTEGER, REAL, TEXT, BLOB and ANY are allowed
INSERT INTO s(n) VALUES ('abc');Fails in a STRICT table with 'cannot store TEXT value in INT column'
INSERT INTO s(n) VALUES ('42');Still works in a STRICT table because '42' converts to an integer without loss
PRAGMA integrity_check;Check the file for corruption; on STRICT tables it also checks column types
WAL details are on https://sqlite.org/wal.html and the foreign key rules on https://sqlite.org/foreignkeys.html.
PRAGMA foreign_keys = ON;Enforce foreign keys for this connection; the default is off and it does nothing inside a transaction
PRAGMA journal_mode = WAL;Switch to write-ahead logging; returns 'wal' and creates -wal and -shm files next to the database
PRAGMA journal_mode;Check the mode; WAL persists after the database is closed and reopened
PRAGMA busy_timeout = 5000;Wait up to 5000 ms for a lock instead of failing at once with 'database is locked'
PRAGMA synchronous = NORMAL;In WAL mode, only checkpoints issue an fsync, which makes ordinary commits cheaper
PRAGMA table_info(people);One row per column with name, type, not-null flag, default and primary key position
PRAGMA optimize;Let SQLite refresh planner statistics; run it before closing a connection and after creating indexes
PRAGMA user_version = 3;Store your own schema version number in the file header for simple migrations
Worked example: Import a CSV, then see why the types are wrong
Here is a three-line CSV and what happened when I loaded it with sqlite3 3.51.0:
$ printf 'id,name,team\n1,Ana,core\n2,Ben,web\n3,Cy,core\n' > people.csv
$ sqlite3 t1.db
sqlite> .import --csv people.csv people
sqlite> .schema people
CREATE TABLE IF NOT EXISTS "people"(
"id" TEXT, "name" TEXT, "team" TEXT);
sqlite> PRAGMA foreign_keys;
0
sqlite> PRAGMA journal_mode = WAL;
wal
The import worked, but id became TEXT, so ORDER BY id would sort 10 before 2. The fix is to create the table yourself first, with id INTEGER PRIMARY KEY, and import with --skip 1. The 0 from PRAGMA foreign_keys is the default; every new connection needs PRAGMA foreign_keys = ON again. After the WAL pragma, t1.db-wal and t1.db-shm appeared beside the file, and reopening the file reported wal again without me setting it. Don't copy just the .db file while a writer is active in WAL mode; use the shell's .backup command so recent commits still sitting in the -wal file come with it.
Common mistakes
Assuming
REFERENCESclauses are enforced because the schema has them.Run
PRAGMA foreign_keys = ONon every connection, ideally in your driver's connect hook.Expecting
INTEGERcolumns to reject text values.Declare the table
STRICT, or add aCHECK (typeof(col) = 'integer')constraint on older schemas.Getting 'database is locked' errors as soon as a second process writes.
Use WAL mode and set
busy_timeoutso writers queue up instead of failing immediately.Letting
.importcreate the table and then doing number comparisons on the columns.Create the table with proper types first, then
.import --csv --skip 1into it.Forgetting that
.importinto an existing table loads the header row as data.Pass
--skip 1whenever the table already exists and the file has a header.Copying only the main
.dbfile as a backup of a WAL database.Use the shell's
.backupcommand or checkpoint first, so unmerged pages in the -wal file are included.
Related cheat sheets
Sources
- https://sqlite.org/cli.html
- https://sqlite.org/stricttables.html
- https://sqlite.org/datatype3.html
- https://sqlite.org/pragma.html
- https://sqlite.org/wal.html
- https://sqlite.org/foreignkeys.html
- https://sqlite.org/changes.html
How we research: where our facts come from, how often we re-check them, and why no one can pay for a review or a verdict.