Python3-ibm_db模块-数据库操作之DB2
官方文档
https://www.ibm.com/support/knowledgecenter/SSEPGG_9.5.0/kc_gen/developing-gen1.html
模块安装
Windows
使用PyCharm安装:File -> Settings -> Project: xxx -> Project Interpreter
Windows下手动安装
1.解压 ibm_db-2.0.7.tar.gz ntx64_odbc_cli.zip 并把 clidriver目录放到 ibm_db-2.0.7 目录下
2.运行 cmd 至 解压后的目录 python setup.py install
3.最后在把clidriver目录放到 %PYTHON_HOME%\Lib\site-packages\ibm_db-2.0.7-py3.5.egg下
Linux
使用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, "", "") # -------------------执行语句有两种方式-----------------------# # 1、采用立即执行的方式,不支持 ? 占位符,只能采取拼接字符串的方式,且字符串类型的数据一定要有单引号 sql = "SELECT * FROM TAB_USER WHERE MOBILE = '%s';" % "13911111111" stmt = ibm_db.exec_immediate(conn, sql) result = ibm_db.fetch_both(stmt) # 提取结果 print(result) # 2、采用预编译的方式,支持 ? 占位符 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)