Skip to main content

dmpython-jdbc

简体中文 | English

macOS 上的达梦 dmPython 兼容层。官方驱动没有 macOS 版,这里用 JayDeBeApi + 达梦 JDBC 实现 DBAPI 子集,让 dmSQLAlchemy 的 dm+dmPython:// 方言能在本机跑起来。

发行名是 dmpython-jdbc,import 名仍是 dmPython。 业务代码不用改。

  • macOS:使用本包的 JDBC shim
  • 其它平台:把 import dmPython 转发给已安装的官方驱动

安装

PyPI:

pip install dmpython-jdbc

跨平台项目(Linux/Windows 用官方驱动,macOS 用本包):

[project]
dependencies = [
    "dmpython>=2.5.32; sys_platform != 'darwin'",
    "dmpython-jdbc>=2.5.32; sys_platform == 'darwin'",
]

如果项目还依赖 dmSQLAlchemy,见 配合 dmSQLAlchemy。该包会无条件拉取官方 dmpython。

运行依赖

  • Python >= 3.12
  • JDK(macOS 可用 brew install openjdk)。JAVA_HOME 会自动探测 /opt/homebrew/opt/openjdk 和 /usr/local/opt/openjdk
  • 达梦 JDBC 驱动已打包进 wheel;也可用环境变量 DM_JDBC_JAR 覆盖

配合 dmSQLAlchemy

dmSQLAlchemy 无条件依赖官方 dmpython(Requires-Dist: dmPython)。该包没有 macOS wheel,所以在 Mac 上 pip install dmSQLAlchemy 会尝试装达梦官方驱动并失败。

要保留 dmSQLAlchemy,又阻止解析器在 macOS 上装官方 dmpython,改装 dmpython-jdbc。import 名仍是 dmPython。

uv

[project]
dependencies = [
    "dmsqlalchemy",
    "dmpython>=2.5.32; sys_platform != 'darwin'",
    "dmpython-jdbc>=2.5.32; sys_platform == 'darwin'",
]

[tool.uv]
override-dependencies = [
    "dmpython ; sys_platform != 'darwin'",
]

override-dependencies 会改写 dmSQLAlchemy 对 dmpython 的依赖,让它在 macOS 上被跳过;然后由 dmpython-jdbc 提供 dmPython 模块。

dmSQLAlchemy.extensions 会调用 importlib.metadata.version("dmPython")。本包会安装 .pth 钩子,让这次查询返回 shim 版本 2.5.32,不依赖 setuptools/pkg_resources。

macOS 上的 pip

pip install "dmSQLAlchemy" --no-deps
pip install "SQLAlchemy>1.4.54" dmpython-jdbc

作为 dmAsync 的 dmPython 后端

本包支持 dmAsync==1.0.0 使用的基础连接和游标接口:connect、execute、 executemany、fetchone/fetchmany/fetchall、异步迭代、commit、rollback 和 ping。dmAsync 使用 asyncio.to_thread 调用同步 shim 完成这些数据库操作; dmAsync 1.0.0 的建连和关闭仍是同步操作。

macOS 上在开发版仓库目录中安装:

python -m pip install . "async-timeout>=4,<6"
python -m pip install "dmAsync==1.0.0" --no-deps

dmAsync 无条件依赖官方 dmPython,因此使用 uv 时,需要保留上文的 [tool.uv].override-dependencies 配置,并在项目依赖中加入 dmAsync==1.0.0 和 async-timeout>=4,<6。dmAsync 1.0.0 导入了 async_timeout,但没有在包元数据中 声明该依赖。运行时的版本别名不能代替这些依赖解析配置。

import asyncio
import os
import dmAsync


async def main():
    conn = await dmAsync.connect(
        host=os.environ["DM_HOST"],
        port=int(os.environ.get("DM_PORT", "5236")),
        user=os.environ["DM_USER"],
        password=os.environ["DM_PASSWORD"],
        autoCommit=False,
        login_timeout=5000,  # 毫秒,直接传给 dmPython。
    )
    cur = await conn.cursor()
    try:
        await cur.execute("SELECT 1")
        print(await cur.fetchone())
        await conn.commit()
    except Exception:
        await conn.rollback()
        raise
    finally:
        await cur.close()
        await conn.close()


