一、代码编程

连接数据库

  1. # !/usr/bin/python
  2. # coding:utf-8
  3. import dmPython
  4. try:
  5. conn = dmPython.connect(user='SYSDBA', password='SYSDBA', server='localhost', port=5236)
  6. cursor = conn.cursor()
  7. print('python: conn success!')
  8. conn.close()
  9. except (dmPython.Error, Exception) as err:
  10. print(err)
  11. #PATH=PATH=D:\damba\dm8\drivers\\;D:\damba\dm8\drivers\logmnr\\;D:\damba\dm8\bin

绑定数据方式传参查询

  1. #!/usr/bin/python
  2. # coding:utf-8
  3. import dmPython
  4. ## 绑定数据的方式 ?
  5. try:
  6. conn = dmPython.connect(user='SYSDBA', password='SYSDBA', server='localhost', port=5236, local_code='utf-8')
  7. cursor = conn.cursor()
  8. try:
  9. # 清空表,初始化测试环境
  10. cursor.execute('delete from PRODUCTION.PRODUCT_CATEGORY')
  11. except (dmPython.Error, Exception) as err:
  12. print (err)
  13. try:
  14. # 插入数据
  15. values = ('稍等')
  16. cursor.execute("insert into PRODUCTION.PRODUCT_CATEGORY(name) values(?)", values)
  17. print('python: insert success!')
  18. # 查询数据
  19. cursor.execute("select name from PRODUCTION.PRODUCT_CATEGORY")
  20. res = cursor.fetchall()
  21. for tmp in res:
  22. for c1 in tmp:
  23. print c1
  24. print('python: select success!')
  25. except (dmPython.Error, Exception) as err:
  26. print(err)
  27. conn.close()
  28. except (dmPython.Error, Exception) as err:
  29. print(err)

调用DM的存储函授

  1. #!/usr/bin/python
  2. # coding:utf-8
  3. #coding:utf-8
  4. import dmPython
  5. try:
  6. conn = dmPython.connect(user='SYSDBA', password='SYSDBA', server='localhost', port=5236, local_code=1)
  7. cursor = conn.cursor()
  8. try:
  9. # 插入数据
  10. print('python: start success!')
  11. ## 调用无参数的存储函数
  12. cursor.execute('create or replace function test_func_1() return int as begin return 123;end;')
  13. print(cursor.callfunc('test_func_1'))
  14. ## 调用带参数的存储函数
  15. cursor.execute('create or replace function test_func_2(p1 int, p2 out int) return int as begin p2 = p1 + 1; return 456;end;')
  16. print(cursor.callfunc('test_func_2', 10000, 0))
  17. print('python: select success!')
  18. except (dmPython.Error, Exception) as err1:
  19. print (err1)
  20. conn.close()
  21. except (dmPython.Error, Exception) as err:
  22. print(err)

调用DM的存储过程

  1. #!/usr/bin/python
  2. # coding:utf-8
  3. #coding:utf-8
  4. import dmPython
  5. try:
  6. conn = dmPython.connect(user='SYSDBA', password='SYSDBA', server='localhost', port=5236, local_code=1)
  7. cursor = conn.cursor()
  8. try:
  9. # 插入数据
  10. print('python: start success!')
  11. # 调用无参的存储过程
  12. cursor.execute('create or replace procedure test_proc_1() as begin print true;end;')
  13. print(cursor.callproc('test_proc_1'))
  14. ## 调用有参的存储过程
  15. cursor.execute('create or replace procedure test_proc_2(p1 int, p2 out int) as begin p2 = p1 + 1;end;')
  16. print(cursor.callproc('test_proc_2', 10000, 0))
  17. paras_list = (10000, 0)
  18. ## 元组 存储 的存储过程
  19. cursor.execute('create or replace procedure test_proc_2(p1 int, p2 out int) as begin p2 = p1 + 1;end;')
  20. print(cursor.callproc('test_proc_2', paras_list))
  21. ## 调用其他模式有参的存储过程
  22. cursor.execute('create or replace procedure dmhr.test_proc_2(p1 int, p2 out int) as begin p2 = p1 + 1;end;')
  23. print(cursor.callproc('test_proc_2', 10000, 0))
  24. print('python: select success!')
  25. except (dmPython.Error, Exception) as err1:
  26. print (err1)
  27. conn.close()
  28. except (dmPython.Error, Exception) as err:
  29. print(err)

