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.3.0.tar.gz (16.8 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.3.0-py3-none-any.whl (19.9 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for anyio_sqlite-0.3.0.tar.gz
Algorithm Hash digest
SHA256 e7723e2eb43cdf2be5e02f2db2fe5379523c0af1a3b545b6ef9082faf268d00c
MD5 74ec619e465aad36d4f4e93274d43571
BLAKE2b-256 7b0a7ea29a837f877d8473f4f8a116bd5528fa0034c01cd8fb6d548fdf656f75

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for anyio_sqlite-0.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 532a2f6103972d13e65a1f54737df3c95d96f38d9e0800669dcf858a9010e4c3
MD5 67003c7061979e7e7827d680ee6cd4a5
BLAKE2b-256 c87bcb4da545e2f550480c9764f9e9122cf2139a911f002ab09d002d29df522f

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