MySQL Context Manager
Work with MySQL based databases asynchronously, using a context manager.
Getting started
You can get mysql-context-manager
from PyPI,
which means you can install it with pip easily:
python -m pip install mysql-context-manager
Example
from mysql_context_manager import MysqlConnector
async with MysqlConnector(hostname="localhost") as conn:
results = await conn.query("select username from users where is_bender = 1 order by username asc;")
assert results[0]["username"] == "Aang"
assert results[1]["username"] == "Katara"
assert results[2]["username"] == "Toph"
Example using SQLAlchemy
from mysql_context_manager import MysqlConnector
import sqlalchemy
from sqlalchemy.dialects import mysql
metadata = sqlalchemy.MetaData()
users = sqlalchemy.Table(
"users",
metadata,
sqlalchemy.Column("user_id", mysql.INTEGER(), autoincrement=True, nullable=False),
sqlalchemy.Column("username", mysql.VARCHAR(length=128), nullable=False),
sqlalchemy.Column("is_bender", mysql.SMALLINT(), autoincrement=False, nullable=True),
sqlalchemy.PrimaryKeyConstraint("user_id"),
mysql_default_charset="utf8mb4",
mysql_engine="InnoDB",
)
async with MysqlConnector(hostname="localhost") as conn:
results = await conn.query(users.select().where(users.c.username == "Aang"))
assert results[0]["username"] == "Aang"
assert results[0]["is_bender"] == 1
Changelog
Refer to the CHANGELOG.rst file.