Skip to main content

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)s and positional %s placeholders in one query;
  • named placeholders require a dict with bare keys like {"id": 1};
  • positional placeholders require a list or tuple;
  • missing or extra parameters raise ProgrammingError;
  • keys starting with $ are not allowed in pyformat=True mode.

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=True at 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


Download files

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

Source Distribution

ydb_dbapi-0.1.21.tar.gz (18.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

ydb_dbapi-0.1.21-py3-none-any.whl (19.1 kB view details)

Uploaded Python 3

File details

Details for the file ydb_dbapi-0.1.21.tar.gz.

File metadata

  • Download URL: ydb_dbapi-0.1.21.tar.gz
  • Upload date:
  • Size: 18.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for ydb_dbapi-0.1.21.tar.gz
Algorithm Hash digest
SHA256 8a305fd1b719705834da0f8a47c2ba913a393db256cf6ac7a7e6c8ca0d108990
MD5 a4c0d5e34ec1756f7d3d0c1168c62dd9
BLAKE2b-256 7e113257b937253147690937a88e4ee6e151355d24fdade2521c9f0565ddf369

See more details on using hashes here.

Provenance

The following attestation bundles were made for ydb_dbapi-0.1.21.tar.gz:

Publisher: python-publish.yml on ydb-platform/ydb-python-dbapi

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file ydb_dbapi-0.1.21-py3-none-any.whl.

File metadata

  • Download URL: ydb_dbapi-0.1.21-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

Hashes for ydb_dbapi-0.1.21-py3-none-any.whl
Algorithm Hash digest
SHA256 b90199ba76b18f564feeb256eec4740ccfa5f6cef7836a8873611141e1c11e76
MD5 5a33cea8348dfbe97357e8cd1dec05ee
BLAKE2b-256 4e8e2a17a3c72c3e56dad584090cbf4793fbb50601b7573b0774f98c480bbc65

See more details on using hashes here.

Provenance

The following attestation bundles were made for ydb_dbapi-0.1.21-py3-none-any.whl:

Publisher: python-publish.yml on ydb-platform/ydb-python-dbapi

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

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