sqlite3.OperationalError: Database is Locked

sqlite3.OperationalError: database is locked

Quick Answer

Another connection has a write lock on the SQLite database. Use WAL mode, close other connections, or switch to a client-server database.

Why This Happens

SQLite uses file-level locking for writes. When one connection is writing, all others are blocked. Long transactions and unclosed connections cause this.

The Problem

import sqlite3
conn1 = sqlite3.connect('app.db')
conn1.execute('BEGIN EXCLUSIVE')
conn2 = sqlite3.connect('app.db')
conn2.execute('INSERT INTO t VALUES (1)')

The Fix

import sqlite3
conn = sqlite3.connect('app.db', timeout=10)
conn.execute('PRAGMA journal_mode=WAL')

with sqlite3.connect('app.db') as conn:
    conn.execute('INSERT INTO t VALUES (?)', (1,))

Step-by-Step Fix

  1. 1

    Enable WAL mode

    Run PRAGMA journal_mode=WAL for better concurrency.

  2. 2

    Use context managers

    Use 'with conn:' for prompt commits.

  3. 3

    Set a timeout

    Use timeout=10 to wait for locks instead of failing.

Bugsly catches this automatically

Bugsly's AI analyzes this error pattern in real-time, explains what went wrong in plain English, and suggests the exact fix — before your users even report it.

Try Bugsly free