SQLITE_LOCKED: database table is locked
A write couldn't continue because of a conflict inside the same connection or shared cache, such as dropping a table a statement is still reading.
- Result code
SQLITE_LOCKED- Name
6- Category
- Primary result codes
What it means
SQLITE_LOCKED (6) means the conflict is within your own connection, or between connections sharing a cache, not with another process. The classic example from the SQLite documentation is a DROP TABLE while another statement on the same connection is still reading that table: dropping it would pull the table out from under the reader.
This is different from SQLITE_BUSY, which means another connection, usually in another process, holds a lock. Waiting doesn't fix SQLITE_LOCKED; finishing the other statement does.
Common causes
- Running
DROP TABLE,ALTER TABLEor similar while aSELECTon the same connection hasn't been stepped to the end or reset. - Writing to a table while iterating a query over it with an unfinished statement, in some cases.
- Shared-cache mode, where one connection modifying a table blocks others sharing the cache (
SQLITE_LOCKED_SHAREDCACHE).
How to fix it
-
Finish, reset or finalize every statement before changing the schema. In Python, fetch all rows or close the cursor first.
-
Read the rows you need into memory first, then run the DDL or the writes.
-- read first SELECT id FROM staging; -- then, after the reader is done: DROP TABLE staging;
-
Avoid shared-cache mode unless you need it. SQLite discourages it; WAL mode is the usual way to get concurrent readers.
Questions
Will a busy timeout fix SQLITE_LOCKED?
No. The busy handler is for conflicts with other connections (SQLITE_BUSY). SQLITE_LOCKED is a conflict inside your own connection or shared cache, so the fix is to finish or reset the statement that is in the way.
Related errors
More primary result codes errors
SQLITE_ERRORSQL logic errorSQLITE_PERMaccess permission deniedSQLITE_ABORTquery abortedSQLITE_NOMEMout of memorySQLITE_READONLYattempt to write a readonly databaseSQLITE_INTERRUPTinterruptedSQLITE_IOERRdisk I/O errorSQLITE_CORRUPTdatabase disk image is malformed
The code and message text are from the SQLite result and error codes documentation. Browse the full SQLite error list or search every database error.
Work on SQLite in SQLly
A native SQL client for macOS, Windows and Linux with schema-aware IntelliSense, guardrails for risky statements and clear error messages, for SQLite and the other databases you use.