asyncio.run(main())

Connection.version 返回服务端版本;标识符长度限制从 JDBC 元数据读取,未知时 返回 None。shim 不实现 Python 层语句缓存,因此 stmtcachesize 为 0, 不支持设为非零值。status 表示 shim 连接状态(1 开启、0 关闭),不是 DPI 事务状态码。修改 autocommit 会同步到 JDBC 连接。

同一连接的 JDBC 操作会在线程间串行执行。并发任务应各自持有连接:锁无法隔离事务, 也无法保证多个任务共享游标时 execute/fetch 的先后关系。取消 await 不会终止线程中 已经开始的 JDBC 操作;重新使用或关闭该连接前,应等待操作结束。callproc、 callfunc、显式 prepare、mogrify、游标滚动、跨提交保留游标以及多结果集仍不支持, 调用会抛出 NotSupportedError。这些适配不代表已完整验证 SQLAlchemy 的 dm+dmasync:// 方言或 dmAsync 连接池。

适配测试

安装上述依赖后执行:

python -m unittest discover -s tests -v

测试使用真实 dmAsync 包,在 JayDeBeApi 边界用 SQLite 模拟数据库,覆盖初始化、 CRUD、事务、游标生命周期和连接级锁,不需要达梦服务器。

真库验证见 达梦端到端测试说明。配置专用测试账号后,执行 bash tests/e2e/test.sh 即可构建并测试实际安装的 wheel,覆盖二进制/LOB 数据往返、 跨连接事务可见性等。

限制

不支持存储过程出参(cursor.var)、LOB 流式读写等高级特性,覆盖 ORM CRUD 即可。

许可证

Python 兼容层使用 MIT。仓库中的 DmJdbcDriver18-*.jar 是达梦官方 JDBC 驱动,版权归达梦数据库所有,不受 MIT 覆盖。

Metadata

Release files for dmpython-jdbc 2.5.32.post3

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for dmpython-jdbc 2.5.32.post3
File Size Uploaded Provenance Actions
dmpython_jdbc-2.5.32.post3.tar.gz 1.4 MB With provenance Details for dmpython_jdbc-2.5.32.post3.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for dmpython-jdbc 2.5.32.post3
File Interpreter ABI Platform Actions
dmpython_jdbc-2.5.32.post3-py3-none-any.whl Python 3 none any Details for dmpython_jdbc-2.5.32.post3-py3-none-any.whl

Total release size: 2.7 MB

Release files / dmpython_jdbc-2.5.32.post3.tar.gz

Download URL dmpython_jdbc-2.5.32.post3.tar.gz
Size 1.4 MB
Tags Source
SHA-256 checksum
How to use checksums
05a65a48b2476a8da3d9530c9eb1143f6294c54757a8fd1faff18a443f18ba34
BLAKE2b-256 checksum
How to use checksums
188b02c74529620990e485aea451082506c41be485fa004a892b0f787cf30b2b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 19, 2026.

Transparency log

Release files / dmpython_jdbc-2.5.32.post3-py3-none-any.whl

Download URL dmpython_jdbc-2.5.32.post3-py3-none-any.whl
Size 1.3 MB
Tags Python 3
SHA-256 checksum
How to use checksums
df49a59e726dfb339e7216855646042ab0065f9a2a5c098341fd7698256eef0f
BLAKE2b-256 checksum
How to use checksums
15a609ae82221d93d5a6a05f8d52d59a8a9a50cfb6413b2bb550d93b929380e5
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 19, 2026.

Transparency log

Security for dmpython-jdbc 2.5.32.post3

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

Full provenance

Both files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for dmpython_jdbc-2.5.32.post3.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 19, 2026.

Transparency log

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

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