ITPub博客

首页 > 数据库 > 数据库开发技术 > Python Uses ibm_db connect to DB2

Python Uses ibm_db connect to DB2

原创 数据库开发技术 作者:zchbaby2000 时间:2018-10-11 15:18:15 0 删除 编辑

安装好了Python和ibm_db以后,做了几个小例子测试一下

>>> import ibm_db
>>> ibm_db.__version__
'2.0.9'
>>>

=======================================================================

#!/usr/bin/python
#Example 1: Connect to a local or cataloged database
import ibm_db

conn = ibm_db.connect( "dsn=SAMPLE", "db2inst1", "dbpwd" )
sql = "SELECT QUOTEID,GEO FROM DB2INST1.TRN fetch first 10 rows only with ur"
stmt = ibm_db.exec_immediate(conn, sql)
dictionary = ibm_db.fetch_both(stmt)
while dictionary != False:
    print(dictionary["QUOTEID"])
    print(dictionary["GEO"])
    dictionary = ibm_db.fetch_both(stmt)

=======================================================================
#!/usr/bin/python
Example 2: Connect to an uncataloged database
import ibm_db
conn = ibm_db.connect("DATABASE=SAMPLE;HOSTNAME=192.168.101.133;PORT=60006;PROTOCOL=TCPIP;UID=db2inst1;PWD=dbpwd;", "", "")
sql = "SELECT QUOTEID,GEO FROM DB2INST1.TRN fetch first 10 rows only with ur"
stmt = ibm_db.exec_immediate(conn, sql)
dictionary = ibm_db.fetch_both(stmt)
while dictionary != False:
    print(dictionary["QUOTEID"])
    print(dictionary["GEO"])
    dictionary = ibm_db.fetch_both(stmt)

=========================================================================

#!/usr/bin/python
#Example 3: Connect to an uncataloged database with SSL enabled
import ibm_db
conn = ibm_db.connect("DATABASE=SAMPLE;HOSTNAME=192.168.101.133;PORT=50001;SECURITY=SSL;UID=db2inst1;PWD=dbpwd;SSLServerCertificate=/home/db2inst1/db2_ssl/db2_ssl_keydb.arm;", "", "")
sql = "SELECT QUOTEID,GEO FROM DB2INST1.TRN fetch first 10 rows only with ur"
stmt = ibm_db.exec_immediate(conn, sql)
dictionary = ibm_db.fetch_both(stmt)
while dictionary != False:
    print(dictionary["QUOTEID"])
    print(dictionary["GEO"])
    dictionary = ibm_db.fetch_both(stmt)


来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/725820/viewspace-2216026/,如需转载,请注明出处,否则将追究法律责任。

上一篇: AIX的yum安装
下一篇: 一次DB2 DR演练
请登录后发表评论 登录
全部评论
混迹IT 15年,喜欢Java开发,喜欢Python,还喜欢数据库的开发,架构设计,运维和性能优化
  • 博文量
    218
  • 访问量
    49866