https://github.com/merlinz01/anysqlite3
An async wrapper for the sqlite3 module compatible with both asyncio and trio.
https://github.com/merlinz01/anysqlite3
anyio asyncio python sqlite trio
Last synced: about 2 months ago
JSON representation
An async wrapper for the sqlite3 module compatible with both asyncio and trio.
- Host: GitHub
- URL: https://github.com/merlinz01/anysqlite3
- Owner: merlinz01
- License: mit
- Created: 2024-12-25T00:42:16.000Z (10 months ago)
- Default Branch: main
- Last Pushed: 2024-12-25T00:46:45.000Z (10 months ago)
- Last Synced: 2025-04-09T22:15:31.513Z (6 months ago)
- Topics: anyio, asyncio, python, sqlite, trio
- Language: Python
- Homepage:
- Size: 18.6 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE.txt
Awesome Lists containing this project
README
# anysqlite3
**anysqlite3** is a wrapper around `sqlite3` that allows you to interact with SQLite databases from async code.
It is built on top of the built-in `sqlite3` module and is compatible with both `asyncio` and `trio` code.## Installation
```bash
pip install anysqlite3
```## Usage
**anysqlite3** provides essentially the same API as the built-in `sqlite3` module, but with async versions of most methods.
```python
import anyio # or asyncio or trio
import anysqlite3async def main():
async with await anysqlite3.connect(":memory:") as db:
await db.execute("CREATE TABLE users (id INTEGER PRIMARY KEY, name TEXT)")
await db.execute("INSERT INTO users (name) VALUES (?)", ("Alice",))
await db.execute("INSERT INTO users (name) VALUES (?)", ("Bob",))async for row in db.execute("SELECT * FROM users"):
print(row)anyio.run(main())
```### Transactions
**anysqlite3** provides a context manager for transactions.
Use this instead of the database's `commit` and `rollback` methods.
Transactions hold a lock on the database, so you should always use them in a `with` block.```python
async with db.transaction() as t:
await db.execute("INSERT INTO users (name) VALUES (?)", ("Charlie",))
await t.rollback()
await db.execute("INSERT INTO users (name) VALUES (?)", ("David",))
await t.commit() # Optional, as the transaction will commit automatically the with block exits without an exception
```## License
This project is licensed under the MIT License - see [LICENSE.txt](LICENSE.txt) for details.