Skip to main content

Async database support for Python.

Project description

Databases

Test Suite Package version

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/

Requirements: Python 3.7+


Installation

$ pip install databases

Database drivers supported are:

You can install the required database drivers with:

$ pip install databases[asyncpg]
$ pip install databases[aiopg]
$ pip install databases[aiomysql]
$ pip install databases[asyncmy]
$ pip install databases[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[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.

# Create a database instance, and connect to it.
from databases import Database
database = Database('sqlite+aiosqlite:///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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

databases-0.8.0.tar.gz (27.6 kB view details)

Uploaded Source

Built Distribution

databases-0.8.0-py3-none-any.whl (22.6 kB view details)

Uploaded Python 3

File details

Details for the file databases-0.8.0.tar.gz.

File metadata

  • Download URL: databases-0.8.0.tar.gz
  • Upload date:
  • Size: 27.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.7.17

File hashes

Hashes for databases-0.8.0.tar.gz
Algorithm Hash digest
SHA256 6544d82e9926f233d694ec29cd018403444c7fb6e863af881a8304d1ff5cfb90
MD5 feb6fe1fe92cae689b5cb2b643158752
BLAKE2b-256 7e73a8e49fa9ae156249e86474a4dc461a95e6e389dc0f139ff4c798a5130e8d

See more details on using hashes here.

File details

Details for the file databases-0.8.0-py3-none-any.whl.

File metadata

  • Download URL: databases-0.8.0-py3-none-any.whl
  • Upload date:
  • Size: 22.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.7.17

File hashes

Hashes for databases-0.8.0-py3-none-any.whl
Algorithm Hash digest
SHA256 0ceb7fd5c740d846e1f4f58c0256d780a6786841ec8e624a21f1eb1b51a9093d
MD5 6a230f9b15449c5d602926813f906a67
BLAKE2b-256 0918af04845ee14bf9183a7403e109ce7ac9468403bf0b8f8ff4802cc8bf0d79

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page