zoukankan      html  css  js  c++  java
  • 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)

     

  • 相关阅读:
    JavaScript DOM编程艺术 读书笔记(简略)
    关于暂停或终止更新的相关读书笔记
    Core Java Volume II—Using annotations
    Data Structure and Algorithms Analysis in C Note (II)
    Hibernate实战——理解对象/关系持久化 笔记
    Data Structure and Algorithms Analysis in C Note (I)
    Java 8实战 第三章
    GitHub入门与实践 学习笔记(二)
    Computer Networking A Top-Down Approach 笔记(一)
    进程基础
  • 原文地址:https://www.cnblogs.com/hong-yf/p/14764982.html
Copyright © 2011-2022 走看看