1. 測試環境
在Window中使用Python 2.7.5版本以及DM8資料庫進行增删改查,以下是踩坑筆記。
軟體 | 版本 |
---|---|
DM 資料庫 | DM 8.0 及以上版本 |
Python | Python 2.7.5 |
Window10專業版 | Window10專業版 |
注意:實施過程中遇到報錯一定要看Q&A中有沒有你的問題的解決方案。
2. 步驟
2.1 安裝Python
使用者請自行下載下傳安裝 Python。安裝的 Python 2.7.5 版本
2.2 初始化dmPython
- dmPython在達夢資料庫的安裝目錄下面,路徑為【資料庫安裝路徑】/drivers/python(我的安裝路徑是D:\damba\dm8\drivers\python\dmPython)
- 進入到dmPython中 編譯,執行以下指令
python setup.py install
如圖所示即為安裝成功。
如果出現如下提示
error: Microsoft Visual C++ 9.0 is required.
請參考【Q&A】中的解決方案
2.3 驗證dmPython
如果出現如下則說明安裝成功
2.5 測試是否連接配接成功
# !/usr/bin/python
# coding:utf-8
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)
如果所示,即為成功。
如是下圖:
如果提示
ImportError: DLL load failed: 找不到指定的子產品。
請到【Q&A】中檢視
2.6 增删改查
#!/usr/bin/python
# coding:utf-8
import dmPython
try:
conn = dmPython.connect(user='SYSDBA', password='SYSDBA', server='localhost', port=5236)
cursor = conn.cursor()
try:
# 清空表,初始化測試環境
cursor.execute('delete from PRODUCTION.PRODUCT_CATEGORY')
except (dmPython.Error, Exception) as err:
print(err)
try:
# 插入資料
cursor.execute("insert into PRODUCTION.PRODUCT_CATEGORY(NAME) values('1'), ('2'), ('3'), ('4')")
print('python: insert success!')
# 删除資料
cursor.execute("delete from PRODUCTION.PRODUCT_CATEGORY where name='1'")
print('python: delete success!')
# 更新資料
cursor.execute('update PRODUCTION.PRODUCT_CATEGORY set name = \'222\' where name=\'2\'')
print('python: update success!')
# 查詢資料
cursor.execute("select name from PRODUCTION.PRODUCT_CATEGORY")
res = cursor.fetchall()
for tmp in res:
for c1 in tmp:
print c1
print('python: select success!')
except (dmPython.Error, Exception) as err1:
print (err1)
conn.close()
except (dmPython.Error, Exception) as err:
print(err)
Q&A
1. pip安裝三方庫不成功
提示:WARNING: You are using pip version 20.2.3, however version 20.2.4 is available.
原因:pip版本過低導緻安裝第三方庫失敗
## 直接輸入下面指令解決
python -m pip install --upgrade pip
2. error: Microsoft Visual C++ 9.0 is required
原因:包編譯的時候缺少C++的編譯環境,是以隻需要安裝即可,安裝網盤連結中
VCForPython27.msi
檔案。下載下傳完成後輕按兩下安裝
3. 控制台亂碼
修改Pycharm的編碼,window預設是gbk。
解決方案
4.ImportError: DLL load failed: 找不到指定的子產品
調用達夢資料庫時需要調用DM資料庫的一些DLL檔案,因為找不到是以報錯了。
解決方案
在項目啟動時,在
Environment variables
中配置環境變量
注意:D:\damba\dm8是我本地dm的安裝路徑,需要替換成自己的。
PYTHONUNBUFFERED=1;PATH=PATH=D:\damba\dm8\drivers\\;D:\damba\dm8\drivers\logmnr\\;D:\damba\dm8\bin
資料
連結:https://pan.baidu.com/s/1Xv8xmy83VbA42FVk7_NtPA 提取碼:b2u8
=======================================
有任何問題請到技術社群回報。
24小時免費服務熱線:400 991 6599
達夢技術社群:https://eco.dameng.com