YDB Python DBAPI which complies with PEP 249
Project description
YDB 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
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},
)
Positional parameters — %s with a list or tuple:
with connection.cursor() as cursor:
cursor.execute(
"INSERT INTO users (id, name, score) VALUES (%s, %s, %s)",
[1, "Alice", 9.8],
)
Use %% to insert a literal % character in the query.
The driver validates pyformat placeholders before executing the query:
- do not mix named
%(name)sand positional%splaceholders in one query; - named placeholders require a
dictwith bare keys like{"id": 1}; - positional placeholders require a
listortuple; - missing or extra parameters raise
ProgrammingError; - keys starting with
$are not allowed inpyformat=Truemode.
Automatic type mapping:
| Python type | YDB type |
|---|---|
bool |
Bool |
int |
Int64 |
float |
Double |
str |
Utf8 |
bytes |
String |
datetime.datetime |
Timestamp |
datetime.date |
Date |
datetime.timedelta |
Interval |
decimal.Decimal |
Decimal(22, 9) |
None |
NULL (passed as-is) |
Explicit types with ydb.TypedValue:
When automatic inference is not suitable (e.g. you need Int32 instead of
Int64, or Json), wrap the value in ydb.TypedValue — it will be passed
through unchanged:
import ydb
with connection.cursor() as cursor:
cursor.execute(
"INSERT INTO events (id, payload) VALUES (%(id)s, %(payload)s)",
{
"id": ydb.TypedValue(99, ydb.PrimitiveType.Int32),
"payload": ydb.TypedValue('{"key": "value"}', ydb.PrimitiveType.Json),
},
)
If the driver cannot infer a YDB type for a Python value, it raises
TypeError. Use ydb.TypedValue for such values or when you need an explicit
YDB type.
Native YDB mode (default, deprecated)
Deprecated. Native YDB mode is the current default for backwards compatibility, but it will be removed in a future release. Migrate to
pyformat=Trueat your earliest convenience.
By default (pyformat=False) the driver passes the query and parameters
directly to the YDB SDK without any transformation. Use $name placeholders
in the query and supply a dict with $-prefixed keys:
connection = ydb_dbapi.connect(
host="localhost", port="2136", database="/local",
)
with connection.cursor() as cursor:
cursor.execute(
"SELECT * FROM users WHERE id = $id",
{"$id": ydb.TypedValue(42, ydb.PrimitiveType.Int64)},
)
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
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file ydb_dbapi-0.1.22.tar.gz.
File metadata
- Download URL: ydb_dbapi-0.1.22.tar.gz
- Upload date:
- Size: 18.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
79df761d2bd67d532a7bae2e9a155f2f083e8de9a71c8398bbdeaceca692677f
|
|
| MD5 |
c475e08285379e756a6531acc4e49829
|
|
| BLAKE2b-256 |
6929473641a18ab54333d8c0d4f2fe46d7acad03688378e9e7e79cf7c95cc4d7
|
Provenance
The following attestation bundles were made for ydb_dbapi-0.1.22.tar.gz:
Publisher:
python-publish.yml on ydb-platform/ydb-python-dbapi
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
ydb_dbapi-0.1.22.tar.gz -
Subject digest:
79df761d2bd67d532a7bae2e9a155f2f083e8de9a71c8398bbdeaceca692677f - Sigstore transparency entry: 1519632118
- Sigstore integration time:
-
Permalink:
ydb-platform/ydb-python-dbapi@2dca46da0b60e52eab7915fd0d0711bf6c1d08bf -
Branch / Tag:
refs/heads/main - Owner: https://github.com/ydb-platform
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
python-publish.yml@2dca46da0b60e52eab7915fd0d0711bf6c1d08bf -
Trigger Event:
workflow_dispatch
-
Statement type:
File details
Details for the file ydb_dbapi-0.1.22-py3-none-any.whl.
File metadata
- Download URL: ydb_dbapi-0.1.22-py3-none-any.whl
- Upload date:
- Size: 19.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
6825d004441fce4a4ab701f619c2d6045809ee764a26d246e546ac5ae7d7be12
|
|
| MD5 |
93bc092d3f9e6b69992f79674fd34022
|
|
| BLAKE2b-256 |
3d14d3a1b14737d95f8192e66b08814a7fbddb4160a4ef4731fe795e9ca24708
|
Provenance
The following attestation bundles were made for ydb_dbapi-0.1.22-py3-none-any.whl:
Publisher:
python-publish.yml on ydb-platform/ydb-python-dbapi
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
ydb_dbapi-0.1.22-py3-none-any.whl -
Subject digest:
6825d004441fce4a4ab701f619c2d6045809ee764a26d246e546ac5ae7d7be12 - Sigstore transparency entry: 1519632123
- Sigstore integration time:
-
Permalink:
ydb-platform/ydb-python-dbapi@2dca46da0b60e52eab7915fd0d0711bf6c1d08bf -
Branch / Tag:
refs/heads/main - Owner: https://github.com/ydb-platform
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
python-publish.yml@2dca46da0b60e52eab7915fd0d0711bf6c1d08bf -
Trigger Event:
workflow_dispatch
-
Statement type: