暂无图片
暂无图片
暂无图片
暂无图片
暂无图片

DM8达梦数据库Python 数据库接口

原创 达梦 2021-01-31
2014

DMPython 是 DM 提供的依据 Python DB API version 2.0 中 API 使用规定而开发的数据库访问接口。DMPython 实现这些 API,使 Python 应用程序能够对 DM 数据库进行访问。

DMPython 通过调用 DM DPI 接口完成 Python 模块扩展。在其使用过程中,除 Python 标准库以外,还需要 DPI 的运行环境。

开发环境搭建

软件 版本
DM 数据库 DM 8.0 及以上版本
Python Python 2.7.5

安装 Python

用户请自行下载安装 Python。也可使用系统自带 Python 环境。本例使用 CentOS Linux release 7.6.1810 (Core) 默认安装的 Python 2.7.5 版本。

[root@RS1821 pytest]# python --version
Python 2.7.5
[root@RS1821 pytest]#

编译安装 DM 驱动

获取达梦 python 驱动源码 并解压。

[root@RS1821 ssd1]# unzip python-126594-20201027.zip
[root@RS1821 ssd1]# cd python/dmPython_C/dmPython/
[root@RS1821 dmPython]# python setup.py install
...
...
...
Installed /usr/lib64/python2.7/site-packages/dmPython-2.3-py2.7-linux-x86_64.egg
Processing dependencies for dmPython==2.3
Finished processing dependencies for dmPython==2.3
[root@RS1821 dmPython]#

数据库连接

Python 接口登录、登出示例程序 py_conn.py 如下:


#!/usr/bin/python
#coding:utf-8
import dmPython
try:
conn = dmPython.connect(user='SYSDBA', password='SYSDBA', server='localhost', port=51236)
cursor = conn.cursor()
print('python: conn success!')
conn.close()
except (dmPython.Error, Exception) as err:
print(err)

执行结果如下:

[root@RS1821 pytest]# python py_conn.py
python: conn success!
[root@RS1821 pytest]#

开发示例

基本操作示例

Python 接口增、删、改、查四个基本操作,示例程序 py_dml.py 如下:

#!/usr/bin/python
#coding:utf-8
import dmPython
try:
conn = dmPython.connect(user='SYSDBA', password='SYSDBA', server='localhost', port=51236)
cursor = conn.cursor()
try:
#清空表,初始化测试环境
cursor.execute ('delete from PRODUCTION.PRODUCT_CATEGORY')
except (dmPython.Error, Exception) as err:
null

try:
#插入数据
cursor.execute ("insert into PRODUCTION.PRODUCT_CATEGORY(NAME) values('语文'), ('数学'), ('英语'), ('体育')")
print('python: insert success!')
#删除数据
cursor.execute ("delete from PRODUCTION.PRODUCT_CATEGORY where name='数学'")
print('python: delete success!')

#更新数据
cursor.execute ('update PRODUCTION.PRODUCT_CATEGORY set name = \'英语-新课标\' where name=\'英语\'')
print('python: update success!')

#查询数据
cursor.execute ("select name from PRODUCTION.PRODUCT_CATEGORY")
res = cursor.fetchall()
for tmp in res:
for c1 in tmp:
print c1

print('python: select success!')
except (dmPython.Error, Exception) as err:
print(err)

conn.close()
except (dmPython.Error, Exception) as err:
print(err)

执行结果如下:

[root@RS1821 pytest]# python py_dml.py
python: insert success!
python: delete success!
python: update success!
语文
英语-新课标
体育
python: select success!
[root@RS1821 pytest]#

绑定变量示例

Python 接口绑定变量示例程序 py_bind.py 如下:

#!/usr/bin/python
#coding:utf-8
import dmPython
try:
conn = dmPython.connect(user='SYSDBA', password='SYSDBA', server='localhost', port=51236)
cursor = conn.cursor()
try:
#清空表,初始化测试环境
cursor.execute ('delete from PRODUCTION.PRODUCT_CATEGORY')
except (dmPython.Error, Exception) as err:
null

try:
#插入数据
values = ('物理')
cursor.execute ("insert into PRODUCTION.PRODUCT_CATEGORY(name) values(?)", values)
print('python: insert success!')

#查询数据
cursor.execute ("select name from PRODUCTION.PRODUCT_CATEGORY")
res = cursor.fetchall()
for tmp in res:
for c1 in tmp:
print c1

print('python: select success!')
except (dmPython.Error, Exception) as err:
print(err)


conn.close()
except (dmPython.Error, Exception) as err:
print(err)

执行结果如下:

[root@RS1821 pytest]# python py_bind.py
python: insert success!
物理
python: select success!
[root@RS1821 pytest]#

「喜欢这篇文章,您的关注和赞赏是给作者最好的鼓励」
关注作者
【版权声明】本文为墨天轮用户原创内容,转载时必须标注文章的来源(墨天轮),文章链接,文章作者等基本信息,否则作者和墨天轮有权追究责任。如果您发现墨天轮中有涉嫌抄袭或者侵权的内容,欢迎发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

评论