Python連接達夢數(shù)據(jù)庫的實現(xiàn)示例
python如果想連接達夢數(shù)據(jù)庫,必須要安裝dmPython。
簡介:dmPython 是 DM 提供的依據(jù) Python DB API version 2.0 中 API 使用規(guī)定而開發(fā)的數(shù)據(jù)庫訪問接口。dmPython 實現(xiàn)這些 API,使 Python 應用程序能夠對 DM 數(shù)據(jù)庫進行訪問。
dmPython 通過調(diào)用 DM DPI 接口完成 python 模塊擴展。在其使用過程中,除 Python 標準庫以外,還需要 DPI 的運行環(huán)境。
第一步:使用源碼包方式安裝
進入達夢數(shù)據(jù)庫安裝目錄下的 dmPython 目錄,執(zhí)行命令 python setup.py install
注意:前提需要你有C++環(huán)境,Visual Studio。


出現(xiàn)上面這些信息代表安裝成功。
第二步:配置dpi環(huán)境變量

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

第四步:復制操作
將達夢數(shù)據(jù)庫安裝目錄中的drivers/dpi下的所有文件復制到D:\python\python3.9\Lib\site-packages\dmPython-2.4.5-py3.9-win-amd64.egg下。
第五步:編寫python查詢達夢數(shù)據(jù)庫代碼進行測試
# coding:utf-8
import dmPython
try:
# 創(chuàng)建達夢數(shù)據(jù)庫連接
conn = dmPython.connect(user='TEST', password='abc123456', server='localhost',
port=5236)
# 創(chuàng)建數(shù)據(jù)庫操作對象
cursor = conn.cursor()
# try:
# # 清空表,初始化測試環(huán)境
# cursor.execute('delete from T2')
# except (dmPython.Error, Exception) as err:
# print(err)
try:
# 插入數(shù)據(jù)
# 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!')
# # 更新數(shù)據(jù)
# cursor.execute("update DMHR.EMPLOYEE set EMPLOYEE_NAME = '劉強東' where EMPLOYEE_ID = 1157")
# print('python: update success!')
# 查詢數(shù)據(jù)
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!')
# # 刪除數(shù)據(jù)
# 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)到此這篇關于Python連接達夢數(shù)據(jù)庫的實現(xiàn)示例的文章就介紹到這了,更多相關Python連接達夢數(shù)據(jù)庫內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
通過Jython調(diào)用Python腳本的實現(xiàn)方法
Jython 是 Python 的純 Java 實現(xiàn)。她無縫地結合了 Java 類與 Python,使用戶能以 Python 語言的語法編寫在 Java 虛擬機上運行的 軟件,本文重點給大家介紹通過Jython調(diào)用Python腳本的實現(xiàn)方法,一起看看吧2021-06-06

