The Wayback Machine - https://web.archive.org/web/20200603104823/https://github.com/encode/databases
Skip to content
Async database support for Python. πŸ—„
Python Shell
Branch: master
Clone or download

Latest commit

brianpkennedy and brian.kennedy Access asyncpg Record field by key on raw query (#207)
Co-authored-by: brian.kennedy <brian.kennedy@MBP-BKennedy.local>
Latest commit 45519d7 May 12, 2020

Files

Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
.github/workflows Update test-suite.yml May 4, 2020
databases Access asyncpg Record field by key on raw query (#207) May 12, 2020
docs Update index.md Apr 25, 2020
scripts Update publish (#202) Apr 30, 2020
tests Use backend native fetch_val() implementation when available (#132) Apr 30, 2020
.gitignore Version 0.1.8 Feb 25, 2019
LICENSE.md Add LICENSE.md Feb 10, 2019
README.md Update README.md Apr 25, 2020
mkdocs.yml Documentation Jun 25, 2019
requirements.txt Adjustments for `requirements.txt` and `setup.py` Jul 12, 2019
setup.py Replace psycopg2-binary with psycopg2 (#198) (#204) May 1, 2020

README.md

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/

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 providing using one of asyncpg, aiomysql, or aiosqlite.


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.

You can’t perform that action at this time.