SQLite Transactions, WAL, and Locking Cheatsheet

BEGIN modes, COMMIT and ROLLBACK, savepoints, WAL mode, busy timeouts, and concurrency patterns for SQLite applications.

View
StandardDetailedCompact
Export
Copy the compact sheet, download it, or print it.
Download
`D` dense toggle · `C` copy all
## Transactions and savepoints
Start a transaction
BEGIN;
INSERT INTO audit_log(action) VALUES ('start');
UPDATE counters SET value = value + 1 WHERE key = 'jobs';
COMMIT;

# Bundle multiple statements atomically.

Begin an IMMEDIATE transaction
BEGIN IMMEDIATE;

# Reserve the write lock up front.

Roll back a transaction
ROLLBACK;

# Undo uncommitted changes.

Use a savepoint
SAVEPOINT import_batch;
INSERT INTO tags(name) VALUES ('sqlite');
ROLLBACK TO import_batch;
RELEASE import_batch;

# Create a nested rollback point within a transaction.

## WAL mode and lock handling
Enable WAL journaling
PRAGMA journal_mode = WAL;

# Switch the database to write-ahead logging mode.

Set a busy timeout
PRAGMA busy_timeout = 5000;

# Wait briefly when the database is locked.

Run a WAL checkpoint
PRAGMA wal_checkpoint(TRUNCATE);

# Merge WAL contents back into the main database file.

Inspect locking mode
PRAGMA locking_mode;

# See or set the database locking mode.

Recommended next

No recommendations yet.