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.1.tar.gz (16.4 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.1-py3-none-any.whl (19.0 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for anyio_sqlite-0.2.1.tar.gz
Algorithm Hash digest
SHA256 5ca1de10e8ae930adaa81212d9a88f590c11797d741ca214edb1546ac65f8f4c
MD5 c1cd1b837c9e69bac89a09fc3628b9e5
BLAKE2b-256 45802dbff27d6acaa5c2ccdf8eefbee80ab084cfc310bc804e33855e65b71b1f

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for anyio_sqlite-0.2.1-py3-none-any.whl
Algorithm Hash digest
SHA256 cb071d99a53f13fce8b72a215a3b9012b761b979d70cf62124538cb80efe54a7
MD5 9e47932c954cbb98a3bc031fe7335e2d
BLAKE2b-256 ca7edacdba897b55109d0f55e618282c046d16673e45e50918c4bbe37370f140

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