Anysqlite provides an async/await interface to the standard `sqlite3` library, supporting both Trio and Asyncio backends using the `Anyio` library. It is currently at version 0.0.5 and has an irregular or slow release cadence, with the last update in October 2023.
Install & Compatibility
Where this runs
tested against v0.0.5 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
muslpy 3.10–3.925 runs
installs and imports cleanly · install 0.0s · import 0.298s · 20.1MB
glibcpy 3.10–3.925 runs
installs and imports cleanly · install 1.8s · import 0.254s · 21MB
18MB installed
● package 18MB
Code
Verified usage
Verified import paths — ran on the pinned version, not inferred.
anysqlite
✓ import anysqlite
The primary library module providing the `connect` function and other database operations.
This quickstart demonstrates how to establish an asynchronous connection to an SQLite database (in-memory in this example), execute SQL commands, insert data, and fetch results using `anysqlite`.
import asyncio
import anysqlite
async def main():
# Connect to an in-memory database for quick testing
# Or use a file path like "my_database.db"
conn = await anysqlite.connect(":memory:")
try:
# Execute a simple query
cursor = await conn.execute("SELECT DATETIME('now')")
response = await cursor.fetchone()
print(f"Current datetime from SQLite: {response[0]}")
# Create a table and insert data
await conn.execute("CREATE TABLE IF NOT EXISTS users (id INTEGER PRIMARY KEY, name TEXT)")
await conn.execute("INSERT INTO users (name) VALUES (?)", ("Alice",))
await conn.execute("INSERT INTO users (name) VALUES (?)", ("Bob",))
await conn.commit()
# Fetch data
cursor = await conn.execute("SELECT id, name FROM users")
users = await cursor.fetchall()
print("Users in database:")
for user_id, user_name in users:
print(f" ID: {user_id}, Name: {user_name}")
finally:
# Ensure the connection is closed
await conn.close()
if __name__ == "__main__":
asyncio.run(main())
Errors
Common errors & fixes
ModuleNotFoundError: No module named 'anysqlite'
The 'anysqlite' package is not installed in the Python environment.
fixInstall the package using 'pip install anysqlite'.
AttributeError: 'NoneType' object has no attribute 'sqlite_db'
Attempting to access 'sqlite_db' on a 'NoneType' object, possibly due to an uninitialized or improperly configured database connection.
fixEnsure the database connection is properly initialized before accessing 'sqlite_db'.
AttributeError: can't set attribute
Attempting to modify an immutable property or misconfigured relationship in a SQLAlchemy model.
fixVerify column properties and avoid modifying immutable attributes directly.
AttributeError: 'ConexionDB' object has no attribute 'execute'
The 'ConexionDB' class lacks an 'execute' method, possibly due to incorrect implementation or missing inheritance.
fixImplement the 'execute' method in the 'ConexionDB' class or ensure it inherits from a class that provides this method.
AttributeError: type object 'Cliente' has no attribute 'query'
The 'Cliente' class is missing the 'query' attribute, likely due to improper setup with SQLAlchemy.
fixEnsure the 'Cliente' class is correctly defined as a SQLAlchemy model and the database session is properly configured.
Audit
Dependencies
anyiorequiredProvides the async/await backend support (asyncio or trio) required for anysqlite's asynchronous operations. Version >=3.4.0 is required.