Skip to main content

Dameng (DM) DB-API 2.0 driver implemented on top of JDBC via JPype

Project description

ns-pydm

基于 JDBC + JPype 的达梦(DM)Python 驱动,提供 DB-API 2.0 风格接口,跨平台使用。

特性

  • DB-API 2.0 风格:connect() / Connection / Cursor / commit() / rollback()
  • 参数绑定:支持 ? 占位符;兼容把 SQL 中的 %s(非引号区域)自动转换为 ?
  • 内置达梦 JDBC 驱动 Jar:安装 Python 包即可使用(仍需本机存在 Java 运行时)
  • 自动选择 Jar:根据 java -version 探测的 Java 主版本选择对应驱动

依赖

  • Python 3.7+
  • 运行环境需要安装 Java(JRE/JDK),并确保 java 在 PATH 中或已配置 JAVA_HOME

安装

pip install ns-pydm

快速开始

import nspydm

conn = nspydm.connect(
    user="SYSDBA",
    password="SYSDBA",
    server="127.0.0.1",
    port=5236,
)

cur = conn.cursor()
cur.execute("select ? as x", [1])
print(cur.fetchone())
conn.close()

参数绑定

推荐写法(JDBC 原生):使用 ?

cur.execute("select ? as x, ? as y", [1, "hello"])

兼容写法:使用 %s(会转换为 ?

cur.execute("select %s as x, %s as y", [1, "hello"])

事务与自动提交

conn = nspydm.connect(..., autocommit=False)
cur = conn.cursor()
cur.execute("insert into t(a) values(?)", [1])
conn.commit()

也可以通过属性开关:

conn.autocommit = True

Schema 切换示例

cur.execute("set schema your_schema")

连接参数

  • connect(user, password, server, port, dsn, url, autocommit, properties, jars, driver_class)
  • url 优先级最高,支持直接传入 jdbc:dm://host:port 形式
  • dsn 传入 host:port 时会自动拼接为 jdbc:dm://{dsn}

Jar 选择与 Java 版本识别

  • 默认会通过 java -version 探测 Java 主版本,并自动选择内置的驱动 Jar:
    • Java 6 -> DmJdbcDriver6.jar
    • Java 7 -> DmJdbcDriver7.jar
    • Java 8/9/10 -> DmJdbcDriver8.jar
    • Java 11+ -> DmJdbcDriver11.jar
  • 如需自定义或覆盖默认选择:
    • 环境变量:DM_JDBC_JAR=/abs/path/to/DmJdbcDriver8.jar
    • 代码传参:connect(..., jars=["/abs/path/to/DmJdbcDriver8.jar"])

常见问题

  1. 提示找不到 java
    • 确认已安装 JRE/JDK,并保证 java -version 能在命令行执行
  2. 连接时报错但信息不全
    • 驱动底层异常会被映射为 DB-API 异常(例如 OperationalError),可直接打印异常信息排查

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

ns_pydm-0.0.3.tar.gz (6.4 MB view details)

Uploaded Source

Built Distribution

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

ns_pydm-0.0.3-py3-none-any.whl (6.4 MB view details)

Uploaded Python 3

File details

Details for the file ns_pydm-0.0.3.tar.gz.

File metadata

  • Download URL: ns_pydm-0.0.3.tar.gz
  • Upload date:
  • Size: 6.4 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.9.13

File hashes

Hashes for ns_pydm-0.0.3.tar.gz
Algorithm Hash digest
SHA256 b7be9402ad9146e591f3ad46bcf61300c1b6e57d6b88ceba85f1acd5e07c0e01
MD5 17f42a3bf0fb248d6a05cd75eee00c3a
BLAKE2b-256 7d21d39735a944108c9906c0f82e8f9a7d50ca75722e445f7e9e4e4f5bea4afb

See more details on using hashes here.

File details

Details for the file ns_pydm-0.0.3-py3-none-any.whl.

File metadata

  • Download URL: ns_pydm-0.0.3-py3-none-any.whl
  • Upload date:
  • Size: 6.4 MB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.9.13

File hashes

Hashes for ns_pydm-0.0.3-py3-none-any.whl
Algorithm Hash digest
SHA256 2c34bfb1c8c5b4a1dc55150db25d03a2a88d9cb03d19e78510dcfc10addf1ee7
MD5 7edbfa0e7ef6cd5d85c2c1141caa3b13
BLAKE2b-256 a783ea39f6c69b2636da1404682a4283542bb311408fdd80dc59227131d1b2f1

See more details on using hashes here.

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