Python Uses ibm_db connect to DB2

发布时间:2020-08-07 09:55:23 作者:zchbaby2000
来源:ITPUB博客 阅读:193

安装好了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)

推荐阅读:
  1. Python十几行代码获取db库新增的poc
  2. Python重写Logstash,把Nginx Access Log清洗后汇入Elastic DB

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

db2 connect to

上一篇:Python中的算术运算符的案例分析

下一篇:如何设置php文件权限

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》