Python连接达梦数据库

python如果想连接达梦数据库,必须要安装dmPython。

简介:dmPython 是 DM 提供的依据 Python DB API version 2.0 中 API 使用规定而开发的数据库访问接口。dmPython 实现这些 API,使 Python 应用程序能够对 DM 数据库进行访问。

dmPython 通过调用 DM DPI 接口完成 python 模块扩展。在其使用过程中,除 Python 标准库以外,还需要 DPI 的运行环境

第一步:使用源码包方式安装

进入达梦数据库安装目录下的 dmPython 目录,执行命令 python setup.py install

注意:前提需要你有C++环境,Visual Studio。

出现上面这些信息代表安装成功。 

第二步:配置dpi环境变量

第三步:3.8及以上版本需操作

第四步:复制操作

将达梦数据库安装目录中的drivers/dpi下的所有文件复制到D:\python\python3.9\Lib\site-packages\dmPython-2.4.5-py3.9-win-amd64.egg下。

第五步:编写python查询达梦数据库代码进行测试

# coding:utf-8
import dmPython

try:
    # 创建达梦数据库连接
    conn = dmPython.connect(user='TEST', password='abc123456', server='localhost',
                            port=5236)
    # 创建数据库操作对象
    cursor = conn.cursor()
    # try:
    #     # 清空表,初始化测试环境
    #     cursor.execute('delete from T2')
    # except (dmPython.Error, Exception) as err:
    #     print(err)
    try:
        # 插入数据
        # cursor.execute("insert into DMHR.EMPLOYEE (EMPLOYEE_ID,EMPLOYEE_NAME,EMAIL,HIRE_DATE,JOB_ID) values(1157, '马云','888888888@qq.com','2023-05-12','42')")
        # print('python: insert success!')

        # # 更新数据
        # cursor.execute("update DMHR.EMPLOYEE set EMPLOYEE_NAME = '刘强东' where EMPLOYEE_ID = 1157")
        # print('python: update success!')

        # 查询数据
        cursor.execute("select id from test.SYSTEMS_USER")
        res = cursor.fetchall()
        for tmp in res:
            for c1 in tmp:
                print(c1)
        print('python: select success!')

        # # 删除数据
        # cursor.execute("delete from DMHR.EMPLOYEE where EMPLOYEE_ID = 1157")
        # print('python: delete success!')
        #


    except (dmPython.Error, Exception) as err1:
        print(err1)
    conn.close()
except (dmPython.Error, Exception) as err:
    print(err)

文章出处登录后可见!

已经登录?立即刷新

共计人评分,平均

到目前为止还没有投票!成为第一位评论此文章。

(0)
xiaoxingxing的头像xiaoxingxing管理团队
上一篇 2023年8月23日
下一篇 2023年8月23日

相关推荐