Asynchronous SQLite client with AnyIO

0.2.8 · active · verified Thu Apr 16

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.

Common errors

Warnings

Install

Imports

Quickstart

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.

import anyio
from sqlite_anyio import Connection
import os

async def main():
    db_path = "test.db"
    async with Connection(db_path) as conn:
        await conn.execute("CREATE TABLE IF NOT EXISTS users (id INTEGER, name TEXT)")
        await conn.execute("INSERT INTO users VALUES (?, ?)", 1, "Alice")
        await conn.execute("INSERT INTO users VALUES (?, ?)", 2, "Bob")

        async with conn.cursor() as cur:
            await cur.execute("SELECT id, name FROM users")
            rows = await cur.fetchall()
            print(f"All users: {rows}")

            await cur.execute("SELECT id, name FROM users WHERE id = ?", 1)
            row = await cur.fetchone()
            print(f"User with ID 1: {row}")

    # Clean up the database file
    if os.path.exists(db_path):
        os.remove(db_path)

if __name__ == "__main__":
    anyio.run(main)

view raw JSON →