YDB Python DBAPI which complies with PEP 249
Maintained. Last release 5 weeks ago, commits in 4 of the last 26 weeks. Weaker on commit activity and contributor spread.
pip install ydb-dbapiYDB Python DBAPI
Introduction
Python DBAPI to YDB, which provides both sync and async drivers and complies with PEP249.
Installation
pip install ydb-dbapi
Usage
To establish a new DBAPI connection you should provide host, port and database:
import ydb_dbapi
connection = ydb_dbapi.connect(
host="localhost", port="2136", database="/local"
) # sync connection
async_connection = await ydb_dbapi.async_connect(
host="localhost", port="2136", database="/local"
) # async connection
For TLS connections, pass PEM certificate contents as bytes through
root_certificates, or use root_certificates_path to load them from a file.
Shared session pools must match the connection API: ydb.QuerySessionPool
for connect() and ydb.aio.QuerySessionPool for async_connect().
Usage of connection:
with connection.cursor() as cursor:
cursor.execute("SELECT id, val FROM table")
row = cursor.fetchone()
rows = cursor.fetchmany(size=5)
rows = cursor.fetchall()
Usage of async connection:
async with async_connection.cursor() as cursor:
await cursor.execute("SELECT id, val FROM table")
row = await cursor.fetchone()
rows = await cursor.fetchmany(size=5)
rows = await cursor.fetchall()
Query parameters
Standard mode (pyformat=True)
Pass pyformat=True to connect() to enable familiar Python DB-API
parameter syntax such as %(name)s and %s. This mode is opt-in: the
default connection mode (pyformat=False) uses YDB-style $name
placeholders instead, so %(name)s and %s do not work unless
pyformat=True is set explicitly. The driver will convert placeholders
and infer YDB types from Python values automatically.
Named parameters — %(name)s with a dict:
connection = ydb_dbapi.connect(
host="localhost", port="2136", database="/local",
pyformat=True,
)
with connection.cursor() as cursor:
cursor.execute(
"SELECT * FROM users WHERE id = %(id)s AND active = %(active)s",
{"id": 42, "active": True},
)