python如果想連接達(dá)夢數(shù)據(jù)庫,必須要安裝dmPython。
簡介:dmPython 是 DM 提供的依據(jù) Python DB API version 2.0 中 API 使用規(guī)定而開發(fā)的數(shù)據(jù)庫訪問接口。dmPython 實(shí)現(xiàn)這些 API,使 Python 應(yīng)用程序能夠?qū)?DM 數(shù)據(jù)庫進(jìn)行訪問。
dmPython 通過調(diào)用 DM DPI 接口完成 python 模塊擴(kuò)展。在其使用過程中,除 Python 標(biāo)準(zhǔn)庫以外,還需要 DPI 的運(yùn)行環(huán)境。
第一步:使用源碼包方式安裝
進(jìn)入達(dá)夢數(shù)據(jù)庫安裝目錄下的 dmPython 目錄,執(zhí)行命令 python setup.py install
注意:前提需要你有C++環(huán)境,Visual Studio。
出現(xiàn)上面這些信息代表安裝成功。?
第二步:配置dpi環(huán)境變量
文章來源:http://www.zghlxwxcb.cn/news/detail-603240.html
第三步:3.8及以上版本需操作
第四步:復(fù)制操作
將達(dá)夢數(shù)據(jù)庫安裝目錄中的drivers/dpi下的所有文件復(fù)制到D:\python\python3.9\Lib\site-packages\dmPython-2.4.5-py3.9-win-amd64.egg下。文章來源地址http://www.zghlxwxcb.cn/news/detail-603240.html
第五步:編寫python查詢達(dá)夢數(shù)據(jù)庫代碼進(jìn)行測試
# coding:utf-8
import dmPython
try:
# 創(chuàng)建達(dá)夢數(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 = '劉強(qiáng)東' 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)
到了這里,關(guān)于Python連接達(dá)夢數(shù)據(jù)庫的文章就介紹完了。如果您還想了解更多內(nèi)容,請?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!