Python operation DB2 and MySQL, ibm_db

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.