Skip to main content

anyio bridge to the standard sqlite3 module

Project description

anyio-sqlite

Python versions PyPI release License

anyio-sqlite is an AnyIO bridge to the standard sqlite3 database module, providing async versions of all connection, cursor and blob (Python 3.11+) methods that runs on both asyncio and Trio. Asynchronous context management is also supported.

Install

anyio-sqlite is compatible with Python 3.9 and newer. Install it from PyPI:

pip install anyio-sqlite

Examples

Use it the same way as the standard sqlite3 module, simply scatter the await keyword when needed:

import anyio
import anyio_sqlite

async def main():
    async with anyio_sqlite.connect("example.sqlite3") as con:
        async with await con.cursor() as cur:
            await cur.execute("CREATE TABLE movie(title, year, score)")
            await cur.execute("""
                INSERT INTO movie VALUES
                    ('Monty Python and the Holy Grail', 1975, 8.2),
                    ('And Now for Something Completely Different', 1971, 7.5)
            """)
            await con.commit()

        async with await con.execute("SELECT score FROM movie") as cur:
            async for row in cur:
                ...

anyio.run(main)

It can also be used mostly procedurally, if you provide an external task group:

import anyio
import anyio_sqlite

async def main():
    async with anyio.create_task_group() as tg:
        con = await anyio_sqlite.Connection.connect(tg, "example.sqlite3")
        cur = await con.cursor()

        await cur.execute("CREATE TABLE movie(title, year, score)")
        await cur.execute("""
            INSERT INTO movie VALUES
                ('Monty Python and the Holy Grail', 1975, 8.2),
                ('And Now for Something Completely Different', 1971, 7.5)
        """)
        await con.commit()
        await cur.aclose()

        cur = await con.execute("SELECT score FROM movie")

        async for row in cur:
            ...

        await cur.aclose()
        await con.aclose()

anyio.run(main)

[!WARNING] Since each connection spawns a long-running worker in another thread, the connection must be closed after you're done using it! Otherwise, the process will hang indefinitely waiting for the worker thread to finish.

License

Licensed under either of

at your option.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

anyio_sqlite-0.2.0.tar.gz (16.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

anyio_sqlite-0.2.0-py3-none-any.whl (18.9 kB view details)

Uploaded Python 3

File details

Details for the file anyio_sqlite-0.2.0.tar.gz.

File metadata

  • Download URL: anyio_sqlite-0.2.0.tar.gz
  • Upload date:
  • Size: 16.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.8.17

File hashes

Hashes for anyio_sqlite-0.2.0.tar.gz
Algorithm Hash digest
SHA256 e9eb09c1c99672c75492f126e28c5c45f8ec426a6c737f52e94e0f5fb9de4d3b
MD5 9bb17ae03816c4cea2cb71fc9cc08b54
BLAKE2b-256 a21c7feeca172818e4608d19e2f2ab7385a6c54c7187f655021a789c1d5436ae

See more details on using hashes here.

File details

Details for the file anyio_sqlite-0.2.0-py3-none-any.whl.

File metadata

File hashes

Hashes for anyio_sqlite-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 b98917442ece3696bb2a8882393e1d9cc9c588d9ca8d48bf546b8a088567245a
MD5 1de60524bdfff8bfc8aa5620f3df7341
BLAKE2b-256 1c98965956fecc9f57c31e74e01f1baaff4671b294535b1541f13c29ae22481e

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page