I need to extract MySQL and DB2 data for comparison, so I need to use Python to manipulate it.
Python operation on MySQL should be no problem, that is, after the installation drive can be, in the previous article on the installation of Python-mysql package can ...
Python operation DB2, I checked there are two ways, one is the DB2 package, one is the ibm_db package, after I installed DB2, did not find DB2 package, but automatically installed ibm_db package, so I chose the direct import ibm_db
Here are some ibm_db methods of operation https://www-01.ibm.com/support/knowledgecenter/SSEPGG_9.5.0/com.ibm.db2.luw.apdv.python.doc/ Doc/r0054401.html
?
| 123456789101112131415161718192021222324252627282930313233 |
import ibm_dbimport MySQLdbtry: conn1=MySQLdb.connect(host=‘172.16.164.12‘,user=‘mustang‘,passwd=‘mustang‘,port=3306)#连接mysql conn2 = ibm_db.connect("nova","nova","nova")#连接db2 #sql = "SELECT * FROM instances" #stmt = ibm_db.exec_immediate(conn2, sql) #print ibm_db.fetch_assoc(stmt) #print ‘========================================================================\n\n\n‘ conn1.select_db(‘mustang‘) cur1=conn1.cursor() cur1.execute(‘select * from instance‘) results1=cur1.fetchall() for r in results1: # id uuid name is_terminal user_id print r[0], r[1], r[3], r[26], r[30] stmt=ibm_db.exec_immediate(conn2,‘select * from instances‘) r = ibm_db.fetch_both(stmt) while( r ): # id vm_state hostname uuid deleted launched_at print r[3], r[14], r[17], r[32], r[49], r[22] r = ibm_db.fetch_both(stmt) cur1.close() conn1.close() ibm_db.close(conn2)except MySQLdb.Error,e: print "Mysql Error %d: %s" % (e.args[0], e.args[1]) |
Python operation DB2 and MySQL, ibm_db