>> import ibm_db >>> ibm_db.__version__ '2.0.9' >>> ============================..."/>
溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

Python Uses ibm_db connect to DB2

發(fā)布時(shí)間:2020-08-07 09:55:23 來(lái)源:ITPUB博客 閱讀:192 作者:zchbaby2000 欄目:數(shù)據(jù)庫(kù)

安裝好了Python和ibm_db以后,做了幾個(gè)小例子測(cè)試一下

>>> 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)

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI