{"id":8680,"library":"sqlite-anyio","title":"Asynchronous SQLite client with AnyIO","description":"sqlite-anyio is an asynchronous client for SQLite databases, built on top of the AnyIO library. It provides an `async`/`await` interface for interacting with SQLite, enabling non-blocking database operations within asynchronous Python applications. The current version is 0.2.8, and it maintains a frequent release cadence, often introducing minor features or bug fixes.","status":"active","version":"0.2.8","language":"en","source_language":"en","source_url":"https://github.com/davidbrochart/sqlite-anyio","tags":["sqlite","anyio","async","database","client"],"install":[{"cmd":"pip install sqlite-anyio","lang":"bash","label":"Install stable version"}],"dependencies":[{"reason":"Core asynchronous backend for non-blocking I/O operations.","package":"anyio","optional":false}],"imports":[{"symbol":"Connection","correct":"from sqlite_anyio import Connection"}],"quickstart":{"code":"import anyio\nfrom sqlite_anyio import Connection\nimport os\n\nasync def main():\n    db_path = \"test.db\"\n    async with Connection(db_path) as conn:\n        await conn.execute(\"CREATE TABLE IF NOT EXISTS users (id INTEGER, name TEXT)\")\n        await conn.execute(\"INSERT INTO users VALUES (?, ?)\", 1, \"Alice\")\n        await conn.execute(\"INSERT INTO users VALUES (?, ?)\", 2, \"Bob\")\n\n        async with conn.cursor() as cur:\n            await cur.execute(\"SELECT id, name FROM users\")\n            rows = await cur.fetchall()\n            print(f\"All users: {rows}\")\n\n            await cur.execute(\"SELECT id, name FROM users WHERE id = ?\", 1)\n            row = await cur.fetchone()\n            print(f\"User with ID 1: {row}\")\n\n    # Clean up the database file\n    if os.path.exists(db_path):\n        os.remove(db_path)\n\nif __name__ == \"__main__\":\n    anyio.run(main)","lang":"python","description":"This quickstart demonstrates opening an asynchronous connection to an SQLite database, creating a table, inserting data, and querying it using both `Connection.execute` and `Cursor` objects, all within an AnyIO runtime. It also shows proper resource management using async context managers."},"warnings":[{"fix":"If you previously relied on `Connection.execute()` to return a cursor for chaining operations, refactor your code to explicitly use `async with conn.cursor() as cur:` to obtain a cursor and then call `await cur.execute(...)`.","message":"The `Connection.execute()` method in version 0.2.8 and later now returns `self` (the Connection object) instead of a new Cursor object. Prior versions might have returned a Cursor or similar.","severity":"breaking","affected_versions":">=0.2.8"},{"fix":"Always wrap `Connection` and `Cursor` instantiation in `async with` statements to ensure proper resource management, e.g., `async with Connection(\"db.db\") as conn:` and `async with conn.cursor() as cur:`.","message":"Both `Connection` and `Cursor` objects are designed to be used as asynchronous context managers (`async with`). Failing to use them as such can lead to unclosed database connections or cursors, potentially causing resource leaks or database locking issues.","severity":"gotcha","affected_versions":">=0.2.1 (Connection), >=0.2.7 (Cursor)"}],"env_vars":null,"last_verified":"2026-04-16T00:00:00.000Z","next_check":"2026-07-15T00:00:00.000Z","problems":[{"fix":"Ensure all calls to `sqlite-anyio` methods are preceded by `await` within an `async` function. The top-level `async` function must be executed using `anyio.run()` (or `asyncio.run()` if using asyncio backend).","cause":"Attempting to call an asynchronous method (like `conn.execute` or `cur.fetchone`) without `await` inside an `async` function, or calling an `async` function (like `main`) directly from a synchronous context without an async event loop runner (e.g., `anyio.run`).","error":"RuntimeError: You must use `await` with an async function"},{"fix":"Design your application to minimize concurrent writes or use a queuing mechanism for write operations. Ensure that `Connection` and `Cursor` objects are always properly closed using `async with` context managers. Consider using WAL (Write-Ahead Logging) journal mode for better concurrency if the underlying SQLite setup supports it, though `sqlite-anyio` abstracts this.","cause":"SQLite is a file-based database that has limitations on concurrent write access. This error typically occurs when multiple processes, threads, or even multiple `sqlite-anyio` connections within the same application try to write to the database simultaneously, or if a previous connection was not properly closed.","error":"sqlite3.OperationalError: database is locked"},{"fix":"Add the correct import statement: `from sqlite_anyio import Connection` at the top of your Python file.","cause":"The `Connection` class was not correctly imported from the `sqlite_anyio` library.","error":"NameError: name 'Connection' is not defined"}]}