Skip to content

Menu

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.db

Open the database file app.db in the interactive shell, or start a new one with that name

.tables

List tables and views in the open database

.schema people

Print the CREATE statement for a table and its indexes

.mode box

Draw results as a boxed table with column headers

.mode csv

Print results as CSV, ready to redirect into a file

.once out.csv

Send only the next query's output to a file, then go back to the screen

From the docs, not run by us
.read seed.sql

Run every statement in a SQL file

From the docs, not run by us
.timer on

Print run time after each statement

From the docs, not run by us
.dump

Write the whole database as SQL text, useful for backups and diffs

From the docs, not run by us
.expert

Ask the shell to suggest indexes for the next query you type

Try it: index-advisorFrom the docs, not run by us

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 people

Load a CSV into a new table named after the header row

.import --csv --skip 1 people.csv people

Load into an existing table and skip the header line

From the docs, not run by us
.import --csv --schema temp data.csv tab1

Load into a table in another schema such as TEMP or an attached database

From the docs, not run by us
sqlite3 -csv -header app.db 'SELECT * FROM people' > people.csv

Export a query result to CSV from the command line in one step

Try it: CSV ↔ JSON ConverterFrom the docs, not run by us

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

From the docs, not run by us

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'

From the docs, not run by us
PRAGMA synchronous = NORMAL;

In WAL mode, only checkpoints issue an fsync, which makes ordinary commits cheaper

From the docs, not run by us
PRAGMA table_info(people);

One row per column with name, type, not-null flag, default and primary key position

From the docs, not run by us
PRAGMA optimize;

Let SQLite refresh planner statistics; run it before closing a connection and after creating indexes

From the docs, not run by us
PRAGMA user_version = 3;

Store your own schema version number in the file header for simple migrations

From the docs, not run by us

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 REFERENCES clauses are enforced because the schema has them.

    Run PRAGMA foreign_keys = ON on every connection, ideally in your driver's connect hook.

  • Expecting INTEGER columns to reject text values.

    Declare the table STRICT, or add a CHECK (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_timeout so writers queue up instead of failing immediately.

  • Letting .import create the table and then doing number comparisons on the columns.

    Create the table with proper types first, then .import --csv --skip 1 into it.

  • Forgetting that .import into an existing table loads the header row as data.

    Pass --skip 1 whenever the table already exists and the file has a header.

  • Copying only the main .db file as a backup of a WAL database.

    Use the shell's .backup command or checkpoint first, so unmerged pages in the -wal file are included.