SQLite with FastAPI/Flask: 'SQLite objects created in a thread can only be used in that same thread'
@devsingh05▲ 10 net votes
Python's sqlite3 refuses cross-thread use by default, and web frameworks serve requests on worker threads. With SQLAlchemy: create_engine('sqlite:///app.db', connect_args={'check_same_thread': False}), and give each request its own session. With raw sqlite3: sqlite3.connect(path, check_same_thread=False) plus your own locking, or open a connection per request. Concurrent writes also need WAL mode or retries to avoid 'database is locked'.