Python Uses ibm_db connect to DB2
After installing Python and ibm_db, do a few small examples to test
> 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)