Permalink
Browse files

added multiple instances variables to connect to, SAMPLE database can…

… be viewed as well
  • Loading branch information...
1 parent ef32a2b commit 35b1188b557092a7a228f88bf4ac8ec2bc1593f0 @remyb committed Feb 8, 2010
Showing with 27 additions and 11 deletions.
  1. +27 −11 main.py
View
38 main.py
@@ -1,11 +1,13 @@
import ibm_db
# connect to database
-conn = ibm_db.connect() # insert here with your connection info
+conn = ibm_db.connect('SAMPLE','db2inst1','password') # insert here with your connection info
+db1 = ibm_db.connect('ASSIGN01','db2inst1','password') # insert here with your connection info
+db2 = ibm_db.connect('ASSIGN02','db2inst1','password') # insert here with your connection info
#show stats on database
-def client_print():
- client = ibm_db.client_info(conn)
+def client_print(db):
+ client = ibm_db.client_info(db)
print "DRIVER_NAME: string(%d) \"%s\"" % (len(client.DRIVER_NAME), client.DRIVER_NAME)
print "DRIVER_VER: string(%d) \"%s\"" % (len(client.DRIVER_VER), client.DRIVER_VER)
print "DATA_SOURCE_NAME: string(%d) \"%s\"" % (len(client.DATA_SOURCE_NAME), client.DATA_SOURCE_NAME)
@@ -15,27 +17,41 @@ def client_print():
print "APPL_CODEPAGE: int(%s)" % client.APPL_CODEPAGE
print "CONN_CODEPAGE: int(%s)" % client.CONN_CODEPAGE
-# Assumes the table does not exist
-def create_table():
+
+def create_table(db):
try:
stmt = ibm_db.exec_immediate(db,"CREATE TABLE BOOKS(isbn char(14), title char(80), price decimal);")
except:
- print "The table probably already exists: " , ibm_db.stmt_errormsg()
+ print "The table probably already exists: \n----------\n" , ibm_db.stmt_errormsg()
else:
print "Successfully Created Table!"
-
-def drop_table():
+
+
+def drop_table(db):
try:
stmt = ibm_db.exec_immediate(db,"DROP TABLE BOOKS")
except:
- print "There may be nothing to drop! :", ibm_db.stmt_errormsg()
+ print "There may be nothing to drop! \n-----------\n:", ibm_db.stmt_errormsg()
else:
print "Successfully Dropped Table!"
+# works on SAMPLE database created with db2sampl program
+def print_table():
+ sql = "SELECT * FROM DEPARTMENT"
+ stmt = ibm_db.exec_immediate(conn,sql)
+ dictionary = ibm_db.fetch_assoc(stmt)
+ while dictionary != False:
+ print '==================='
+ print "Department #: ",dictionary["DEPTNO"]
+ print "Department Name: ",dictionary["DEPTNAME"]
+ print "Department Location: ",dictionary["LOCATION"]
+ dictionary = ibm_db.fetch_assoc(stmt)
-client_print()
-#create_table()
+client_print(db1)
+#print_table()
+create_table(db1) # going to thread these
+create_table(db2)
#drop_table()
ibm_db.close(conn)

0 comments on commit 35b1188

Please sign in to comment.