官方文檔html
https://www.ibm.com/support/knowledgecenter/SSEPGG_9.5.0/kc_gen/developing-gen1.htmlpython
模塊安裝linux
Windowssql
使用PyCharm安裝:File -> Settings -> Project: xxx -> Project Interpreter數據庫
Windows下手動安裝ide
1.解壓 ibm_db-2.0.7.tar.gz ntx64_odbc_cli.zip 並把 clidriver目錄放到 ibm_db-2.0.7 目錄下post
2.運行 cmd 至 解壓後的目錄 python setup.py install fetch
3.最後在把clidriver目錄放到 %PYTHON_HOME%\Lib\site-packages\ibm_db-2.0.7-py3.5.egg下 spa
Linux3d
使用pip安裝感受有點慢,由於須要去官網下載驅動包,我是提早下好進行手動安裝:
百度雲盤:https://pan.baidu.com/s/1skGhqMD
1. tar -zxvf ibm_db-2.0.7.tar.gz
2. tar -zxvf linuxx64_odbc_cli.tar.gz
3. cd ibm_db-2.0.7
4. python3 install setup.py
安裝成功會有一個提示,告訴你要把clidriver驅動包放到其中一個目錄中:
/usr/local/lib/python3.5/site-packages/clidriver
/usr/local/lib/python3.5/site-packages/ibm_db-2.0.7-py3.5-linux-x86_64.egg/clidriver
5. mv clidriver/ /usr/local/lib/python3.5/site-packages/ibm_db-2.0.7-py3.5-linux-x86_64.egg/clidriver
代碼示例
import ibm_db # 鏈接字符串 connStr = "DATABASE=MSJ;HOSTNAME=13.13.13.123;PORT=50000;PROTOCOL=TCPIP;UID=username;PWD=password;" conn = None try: # 鏈接數據庫 conn = ibm_db.connect(connStr, "", "") # 以插入語句爲例,刪除和更新只須要替換語句便可 sql = "insert into tab_student values('%s', '%s')" % (1, "Jet") # 執行SQL語句 stmt = ibm_db.exec_immediate(conn, sql) # 獲取受影響的行數 rows = ibm_db.num_rows(stmt) except Exception as ex: print(ex) finally: # 關閉數據庫 ibm_db.close(conn)
import ibm_db # 鏈接字符串 connStr = "DATABASE=MSJ;HOSTNAME=13.13.13.123;PORT=50000;PROTOCOL=TCPIP;UID=username;PWD=password;" conn = None try: # 鏈接數據庫 conn = ibm_db.connect(connStr, "", "") # 關閉自動提交 ibm_db.autocommit(conn, ibm_db.SQL_AUTOCOMMIT_OFF) # 以插入語句爲例,刪除和更新只須要替換語句便可 sql = "insert into tab_student values('%s', '%s')" % (1, "Jet") # 執行SQL語句 stmt = ibm_db.exec_immediate(conn, sql) # 獲取受影響的行數 rows = ibm_db.num_rows(stmt) # 提交事務 ibm_db.commit(conn) except Exception as ex: # 回滾事務 ibm_db.rollback(conn) finally: # 關閉數據庫鏈接 ibm_db.close(conn)
import ibm_db # 鏈接字符串 connStr = "DATABASE=MSJ;HOSTNAME=13.13.13.123;PORT=50000;PROTOCOL=TCPIP;UID=username;PWD=password;" conn = None try: # 鏈接數據庫 conn = ibm_db.connect(connStr, "", "") # -------------------執行語句有兩種方式-----------------------# # 一、採用當即執行的方式,不支持 ? 佔位符,只能採起拼接字符串的方式,且字符串類型的數據必定要有單引號 sql = "SELECT * FROM TAB_USER WHERE MOBILE = '%s';" % "13911111111" stmt = ibm_db.exec_immediate(conn, sql) result = ibm_db.fetch_both(stmt) # 提取結果 print(result) # 二、採用預編譯的方式,支持 ? 佔位符 sql = "SELECT * FROM TAB_USER WHERE MOBILE= ?" sql_args = ("13911111111",) stmt = ibm_db.prepare(conn, sql) # 預編譯 ibm_db.execute(stmt, sql_args) # 執行 result = ibm_db.fetch_row(stmt) print(result) # -------------------提取查詢結果有四種方式-----------------------# # 1.先調用fetch_row(stmt),有結果返回true不然返回false,再調用ibm_db.result(stmt, col_idx)提取結果 # ibm_db.fetch_row(stmt) # result = ibm_db.result(stmt, 0) # 2.調用fetch_both(stmt)提取一行,返回的結果是一個字典,取值能夠用列名或索引 result["MOBILE"]或 result[0] # result = ibm_db.fetch_both(stmt) # 3.調用fetch_assoc(stmt)提取一行,返回的是一個字典,取值只能用列明 result["MOBILE"] # result = ibm_db.fetch_assoc(stmt) # 4.調用fetch_tuple(stmt)提取一行,返回的是一個元組,取值只能用索引 result[0] # result = ibm_db.fetch_tuple(stmt) # 注: ibm_db沒辦法一次提取多行,提取多行數據,須要循環提取 except Exception as ex: print(ex) finally: ibm_db.close(conn)