Async database support for Python.
Project description
Databases
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.
Documentation: https://www.encode.io/databases/
Community: https://discuss.encode.io/c/databases
Requirements: Python 3.6+
Installation
$ pip install databases
You can install the required database drivers with:
$ pip install databases[postgresql]
$ pip install databases[mysql]
$ pip install databases[sqlite]
Driver support is provided using one of asyncpg, aiomysql, or 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[sqlite]
$ 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.
# Create a database instance, and connect to it.
from databases import Database
database = Database('sqlite:///example.db')
await database.connect()
# Create a table.
query = """CREATE TABLE HighScores (id INTEGER PRIMARY KEY, name VARCHAR(100), score INTEGER)"""
await database.execute(query=query)
# Insert some data.
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)
# Run a database query.
query = "SELECT * FROM HighScores"
rows = await database.fetch_all(query=query)
print('High Scores:', rows)
Check out the documentation on making database queries for examples of how to start using databases together with SQLAlchemy core expressions.
— ⭐️ —
Databases is BSD licensed code. Designed & built in Brighton, England.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file databases-0.4.3.tar.gz
.
File metadata
- Download URL: databases-0.4.3.tar.gz
- Upload date:
- Size: 14.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.4.1 importlib_metadata/3.7.3 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.59.0 CPython/3.7.10
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 1521db7f6d3c581ff81b3552e130b27a13aefea2a57295e65738081831137afc |
|
MD5 | 60ee40222089017a799c06e743e0ab8f |
|
BLAKE2b-256 | 77db07bf9557c383abc68bf53d642b187e99e7b276a016c1d3e1d2e4cf2cb33d |
File details
Details for the file databases-0.4.3-py3-none-any.whl
.
File metadata
- Download URL: databases-0.4.3-py3-none-any.whl
- Upload date:
- Size: 19.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.4.1 importlib_metadata/3.7.3 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.59.0 CPython/3.7.10
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | f82b02c28fdddf7ffe7ee1945f5abef44d687ba97b9a1c81492c7f035d4c90e6 |
|
MD5 | 082b1b32de65d37da27a60a74f8d45eb |
|
BLAKE2b-256 | 564b0637280d43e302f2b6c635f7988c067c5a45eca57523655d73fe6463ab38 |