Skip to content

SQLite

Scope: SQLite embedded in an application as its local database or file format: mobile and desktop apps, local-first data, game saves, tools, and small servers that keep the database on their own disk. The guidelines cover SQLite 3.54, the current release, and point out features that need a minimum version, because apps often use the copy of SQLite that the operating system provides. Hosted services built on SQLite, such as Cloudflare D1, aren't covered. Architecture-level data rules, such as versioned migrations and backups, are in general/data.md (DATA-2, DATA-8), and validating input in general/security.md (SEC-10). For save data in games, this file shows how to apply GAMEDEV-10 and GAMEDEV-11 from Game development in SQLite, and Steam Cloud saves are in Steam and PC games (STEAM-9). Offline-first mobile apps with a local database are covered in Android (ANDROID-7) and Flutter, and backends that sync data to the cloud in Firebase and Supabase. When the data lives on a server that many clients share, see PostgreSQL.

Entries are sorted by score from 1 to 10 (criteria): 9โ€“10 start here or strongly recommended, 6โ€“8 recommended, 1โ€“5 further reading or conditional.

Adopted references

  • 9 SQLite Documentation (SQLite): the baseline. The SQL dialect, the C interface, pragmas, file format, locking, transactions and the technical notes on using SQLite well.
  • 8 Appropriate Uses For SQLite (SQLite): where SQLite works well, where a client/server database works better, and a checklist for choosing between them.
  • 8 How To Corrupt An SQLite Database File (SQLite): the ways databases get corrupted in practice, such as copying files during a transaction, deleting journals, broken file locking and disabled syncs, and how to avoid each one.
  • 8 Defense Against The Dark Arts (SQLite): precautions for applications that run untrusted SQL or open database files of uncertain origin.
  • 7 Pragma statements supported by SQLite (SQLite): the settings for each connection and database, such as foreign_keys, journal_mode, synchronous, busy_timeout, user_version, optimize and integrity_check.
  • 7 Quirks, Caveats, and Gotchas In SQLite (SQLite): where SQLite differs from other SQL databases, such as flexible typing, foreign keys being off by default, and primary keys that can contain NULLs.
  • 7 Write-Ahead Logging (SQLite): how WAL mode works, its advantages and limits, checkpoints and WAL file growth.
  • 7 Best practices for SQLite performance (Android Developers): WAL, synchronous mode, indexes, transactions and connection handling for SQLite on Android.
  • 6 SQLite As An Application File Format (SQLite): why an SQLite database can replace custom document or save file formats, and its security considerations.

Key rules

Choosing and placing SQLite

Connections and concurrency

Schema and queries

Backups and file handling

  • 8 SQLITE-11 Copy a live database only with the online backup API, VACUUM INTO or sqlite3_rsync, never copy the database file while a connection may be writing to it, and never delete, rename or separate it from its -wal or -journal file. A file copied during a transaction can mix old and new content, and a database separated from its hot journal can't recover after a crash. For files that users copy or a cloud service syncs, such as game saves (STEAM-9), sync a copy made this way, or close every connection first: the last connection to close checkpoints and deletes the WAL file. (SQLite, How To Corrupt: backup or restore while a transaction is active, deleting a hot journal, SQLite Backup API, VACUUM INTO, sqlite3_rsync, Write-Ahead Logging: the WAL file)
  • 6 SQLITE-12 Check backups and saves with PRAGMA integrity_check, or the faster quick_check, before you rely on them, and keep the previous good copy until the new one passes. This is how to apply GAMEDEV-11 and the restore tests of DATA-8 in SQLite. integrity_check doesn't find foreign key violations; use PRAGMA foreign_key_check for those. (SQLite, PRAGMA integrity_check, PRAGMA quick_check)

Untrusted databases and SQL

How to enforce

Tools that check the key rules above automatically, in CI, the IDE or a commit hook. "Partly" means the tool covers only some of what the rule asks.

Tutorials and further reading

  • 6 Query Planning (SQLite): how SQLite uses indexes to find and sort rows, and how to design indexes for your queries.
  • 6 Persist data with SQLite (Flutter cookbook): opening a database, creating a table, and inserting, querying, updating and deleting rows with bound parameters from a Flutter app.
  • 5 Atomic Commit In SQLite (SQLite): how SQLite makes transactions atomic across crashes and power failures, and what can go wrong.
  • 5 Persistent Storage Options (SQLite Wasm): keeping SQLite databases in the browser with the Origin-Private File System (OPFS), and choosing between its storage back ends.