SQLITE_LOCKED: table is locked
This error occurs when a table-level lock prevents your operation. Different from SQLITE_BUSY, this is about internal table locks within a connection.
The table is locked error (SQLITE_LOCKED) is different from database is locked (SQLITE_BUSY). It's about table-level locks within SQLite.
Understanding the Error
SQLITE_LOCKED: database table is locked
This usually happens within a single connection when operations conflict.
Common Causes
1. Reading While Writing Same Table
JAVASCRIPT
2. Multiple Statements on Same Table
JAVASCRIPT
3. Recursive Triggers
SQL
4. Shared Cache Mode Conflicts
When using shared cache, connections can lock each other's tables.
How to Fix It
Solution 1: Collect Then Modify
JAVASCRIPT
Solution 2: Use Transactions
JAVASCRIPT
Solution 3: Finalize Statements
JAVASCRIPT
Solution 4: Use Single Statement
SQL
Solution 5: Disable Shared Cache
JAVASCRIPT
Best Practices
- Finish reading before writing to same table
- Use
.all()instead of iterating with active cursor - Use single SQL statements when possible
- Avoid shared cache unless specifically needed
- Keep transactions short