DMPython安装和测试
1、DMPython概述
Python 是一个高层次的结合了解释性、编译性、互动性和面向对象的脚本语言。运行 Python 程序需要解释器的支持,只要在不同的平台安装不同的解释器,Python 代码就可以跨平台运行,可移植性强,不用担心任何兼容性问题。
DMPython 是 DM 提供的依据 Python DB API version 2.0 中 API 使用规定而开发的数据库访问接口。DMPython 实现这些 API,使 Python 应用程序能够对 DM 数据库进行访问。
DMPython 通过调用 DM DPI 接口完成 Python 模块扩展。在其使用过程中,除 Python 标准库以外,还需要 DPI 的运行环境。以下是通过 DMPython 对 DM 数据库进行一些基本操作的示例。
2、DMPython部署
1)Linux下部署dmPyhon
—确认当前linux环境python版本
[root@dm ~]# python --version
—确认当前gcc版本
[root@dm ~]# gcc --version
—获取DMPython
—部署DMPython
python setup.py install
—验证
2)部署问题处理
(1)cannot locate an Dameng software
部署时提示报错cannot locate an Dameng software,这是因为在root用户下执行,且root用户没有配置相关环境变量导致的。为root用户配置相应的环境变量即可。
(2)[Erron 13] Permission denied
在上一个报错中,因为部署时提示定位不到达梦的安装路径。可能是因为root没有配置对应的环境变量,尝试用dmdba用户进行配置。但是dmdba用户执行部署时,提示linux相关路径下的权限报错。所以用dmdba部署还是不行,还是需要用root用户配置并设置对应的环境变量。
(3)Python.h:No such file or directory
继续使用root用户进行部署,配置了对应的环境变量后,安装仍提示报错:Python.h:No such file or directory。查询了相关资料后,推断当前环境可能缺少部分依赖导致无法安装。所以尝试安装涉及python的所有依赖。安装完依赖后,部署成功。
3、DMPython测试
1)数据库连接测试
---编辑连接测试脚本
Vim py_conn.py
#!/usr/bin/python
#coding:utf-8
import DMPython
try:
conn = DMPython.connect(user='SYSDBA', password='Dameng_123', server='localhost', port=5236)
cursor = conn.cursor()
print('python: conn success!')
conn.close()
except (DMPython.Error, Exception) as err:
print(err)
—测试结果
[root@dm dmdata]# python py_conn.py
2)基础操作测试
---编辑操作脚本
Vim py_dml.py
#!/usr/bin/python
#coding:utf-8
import DMPython
try:
conn = DMPython.connect(user='SYSDBA', password='Dameng_123', server='192.168.168.13', port=5236)
cursor = conn.cursor()
try:
#清空表,初始化测试环境
cursor.execute ('delete from PRODUCTION.PRODUCT_CATEGORY')
except (DMPython.Error, Exception) as err:
print(err)
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@dm dmdata]# python py_dml.py
3)绑定变量测试
---编辑测试脚本
vim py_bind.py
#!/usr/bin/python
#coding:utf-8
import DMPython
try:
conn = DMPython.connect(user='SYSDBA', password='Dameng_123', server='localhost', port=5236)
cursor = conn.cursor()
try:
#清空表,初始化测试环境
cursor.execute ('delete from PRODUCTION.PRODUCT_CATEGORY')
except (DMPython.Error, Exception) as err:
print(err)
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@dm dmdata]# python py_bind.py
4)大字段测试
---编辑测试脚本
vim py_blob.py
#!/usr/bin/python
#coding:utf-8
import sys
longstring = ""
longstring += 'ABCDEF0123456789' * 500
cvalue = longstring
if sys.version_info[0] >= 3 :
bvalue = longstring.encode("ascii")
else :
bvalue = longstring
import DMPython
conn = DMPython.connect(user='SYSDBA', password='Dameng_123', server='localhost', port=5236)
cursor = conn.cursor()
try:
#清理测试环境
cursor.execute("select object_id from all_objects where object_type='TABLE' and OBJECT_NAME='BIG_DATA';")
bigdata_id = cursor.fetchone()
if(bigdata_id):
cursor.execute('drop table PRODUCTION.BIG_DATA;')
print('drop table success')
cursor.execute('create table PRODUCTION.BIG_DATA(c1 blob, c2 clob)')
print('create table success!')
cursor.execute('insert into PRODUCTION.BIG_DATA values(?, ?)', bvalue, cvalue)
print('insert success!')
cursor.execute('select * from PRODUCTION.BIG_DATA')
print('select success!')
cursor.description
row = cursor.fetchone()
(blob, clob) = row
if sys.version_info[0] >= 3 :
type(blob)
type(clob)
blob
clob
except(DMPython.Error,Exception) as err:
print(err)
conn.close
—测试结果
[root@dm dmdata]# python py_blob.py
5)多线程测试
---编辑测试脚本
vim py_multi.py
#!/usr/bin/python
import DMPython
import thread
import time
def print_time( threadName, delay):
count = 0
while count < 3:
try:
conn = DMPython.connect(user='SYSDBA',password='Dameng_123',server='localhost',port=5236)
cursor = conn.cursor()
print ("%s: %s" % ( threadName, time.ctime(time.time()) ))
conn.close()
time.sleep(delay)
count += 1
except(DMPython.Error, Exception) as err:
print(err)
try:
thread.start_new_thread(print_time, ("Thread-1", 1,))
thread.start_new_thread(print_time, ("Thread-2", 2,))
except:
print ("Error: unable to start thread")
while 1:
pass
—测试结果
[root@dm dmdata]# python py_multi.py
社区地址:https://eco.dameng.com