Python Uses ibm_db connect to DB2

zchbaby2000發表於2018-10-11

安裝好了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/,如需轉載,請註明出處,否則將追究法律責任。

相關文章