Databases
This is a drop-in replacement for the original databases package
Databases gives you simple asyncio support for a range of databases.
It allows you to make queries using the powerful SQLAlchemy Core
expression language, and provides support for PostgreSQL, MySQL, and SQLite.
Databases is suitable for integrating against any async Web framework, such as Starlette,
Sanic, Responder, Quart, aiohttp, Tornado, or FastAPI.
Requirements: Python 3.8+
Installation
$ pip install databases-patched
Database drivers supported are:
You can install the required database drivers with:
$ pip install databases-patched[asyncpg]
$ pip install databases-patched[aiopg]
$ pip install databases-patched[aiomysql]
$ pip install databases-patched[asyncmy]
$ pip install databases-patched[aiosqlite]
Note that if you are using any synchronous SQLAlchemy functions such as engine.create_all() or alembic migrations then you still have to install a synchronous DB driver: psycopg2 for PostgreSQL and pymysql for MySQL.
Quickstart
For this example we'll create a very simple SQLite database to run some
queries against.
$ pip install databases-patched[aiosqlite]
$ pip install ipython
We can now run a simple example from the console.
Note that we want to use ipython here, because it supports using await
expressions directly from the console.
from databases_patched import Database
database = Database('sqlite+aiosqlite:///example.db')
await database.connect()
query = """CREATE TABLE HighScores (id INTEGER PRIMARY KEY, name VARCHAR(100), score INTEGER)"""
await database.execute(query=query)
query = "INSERT INTO HighScores(name, score) VALUES (:name, :score)"
values = [
{"name": "Daisy", "score": 92},
{"name": "Neil", "score": 87},
{"name": "Carol", "score": 43},
]
await database.execute_many(query=query, values=values)
query = "SELECT * FROM HighScores"
rows = await database.fetch_all(query=query)
print('High Scores:', rows)
for examples of how to start using databases together with SQLAlchemy core expressions.
— ⭐️ —
Databases is BSD licensed code. Designed & built in Brighton, England.