一、代码编程
连接数据库
# !/usr/bin/python# coding:utf-8import dmPythontry:conn = dmPython.connect(user='SYSDBA', password='SYSDBA', server='localhost', port=5236)cursor = conn.cursor()print('python: conn success!')conn.close()except (dmPython.Error, Exception) as err:print(err)#PATH=PATH=D:\damba\dm8\drivers\\;D:\damba\dm8\drivers\logmnr\\;D:\damba\dm8\bin
绑定数据方式传参查询
#!/usr/bin/python# coding:utf-8import dmPython## 绑定数据的方式 ?try:conn = dmPython.connect(user='SYSDBA', password='SYSDBA', server='localhost', port=5236, local_code='utf-8')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 c1print('python: select success!')except (dmPython.Error, Exception) as err:print(err)conn.close()except (dmPython.Error, Exception) as err:print(err)
调用DM的存储函授
#!/usr/bin/python# coding:utf-8#coding:utf-8import dmPythontry:conn = dmPython.connect(user='SYSDBA', password='SYSDBA', server='localhost', port=5236, local_code=1)cursor = conn.cursor()try:# 插入数据print('python: start success!')## 调用无参数的存储函数cursor.execute('create or replace function test_func_1() return int as begin return 123;end;')print(cursor.callfunc('test_func_1'))## 调用带参数的存储函数cursor.execute('create or replace function test_func_2(p1 int, p2 out int) return int as begin p2 = p1 + 1; return 456;end;')print(cursor.callfunc('test_func_2', 10000, 0))print('python: select success!')except (dmPython.Error, Exception) as err1:print (err1)conn.close()except (dmPython.Error, Exception) as err:print(err)
调用DM的存储过程
#!/usr/bin/python# coding:utf-8#coding:utf-8import dmPythontry:conn = dmPython.connect(user='SYSDBA', password='SYSDBA', server='localhost', port=5236, local_code=1)cursor = conn.cursor()try:# 插入数据print('python: start success!')# 调用无参的存储过程cursor.execute('create or replace procedure test_proc_1() as begin print true;end;')print(cursor.callproc('test_proc_1'))## 调用有参的存储过程cursor.execute('create or replace procedure test_proc_2(p1 int, p2 out int) as begin p2 = p1 + 1;end;')print(cursor.callproc('test_proc_2', 10000, 0))paras_list = (10000, 0)## 元组 存储 的存储过程cursor.execute('create or replace procedure test_proc_2(p1 int, p2 out int) as begin p2 = p1 + 1;end;')print(cursor.callproc('test_proc_2', paras_list))## 调用其他模式有参的存储过程cursor.execute('create or replace procedure dmhr.test_proc_2(p1 int, p2 out int) as begin p2 = p1 + 1;end;')print(cursor.callproc('test_proc_2', 10000, 0))print('python: select success!')except (dmPython.Error, Exception) as err1:print (err1)conn.close()except (dmPython.Error, Exception) as err:print(err)
DM的CRUD
#!/usr/bin/python# coding:utf-8import dmPythontry:conn = dmPython.connect(user='SYSDBA', password='SYSDBA', server='localhost', port=5236, local_code=1)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('sd'), ('单独'), ('3'), ('4')")print('python: insert success!')# 删除数据cursor.execute("delete from PRODUCTION.PRODUCT_CATEGORY where name='1'")print('python: delete success!')# 更新数据cursor.execute('update PRODUCTION.PRODUCT_CATEGORY set name = \'222\' where name=\'2\'')print('python: update success!')# 查询数据cursor.execute("select name from PRODUCTION.PRODUCT_CATEGORY")res = cursor.fetchall()for tmp in res:for c1 in tmp:print c1print('python: select success!')except (dmPython.Error, Exception) as err1:print (err1)conn.close()except (dmPython.Error, Exception) as err:print(err)
执行命令操作fldr
fldr_export.py
#encoding:utf-8import subprocesscmd = r"D:\damba\dm8\bin\dmfldr.exe userid=SYSDBA/SYSDBA@localhost:5236 control='E:\export.ctl' mode = 'out'"p = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)for line in p.stdout.readlines(): # 逐行输出print(line.decode('GBK'))retval = p.wait() # 等待进程结束返回结果print(retval)### -- 创建表sql# CREATE TABLE "SYSDBA"."TEST"# (# "C1" INT,# "C2" INT,# "C3" DATE) STORAGE(ON "MAIN", CLUSTERBTR) ;# -- 插入数据# INSERT INTO test VALUES(1, 1 , '2021-09-15');# INSERT INTO test VALUES(2, 2 , '2021-09-16');# INSERT INTO test VALUES(3, 3 , '2021-09-17');# INSERT INTO test VALUES(4, null , '2021-09-18');###
fldr_import.py
#encoding:utf-8import subprocesscmd = r"D:\damba\dm8\bin\dmfldr.exe userid=SYSDBA/SYSDBA@localhost:5236 control='E:\import.ctl'"p = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)for line in p.stdout.readlines(): # 逐行输出print(line.decode('GBK'))retval = p.wait() # 等待进程结束返回结果print(retval)# CREATE TABLE "SYSDBA"."TEST4"# (# "C1" INT,# "C2" VARCHAR(8188)) STORAGE(ON "MAIN", CLUSTERBTR) ;
二、测试案例
1. 报文测试
#!/usr/bin/python#coding:utf-8import codecsimport socketimport timepayload="00000000c800520000000000000000000000009a000000000000000001010200000000000000000000000000000000000000000000000000000000000000000009000000382e312e302e313437004000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"s=socket.socket()s.connect(('120.53.103.235',5236))shakehand=codecs.decode(payload,'hex')s.send(shakehand)time.sleep(1)print(s.recv(1024))time.sleep(1)
参考连接
[Python 封装 DM 达梦 数据库操作(使用类封装基本的增删改查)]https://www.cndba.cn/dave/article/3827 [Python 数据库接口]https://eco.dameng.com/docs/zh-cn/app-dev/python-python.html
