Skip to main content

python-oceanbase-driver

Python 的 OceanBase 驱动,基于 PyMySQL 1.2.0,纯 Python,用法与 PyMySQL 完全一致, 区别是默认置上握手包 capability bit27,可直连 OceanBase Oracle 租户。 未打补丁的 MySQL 协议驱动连 Oracle 租户会被服务端拒绝:

pyoceanbase.err.NotSupportedError: (1235, 'Oracle tenant for current client driver is not supported')

原理:OceanBase 登录时检查该位(OB_CLIENT_SUPPORT_ORACLE_MODE, 与 MySQL 8.0 的 CLIENT_QUERY_ATTRIBUTES 同一位),mysql 8.0 客户端默认置位, PyMySQL 默认没置。本驱动默认置上,对 MySQL / MariaDB / OB MySQL 租户无副作用。

安装

pip install pyoceanbase

注意:顶层包名为 pyoceanbase,可与上游 PyMySQL 共存于同一环境,按需选用。

用法

import pyoceanbase

conn = pyoceanbase.connect(
    host="HOST",
    port=9090,
    user="USER@TENANT#CLUSTER",
    password="PASSWORD",
    database="DBNAME",
)
with conn.cursor() as cur:
    cur.execute("SELECT USER FROM DUAL")
    print(cur.fetchone())
conn.close()

SQLAlchemy

pip install "pyoceanbase[sqlalchemy]"

MySQL 租户(oceanbase+pyoceanbase,ORM 全功能):

from sqlalchemy import create_engine

engine = create_engine("oceanbase+pyoceanbase://USER:PASSWORD@HOST:3306/DBNAME")

Oracle 租户(oceanbase_oracle+pyoceanbase,主键用 Sequence,不支持 identity/RETURNING):

from sqlalchemy import create_engine, MetaData, Table, Column, Integer, String, Sequence

# 用户名里的 @/# 必须百分号编码:@ -> %40,# -> %23
engine = create_engine("oceanbase_oracle+pyoceanbase://USER%40TENANT%23CLUSTER:PASSWORD@HOST:9090/DBNAME")

md = MetaData()
seq = Sequence("T_SEQ")
t = Table("T", md, Column("id", Integer, seq, primary_key=True), Column("name", String(50)))
md.create_all(engine)

注意:Oracle 租户须用 Oracle 方言写原生 SQL 时照常 text("SELECT ... FROM DUAL"), 表名建议大写;数字类型默认返回 Decimal

注意事项

  1. 业务 SQL 须用 Oracle 方言(如 FROM DUALSYSDATE)。
  2. 默认 autocommit=False,写入后记得 conn.commit()(标准 PyMySQL 行为)。
  3. 数字类型默认返回 Decimal(标准 PyMySQL 行为),可用 conv 覆盖。
  4. DSN 参数等细节见上游文档:https://github.com/PyMySQL/PyMySQL

协议

MIT,与上游一致。

Download files

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

Source Distribution

pyoceanbase-1.1.0.tar.gz (47.3 kB view details)

Uploaded Source

Built Distribution

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

pyoceanbase-1.1.0-py3-none-any.whl (48.7 kB view details)

Uploaded Python 3

File details

Details for the file pyoceanbase-1.1.0.tar.gz.

File metadata

  • Download URL: pyoceanbase-1.1.0.tar.gz
  • Upload date:
  • Size: 47.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/7.0.0 CPython/3.13.14

File hashes

Hashes for pyoceanbase-1.1.0.tar.gz
Algorithm Hash digest
SHA256 cd6e55d16fc2f61c19c5cd8ff532c4f2afbdbd69b64471bfe44a2781151e05c9
MD5 4094684f9f2933768ee663a4ec6138a0
BLAKE2b-256 7bbf1c0cfea7f118b051025573f2a874a00ff225665f53a6c64e8caf04d496cd

See more details on using hashes here.

Provenance

The following attestation bundles were made for pyoceanbase-1.1.0.tar.gz:

Publisher: publish.yml on oceanbase-driver/python-oceanbase-driver

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

File details

Details for the file pyoceanbase-1.1.0-py3-none-any.whl.

File metadata

  • Download URL: pyoceanbase-1.1.0-py3-none-any.whl
  • Upload date:
  • Size: 48.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/7.0.0 CPython/3.13.14

File hashes

Hashes for pyoceanbase-1.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 5edd21f7a125c65d63de3b1dab88c14d65fd457be473a81fa05ef661f1f2a777
MD5 1ee26fef8bf3d96bee29fb47114e1f43
BLAKE2b-256 4cf4a813b298d79cd4be908072ecefed92171a61781784e309fd22da4fbaf4af

See more details on using hashes here.

Provenance

The following attestation bundles were made for pyoceanbase-1.1.0-py3-none-any.whl:

Publisher: publish.yml on oceanbase-driver/python-oceanbase-driver

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

Release history Release notifications | RSS feed

This release

1.1.0 This release

2 files

1.0.1

2 files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page