SQLite Development
When to Use
- Configure SQLite for concurrent web, CLI, or desktop workloads and diagnose
SQLITE_BUSYfailures. - Enforce relational integrity and storage types with
PRAGMA foreign_keys = ONandSTRICTtables. - Maintain FTS5 full-text search indexes or execute SQLite schema migrations that require table rebuilds.
Prerequisites
Identify the SQLite library version (sqlite3.sqlite_version or SELECT sqlite_version()), driver/binding, concurrency pattern (single process vs multi-process, read/write ratio), and storage medium (local filesystem vs network mount). Inspect existing connection setup code, pragma defaults, and migration scripts. Avoid placing active WAL-mode SQLite databases on network filesystems where shared-memory locking does not work reliably.
Configure every connection intentionally
- Enable
PRAGMA foreign_keys = ONon every new connection. SQLite defaults this setting toOFFper connection even when tables declareREFERENCESconstraints. RunPRAGMA foreign_key_checkafter bulk loads or legacy repairs. - Enable
PRAGMA journal_mode = WALand set a per-connectionPRAGMA busy_timeoutthat fits the workload latency budget. With WAL andPRAGMA synchronous = NORMAL, an OS crash or power failure can roll back recently committed transactions. UseFULLwhen those commits must survive. - Use
BEGIN IMMEDIATEfor transactions that will write. A defaultBEGIN DEFERREDtransaction that reads first and upgrades to a write while another writer holds the lock fails immediately withSQLITE_BUSYwithout waiting forbusy_timeout. - Keep write transactions short and keep long read transactions from starving WAL checkpoints. Monitor WAL file growth and run explicit
PRAGMA wal_checkpoint(TRUNCATE)during maintenance windows when needed.
Enforce types, indexes, and FTS5 sync
- Declare new tables
STRICT(SQLite 3.37+) when dynamic type affinity is undesirable, so string values cannot silently enterINTEGERcolumns. - Store timestamps and money consistently (for example ISO-8601 UTC strings or Unix epoch integers, and integer minor units such as cents) and enforce invariants with
CHECKconstraints. - For FTS5 external-content tables (
content='table_name'), install matchingAFTER INSERT,AFTER DELETE, andAFTER UPDATEtriggers to keep the search index synchronized. When attaching FTS5 to an already-populated table, run an initial backfill (INSERT INTO <fts_table>(<fts_table>) VALUES('rebuild')) inside the setup transaction so pre-existing rows are indexed and later updates/deletes do not fail withSQLITE_CORRUPT_VTAB, then verify search and update behavior on a pre-existing row.
Run table-rebuild migrations safely
- SQLite
ALTER TABLEsupports renaming tables/columns, adding columns, and dropping simple columns, but changing column types, nullability,CHECKconstraints, or foreign keys requires the documented 12-step table-rebuild pattern. - Back up the database before destructive DDL (
VACUUM INTOor SQLite's online backup API rather than copying a live WAL database with raw file copy while writers are active). - SQLite ignores
PRAGMA foreign_keyschanges inside an active transaction, so disable foreign keys (PRAGMA foreign_keys = OFF) beforeBEGIN. Inside the transaction, record existing indexes, triggers, and dependent views fromsqlite_schema, drop dependent views before dropping the old table and renaming the replacement (otherwiseALTER TABLE ... RENAME TOfails view validation withno such table), recreate the table, copy rows explicitly by column list, drop the old table, rename the new table, recreate indexes/triggers/views, and runPRAGMA foreign_key_checkbeforeCOMMIT. RestorePRAGMA foreign_keys = ONafterCOMMITorROLLBACK(outside the transaction).
Example
An application using SQLite silently stored orphan rows, accepted string prices in an integer column, and hit
SQLITE_BUSYunder concurrent writes.
Run the worked WAL concurrency and integrity test to reproduce each failure mode and verify per-connection foreign_keys, STRICT tables, and BEGIN IMMEDIATE serialization under WAL mode.
Limitations
- Single-writer concurrency scales well for read-heavy and moderate-write single-node workloads, not multi-node horizontal write scaling.
STRICTtables require SQLite 3.37.0 or newer; checksqlite_version()before emittingSTRICTDDL.- WAL mode requires shared memory on a local filesystem and is unsuitable for NFS or SMB shares.
Safety and Side Effects
Test schema rebuilds, checkpoints, and VACUUM on a copy of the database first. Use VACUUM INTO or the online backup API for a consistent live copy. For a raw file copy, close every connection and keep writers stopped until the copy is complete. Keep production paths and user records out of examples and logs.
Related Skills and Documentation
- For PostgreSQL, MySQL, and SQL Server zero-downtime migrations, see SQL Database Migration Strategy.
- For engine selection and schema design fundamentals, see Database Design.
- For PostgreSQL-specific administration and tuning, see PostgreSQL.
- See SQLite's official documentation on WAL mode, Foreign Key Support, STRICT tables, and ALTER TABLE 12-step rebuilds.