python操作db2和mysql ,ibm_db

我需要提取mysql和db2的数据进行对比,所以需要用python对其都进行操作。

 

python对mysql进行操作应该没什么问题,就是安装drive后就可以了,在上一篇中有讲安装python-mysql的包即可。。。

python操作db2,我查了有两种方法,一个是DB2的包,一个是ibm_db的包,在我安装db2后,没有找到DB2的包,但是自动安装了ibm_db的包,所以我就选择了直接import ibm_db

这里附上一些ibm_db的操作方法 https://www-01.ibm.com/support/knowledgecenter/SSEPGG_9.5.0/com.ibm.db2.luw.apdv.python.doc/doc/r0054401.html

 

import ibm_db
import MySQLdb

try:
    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])

  

posted @ 2015-10-29 18:43  juandx  阅读(8714)  评论(0编辑  收藏  举报