Python在linux下连接达梦数据库

环境:Linux centos7.6 +Python 2.7.5+DM8

必须安装dmPython。

安装包可以在1139926252QQ群里的文件中下载,下载好后上传到linux服务器中。

一、安装dmPython

Python 有现成的mysql,oracle 等数据库包,可以直接import 使用,但对于达梦数据库,必须首先安装dmPython,才可以使用。

dmPython的运行需要使用dpi动态库,用户应将dpi所在目录(一般为DM安装目录中的bin目录)加入系统环境变量。

[root@DM2 dmPython]# vi ~/.bash_profile

添加如下配置:

执行以下命令使环境变量生效:

[root@DM2 dmPython]# source ~/.bash_profile

开始安装

[root@DM2 dmPython]# python setup.py install

二、测试

1、创建Python脚本ConnDM.py

import dmPython
try:
  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)

2、执行脚本

python ConnDM.py

三、建表插入数据

创建脚本connPython2.py

import dmPython

try:
  conn = dmPython.connect(user='SYSDBA', password='SYSDBA', server='localhost', port=5236)
  cursor = conn.cursor()
  print('python: conn success!')
  cursor.execute("create table test(c1 int, c2 varchar)")
  cursor.execute("insert into test values(2,'hyf')")
  cursor.execute("select * from test")
  res = cursor.fetchall()
  for tmp in res:
    for c1 in tmp:
      print(c1)
      print('python: select success!')
      conn.close()
except (dmPython.Error, Exception) as err:
print(err)

 

原文地址:https://www.cnblogs.com/hong-yf/p/14764982.html