/errors/sqlite/database-is-locked
SQLite: database is locked (SQLITE_BUSY)
verified onenvironmentlocal · windows 10.0.26200 arm64 · GNU bash 5.3.15 (Git Bash)updated 2026-10-20
- tool
- SQLite
- exit code
- 0
- versions
- SQLite 3
- os
- any
SQLite allows many readers but only one writer at a time per database file. When a connection wants to write while another connection holds the write lock, SQLite returns SQLITE_BUSY, which drivers report as database is locked. With a busy timeout the second connection waits and retries; with a timeout of zero it fails at once.
Reproduce it
Two connections from Python's sqlite3 module; the first holds a write transaction, the second has timeout=0:
python - <<'EOF'
import sqlite3
print("SQLite", sqlite3.sqlite_version)
a = sqlite3.connect("app.db", timeout=0)
a.execute("create table jobs (id integer primary key, state text)")
a.commit()
a.execute("begin immediate")
a.execute("insert into jobs (state) values ('new')")
b = sqlite3.connect("app.db", timeout=0)
try:
b.execute("insert into jobs (state) values ('other')")
except sqlite3.OperationalError as e:
print(f"{type(e).__name__}: {e} ({e.sqlite_errorname})")
a.commit()
b.execute("insert into jobs (state) values ('other')") # works once the lock is released
b.commit()
print(b.execute("select count(*) from jobs").fetchone()[0], "rows")
EOFOutput
SQLite 3.53.1 OperationalError: database is locked (SQLITE_BUSY) 2 rows
Fixes
conn = sqlite3.connect("app.db", timeout=5) # wait up to 5 s for the lock (Python's default is 5)
conn.execute("PRAGMA journal_mode=WAL") # readers and one writer at the same time
with conn: # commits, or rolls back on an exception
conn.execute("insert into jobs (state) values (?)", ("new",))Other drivers: PRAGMA busy_timeout = 5000; works in any connection, and most ORMs expose it as a connection option.
Sources
Related pages
config · sqliteSQLite PRAGMA journal_mode=WAL: concurrent readers and a writerPRAGMA journal_mode=WAL makes SQLite write changes to a -wal file, so readers keep working while one connection writes. It is stored in the database file.compat · sqliteSQLite feature versions: RETURNING, STRICT, ->> and DROP COLUMNWhich SQLite version added what: DROP COLUMN 3.35, RETURNING 3.35, STRICT tables 3.37, the -> and ->> JSON operators 3.38, unixepoch() 3.38, concat_ws() 3.44.glossary · sqlWhat is a database index?A database index is a sorted structure, usually a B-tree, that finds rows by column value without reading the whole table, at the cost of slower writes.