Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100755 107 lines (85 sloc) 3.149 kb
eee166a Remy added Threading for remote connections and real configuration parsing an...
authored
1 #!/usr/bin/python
ef32a2b Remy I added some sample functions to interact with the DB2 instance
authored
2
eee166a Remy added Threading for remote connections and real configuration parsing an...
authored
3 import ibm_db, ConfigParser
4 from threading import Thread
7dc30ba Remy added some sample config reading functions
authored
5
6 # read config and parse
7 def config(section):
8 config = ConfigParser.RawConfigParser()
9 config.read('test.cfg')
10 dict = {}
11 options = config.options(section)
12 for option in options:
13 try:
14 dict[option] = config.get(section, option)
15 if dict[option] == -1:
16 DebugPrint("skip: %s" % option)
17 except:
18 print("exception on %s!" % option)
19 dict[option] = None
20 return dict
ef32a2b Remy I added some sample functions to interact with the DB2 instance
authored
21
22 #show stats on database
35b1188 Remy added multiple instances variables to connect to, SAMPLE database can be...
authored
23 def client_print(db):
24 client = ibm_db.client_info(db)
ef32a2b Remy I added some sample functions to interact with the DB2 instance
authored
25 print "DRIVER_NAME: string(%d) \"%s\"" % (len(client.DRIVER_NAME), client.DRIVER_NAME)
26 print "DRIVER_VER: string(%d) \"%s\"" % (len(client.DRIVER_VER), client.DRIVER_VER)
27 print "DATA_SOURCE_NAME: string(%d) \"%s\"" % (len(client.DATA_SOURCE_NAME), client.DATA_SOURCE_NAME)
28 print "DRIVER_ODBC_VER: string(%d) \"%s\"" % (len(client.DRIVER_ODBC_VER), client.DRIVER_ODBC_VER)
29 print "ODBC_VER: string(%d) \"%s\"" % (len(client.ODBC_VER), client.ODBC_VER)
7dc30ba Remy added some sample config reading functions
authored
30 print "ODBC_SQL_CONFORMANCE: string(%d) \"%s\"" % (len(client.ODBC_SQL_CONFORMANCE), client.ODBC_SQL_CONFORMANCE)
ef32a2b Remy I added some sample functions to interact with the DB2 instance
authored
31 print "APPL_CODEPAGE: int(%s)" % client.APPL_CODEPAGE
32 print "CONN_CODEPAGE: int(%s)" % client.CONN_CODEPAGE
33
35b1188 Remy added multiple instances variables to connect to, SAMPLE database can be...
authored
34
35 def create_table(db):
ef32a2b Remy I added some sample functions to interact with the DB2 instance
authored
36 try:
37 stmt = ibm_db.exec_immediate(db,"CREATE TABLE BOOKS(isbn char(14), title char(80), price decimal);")
38 except:
35b1188 Remy added multiple instances variables to connect to, SAMPLE database can be...
authored
39 print "The table probably already exists: \n----------\n" , ibm_db.stmt_errormsg()
eee166a Remy added Threading for remote connections and real configuration parsing an...
authored
40 return False
ef32a2b Remy I added some sample functions to interact with the DB2 instance
authored
41 else:
42 print "Successfully Created Table!"
35b1188 Remy added multiple instances variables to connect to, SAMPLE database can be...
authored
43
44
45 def drop_table(db):
ef32a2b Remy I added some sample functions to interact with the DB2 instance
authored
46 try:
47 stmt = ibm_db.exec_immediate(db,"DROP TABLE BOOKS")
48 except:
35b1188 Remy added multiple instances variables to connect to, SAMPLE database can be...
authored
49 print "There may be nothing to drop! \n-----------\n:", ibm_db.stmt_errormsg()
ef32a2b Remy I added some sample functions to interact with the DB2 instance
authored
50 else:
51 print "Successfully Dropped Table!"
eee166a Remy added Threading for remote connections and real configuration parsing an...
authored
52
53 # execute query
54 def exec_query(db, query):
55 try:
56 ibm_db.exec_immediate(db,query)
57 except:
58 print "the query could not be completed:\n",query
59 else:
60 print "the query was successful:\n",query
ef32a2b Remy I added some sample functions to interact with the DB2 instance
authored
61
35b1188 Remy added multiple instances variables to connect to, SAMPLE database can be...
authored
62 # works on SAMPLE database created with db2sampl program
63 def print_table():
64 sql = "SELECT * FROM DEPARTMENT"
65 stmt = ibm_db.exec_immediate(conn,sql)
66 dictionary = ibm_db.fetch_assoc(stmt)
67 while dictionary != False:
68 print '==================='
69 print "Department #: ",dictionary["DEPTNO"]
70 print "Department Name: ",dictionary["DEPTNAME"]
71 print "Department Location: ",dictionary["LOCATION"]
72 dictionary = ibm_db.fetch_assoc(stmt)
ef32a2b Remy I added some sample functions to interact with the DB2 instance
authored
73
eee166a Remy added Threading for remote connections and real configuration parsing an...
authored
74 #client_print(db1)
75
76
77 node1 = config('node1')
78 node2 = config('node2')
79 db1 = ibm_db.connect(node1['hostname'], node1['username'],node1['passwd'])
80 db2 = ibm_db.connect(node2['hostname'], node2['username'],node2['passwd'])
81
82 nodes = [db1,db2]
83 querys = ["CREATE TABLE BOOKS(isbn char(14), title char(80), price decimal);","DROP TABLE BOOKS"]
84
85 # foreach db execute the DDL's
86 for query in querys:
87 for node in nodes:
88 Thread(target=exec_query,args=(node,query,)).start()
89
90 for node in nodes:
91 ibm_db.close(node)
92
93 #try:
94 #except Exception, errtxt:
95 #print errtxt
96
97 #thread.start_new_thread(drop_table,(db2))
98
7dc30ba Remy added some sample config reading functions
authored
99 #create_table(db1) # going to thread these
100 #create_table(db2)
eee166a Remy added Threading for remote connections and real configuration parsing an...
authored
101
102
ef32a2b Remy I added some sample functions to interact with the DB2 instance
authored
103 #drop_table()
eee166a Remy added Threading for remote connections and real configuration parsing an...
authored
104 #print_table()
105 #ibm_db.close(conn)
ef32a2b Remy I added some sample functions to interact with the DB2 instance
authored
106
Something went wrong with that request. Please try again.