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

  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM