# SQLite: database table is locked (SQLITE_LOCKED)

**Code:** `table is locked` · **Category:** SQL · **Seen on:** SQLite, Python

A statement on the same connection (or shared cache) conflicts with another statement that still has an open cursor on that table.

## Meaning

Unlike "database is locked" (SQLITE_BUSY, another connection), SQLITE_LOCKED is about conflicts within one connection or shared-cache mode: you are reading a table while dropping/altering it, or finalising nothing. Finish and close open cursors first, avoid shared cache, and don't modify a table while iterating it with the same connection.

## Common causes

- Modifying a table while a SELECT cursor is still open
- DROP TABLE/ALTER with an unfinished statement
- Shared-cache mode conflicts
- Unreset prepared statements
- Multiple threads sharing one connection

## Quick fix

1. Fetch all rows (or close the cursor) before changing the table
2. Reset/finalize prepared statements
3. Avoid shared-cache mode

## Detailed fix by platform

### Python

1. 

```python
rows = cur.execute("SELECT id FROM t").fetchall()   # close the read cursor first
for (i,) in rows: conn.execute("UPDATE t SET x=1 WHERE id=?", (i,))
```


## How to diagnose

1. **Cursor** — Still open?
2. **Statement** — Finalised?
3. **Shared cache** — On?

---
Source: [WhatIsMyError](https://whatismyerror.com/errors/sql/sqlite-database-table-is-locked)
