參考地址:https://github.com/OpenInformix/IfxPy
1. 下載安裝 Informix Client SDK
地址:鏈接:https://pan.baidu.com/s/1CXMkwUnhRl4StrhPJ4n5fw
提取碼:
解壓,執行可執行文件即可
2. 安裝IfxPy模塊
pip install ifxpy
pip若無法安裝,可選擇離線安裝,下載安裝包,執行:python setup.py install 即可
3. 設置環境變量
export LD_LIBRARY_PATH=${INFORMIXDIR}/lib:${INFORMIXDIR}/lib/esql:${INFORMIXDIR}/lib/cli
以下是我的環境變量:
export LD_LIBRARY_PATH=/opt/IBM/Informix_Client-SDK/lib:/opt/IBM/Informix_Client-SDK/lib/esql:/opt/IBM/Informix_Client-SDK/lib/cli
4. 環境配置
vim /etc/hosts:
ip 服務名
vim sqlhosts:
服務名 onsoctcp 服務名 服務名 g=g_服務名
export INFORMIXSQLHOSTS=/data/sqlhosts
vim /etc/services
服務名 27583/tcp
導庫報錯:ImportError: libifgls.so: cannot open shared object file: No such file or directory
原因:環境變量沒有配對
5. 連接informix數據庫
# Sample1.py import IfxPy def my_Sample(): ConStr = "SERVER=服務器;DATABASE=庫名;HOST=ip;SERVICE=端口;UID=用戶名;PWD=密碼;" try: # netstat -a | findstr 27583 conn = IfxPy.connect( ConStr, "", "") except Exception as e: print ('ERROR: Connect failed') print ( e ) quit() # Select records sql = "select tabname from systables where tabid>=100;" # 查庫下的非系統表 # sql = "SELECT username FROM 'informix'.sysusers WHERE usertype IN ('D', 'R');" stmt = IfxPy.exec_immediate(conn, sql) dictionary = IfxPy.fetch_both(stmt) rc = 0 while dictionary != False: rc += 1 print (dictionary) dictionary = IfxPy.fetch_both(stmt) print( "Total Record Selected {}".format(rc) ) # Free up memory used by result and then stmt too IfxPy.free_result(stmt) IfxPy.free_stmt (stmt) IfxPy.close(conn) print ("Done") ####### Run the sample function ###### my_Sample()