DM的CRUD

  1. #!/usr/bin/python
  2. # coding:utf-8
  3. import dmPython
  4. try:
  5. conn = dmPython.connect(user='SYSDBA', password='SYSDBA', server='localhost', port=5236, local_code=1)
  6. cursor = conn.cursor()
  7. try:
  8. # 清空表,初始化测试环境
  9. cursor.execute('delete from PRODUCTION.PRODUCT_CATEGORY')
  10. except (dmPython.Error, Exception) as err:
  11. print(err)
  12. try:
  13. # 插入数据
  14. cursor.execute("insert into PRODUCTION.PRODUCT_CATEGORY(NAME) values('sd'), ('单独'), ('3'), ('4')")
  15. print('python: insert success!')
  16. # 删除数据
  17. cursor.execute("delete from PRODUCTION.PRODUCT_CATEGORY where name='1'")
  18. print('python: delete success!')
  19. # 更新数据
  20. cursor.execute('update PRODUCTION.PRODUCT_CATEGORY set name = \'222\' where name=\'2\'')
  21. print('python: update success!')
  22. # 查询数据
  23. cursor.execute("select name from PRODUCTION.PRODUCT_CATEGORY")
  24. res = cursor.fetchall()
  25. for tmp in res:
  26. for c1 in tmp:
  27. print c1
  28. print('python: select success!')
  29. except (dmPython.Error, Exception) as err1:
  30. print (err1)
  31. conn.close()
  32. except (dmPython.Error, Exception) as err:
  33. print(err)

执行命令操作fldr

fldr_export.py

  1. #encoding:utf-8
  2. import subprocess
  3. cmd = r"D:\damba\dm8\bin\dmfldr.exe userid=SYSDBA/SYSDBA@localhost:5236 control='E:\export.ctl' mode = 'out'"
  4. p = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
  5. for line in p.stdout.readlines(): # 逐行输出
  6. print(line.decode('GBK'))
  7. retval = p.wait() # 等待进程结束返回结果
  8. print(retval)
  9. ### -- 创建表sql
  10. # CREATE TABLE "SYSDBA"."TEST"
  11. # (
  12. # "C1" INT,
  13. # "C2" INT,
  14. # "C3" DATE) STORAGE(ON "MAIN", CLUSTERBTR) ;
  15. # -- 插入数据
  16. # INSERT INTO test VALUES(1, 1 , '2021-09-15');
  17. # INSERT INTO test VALUES(2, 2 , '2021-09-16');
  18. # INSERT INTO test VALUES(3, 3 , '2021-09-17');
  19. # INSERT INTO test VALUES(4, null , '2021-09-18');
  20. ###

fldr_import.py

  1. #encoding:utf-8
  2. import subprocess
  3. cmd = r"D:\damba\dm8\bin\dmfldr.exe userid=SYSDBA/SYSDBA@localhost:5236 control='E:\import.ctl'"
  4. p = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
  5. for line in p.stdout.readlines(): # 逐行输出
  6. print(line.decode('GBK'))
  7. retval = p.wait() # 等待进程结束返回结果
  8. print(retval)
  9. # CREATE TABLE "SYSDBA"."TEST4"
  10. # (
  11. # "C1" INT,
  12. # "C2" VARCHAR(8188)) STORAGE(ON "MAIN", CLUSTERBTR) ;

二、测试案例

1. 报文测试

  1. #!/usr/bin/python
  2. #coding:utf-8
  3. import codecs
  4. import socket
  5. import time
  6. payload="00000000c800520000000000000000000000009a000000000000000001010200000000000000000000000000000000000000000000000000000000000000000009000000382e312e302e313437004000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"
  7. s=socket.socket()
  8. s.connect(('120.53.103.235',5236))
  9. shakehand=codecs.decode(payload,'hex')
  10. s.send(shakehand)
  11. time.sleep(1)
  12. print(s.recv(1024))
  13. 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


代码包

pythonCode.zip