千家信息网

Python Uses ibm_db connect to DB2

发表于:2024-11-13 作者:千家信息网编辑
千家信息网最后更新 2024年11月13日,安装好了Python和ibm_db以后,做了几个小例子测试一下>>> import ibm_db>>> ibm_db.__version__'2.0.9'>>>====================
千家信息网最后更新 2024年11月13日Python Uses ibm_db connect to DB2

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

0