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 如下:
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 如下:
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 如下:
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]#
|