SQLite Guide: Setup, Queries, WAL, and Backups
SQLite is an embedded relational database engine. The application reads and writes a database file directly, without connecting to a separate database server.
For query syntax, start with the SQL guide. The database engine guides explain how SQLite compares with the other options.
Choose SQLite when the data belongs to one application or device and writes can take turns. A single file still needs backups, migrations, and a restore plan.
When Should You Use SQLite?
SQLite fits data that belongs close to one application or device. It can replace a custom JSON, XML, or binary project file while adding SQL queries, constraints, transactions, and indexes.
SQLite is a strong fit for these uses:
- Mobile and desktop application state.
- Command-line history, caches, and searchable local data.
- Test databases that need no server provisioning.
- Embedded devices and application file formats.
- Small websites on one host with short, modest writes.
A client/server database fits better when data lives across a network, several hosts need to write concurrently, or database roles and central operations are part of the requirement. Compare PostgreSQL and MySQL before adding workarounds for a deployment that has outgrown one file.
How Do You Open and Query a SQLite Database?
Open a SQLite database with sqlite3 app.db, then run SQL in the shell. If the file does not exist, SQLite creates it when you first write to it. Install the command-line tools if sqlite3 is unavailable; the library bundled with a language runtime may be a different version.
sqlite3 --version
sqlite3 app.db Inside the shell, inspect the file before changing it:
.databases
.tables
.schema
.headers on
.mode column
SELECT sqlite_version(); The dot-prefixed lines are commands understood by the sqlite3 shell, while SELECT sqlite_version(); is the SQL statement sent to the database.
Create a small notes table with required fields and a stable sort key:
CREATE TABLE notes (
id INTEGER PRIMARY KEY,
title TEXT NOT NULL,
body TEXT NOT NULL,
created_at TEXT NOT NULL DEFAULT CURRENT_TIMESTAMP
);
CREATE INDEX notes_created_idx
ON notes (created_at DESC, id DESC);
INSERT INTO notes (title, body)
VALUES ('Backup check', 'Restore the notebook before trusting the copy.');
SELECT id, title, created_at
FROM notes
ORDER BY created_at DESC, id DESC; INTEGER PRIMARY KEY identifies the row using SQLite's integer row identifier. The composite index matches the sort order and uses id to break timestamp ties. The same CLI can inspect a backup or reproduce a migration without first writing application code.
How Does SQLite Handle Types and Foreign Keys?
SQLite normally uses flexible typing and requires foreign-key enforcement to be enabled on each connection. A column can store a value that does not match its declared type unless stricter checks apply. Use a STRICT table when you need stricter type checks. SQLite has no dedicated date type; the notes example stores its timestamp as text.
Enable foreign-key enforcement on each connection before starting a transaction with PRAGMA foreign_keys = ON;. Declaring a foreign key alone is not enough when enforcement is disabled. The schema design guide explains the constraints that keep related rows consistent.
Keep the sqlite3 app.db shell open for the WAL settings below. Use .quit to leave it before running the later backup commands in your terminal.
How Does SQLite WAL Mode Change Concurrency?
SQLite WAL mode lets readers and one writer work at the same time in common cases. It appends writes to a separate log before checkpointing them into the database file; it still allows only one writer at a time.
PRAGMA journal_mode = WAL;
PRAGMA busy_timeout = 5000;
PRAGMA journal_mode;
PRAGMA wal_checkpoint; WAL lets readers continue while a writer is active in common cases, which can improve a read-heavy local application. It does not permit several simultaneous writers. SQLite still serializes writes, so long transactions make other writers wait.
The main database file and the WAL state belong together until a checkpoint integrates the changes. All processes using a WAL database must run on the same host because the wal-index relies on shared memory. Do not place the database on a network filesystem to let several application servers write it.
Automatic checkpoints handle many workloads. If the WAL keeps growing, check for long-running readers that prevent a checkpoint from finishing.
How Do You Back Up and Migrate SQLite?
Back up a live SQLite database with .backup or the backup API, and apply schema migrations in a transaction where the operation supports it. The command-line shell's .backup command uses the backup API rather than treating the active database as an ordinary file.
sqlite3 app.db ".backup 'app-backup.db'"
sqlite3 app-backup.db "PRAGMA integrity_check;" An integrity check is useful, but a restore test must also open the application schema, run representative queries, and confirm that the backup contains the expected rows. Keep a protected copy away from the device or host it protects.
Reopen sqlite3 app.db for the migration below. Track schema changes in migrations and apply them inside transactions where SQLite supports the complete operation:
BEGIN IMMEDIATE;
ALTER TABLE notes
ADD COLUMN archived_at TEXT;
CREATE TABLE schema_migrations (
version TEXT PRIMARY KEY,
applied_at TEXT NOT NULL DEFAULT CURRENT_TIMESTAMP
);
INSERT INTO schema_migrations (version)
VALUES ('2026-07-29-add-archived-at');
COMMIT; BEGIN IMMEDIATE acquires the write transaction before migration work begins, so a lock conflict appears before partial work. Rehearse table-rebuild migrations on a realistic backup because some schema changes require a new table, copied rows, and a controlled rename.
How Did SQLite Begin?
D. Richard Hipp originally developed and released SQLite in 2000. The official release chronology starts with SQLite 1.0 on August 17, 2000. SQLite 3.0 arrived in 2004 with the modern file format and support for binary data plus Unicode.
The project publishes its source and documentation in the public domain. Its self-contained library and stable file format helped it spread through phones, desktop software, browsers, devices, command-line tools, and test environments.
That history explains why SQLite appears inside applications instead of beside them as another service. Whether SQLite suits an application depends on how many writes it makes and where its data must be shared.
Common Pitfalls
Sharing the File over a Network Filesystem
WAL mode fails or database access becomes unreliable across hosts. Several machines are treating one file as a network database. Keep SQLite and every accessing process on one host, or move shared network data to a client/server database.
Holding Write Transactions Open
Other writes return busy errors or wait for noticeable periods. Application code performs slow work while a write transaction remains open. Prepare external work first, keep database writes short, commit promptly, and set a measured busy timeout.
Copying Only the Main File During a Write
A copied database is missing recent changes or fails verification. A file copy ignored active journal or WAL state. Use the online backup API, the shell's .backup command, or another documented SQLite-aware method, then test the copy.
Treating One File as No Operations
An application update loses data or cannot restore an older schema. The single-file model was mistaken for a system without backups or migrations. Document the file location, migration ledger, backup schedule, restore command, retention, and recovery owner.
For related decisions, read MySQL vs PostgreSQL vs SQLite.
Frequently Asked Questions
Is SQLite a relational database?
SQLite is a relational database with transactional SQL support. It implements tables, indexes, triggers, views, constraints, and joins. The engine is embedded in the application process instead of running as a separate server.
Can SQLite run a website?
SQLite can run a website when one application host, modest write concurrency, and a local database file match the workload. A website that needs several writing hosts, database-level user administration, replicas, or heavy concurrent writes usually fits a client/server database better.
Does SQLite WAL mode allow multiple writers?
SQLite WAL mode allows only one writer at a time. It lets readers continue while a writer appends changes. Keep write transactions short, set a suitable busy timeout, and measure lock waits. WAL also requires all processes using the database to remain on the same host.
When should an application move from SQLite?
Move an application from SQLite to a server database when sustained concurrent writes cause unacceptable waits, several hosts need direct database access, or you need built-in database users and replication. Database size alone is not a reason to move.
Sources
-
[1]
SQLite Foreign Key Support(sqlite.org)
-
[2]
SQLite STRICT Tables(sqlite.org)
-
[3]
About SQLite(sqlite.org)
-
[4]
History of SQLite Releases(sqlite.org)
-
[5]
Appropriate Uses for SQLite(sqlite.org)
-
[6]
Command-Line Shell for SQLite(sqlite.org)
-
[7]
Write-Ahead Logging(sqlite.org)
-
[8]
SQLite Backup API(sqlite.org)
Read Next
Compare MySQL, PostgreSQL, and SQLite by deployment, write concurrency, data rules, JSON, search, and recovery needs.
Learn where PostgreSQL fits, how roles and schemas separate access, how JSONB works with indexes, and how to plan backups and major upgrades.
Learn where MySQL fits, how to create a restricted application user, how InnoDB transactions work, and how to plan backups and upgrades.
Guides to the common SQL database engines: MySQL, PostgreSQL, SQLite, and MariaDB, and when to reach for each.