mysql oracle python串連

來源:互聯網
上載者:User

標籤:mysql oracle python串連


#encoding:utf-8

#dataProc

#auth xiajikun

import sys

# oracle庫串連模組

import cx_Oracle

# mysql庫串連

import MySQLdb

import time

import os


#水電煤庫

# SDM = ‘username/[email protected]:port/servicename‘

DB_ORA_STR = ‘admin/[email protected]:1521/c9db1111‘


# oracle串連

class OracleLogin(object):

    #初始化,建立串連資料庫物件  

    def __init__(self, loginName):

        #--建立串連

        self.conn = cx_Oracle.connect(loginName)

        #--擷取遊標        

        self.cursor = self.conn.cursor()

        self.l_v_status = self.cursor.var(cx_Oracle.NUMBER)

        self.l_v_return_code = self.cursor.var(cx_Oracle.STRING)

        self.l_v_sno = self.cursor.var(cx_Oracle.STRING)

    #查詢sql方法

    def selectSql(self, sql):

        self.cursor.execute(sql)

        self.result_sql = self.cursor.fetchall()

        self.count = self.cursor.rowcount

        self.conn.commit()

    #執行sql方法

    def execSql(self,sql):

        self.cursor.execute(sql)

        self.conn.commit()

    #執行預存程序方法

    def getMetaData(self,procdName, sql, procArgs):

        self.procResult = self.cursor.callproc(procdName, procArgs)

        self.cursor.execute(sql)

        self.result_pro = self.cursor.fetchall()

        self.count = self.cursor.rowcount

        self.conn.commit()

    #無用時自動析構此對象

    def __del__(self):

        self.cursor.close()

        self.conn.close()



# mysql串連

class MysqlLogin(object):

    #初始化,自動連接資料庫

    def __init__(self, db_str):

        # self.conn = MySQLdb.connect(host=‘10.7.11.242‘,user=‘credcard‘,passwd=‘5sFVDVCeKo‘,db=‘credcard‘,port=3320)

        self.conn = MySQLdb.connect(host=db_str[0],user=db_str[1],passwd=db_str[2],db=db_str[3],port=db_str[4])


        #self.conn = MySQLdb.connect(‘%s‘ % db_str)

        #self.conn = MySQLdb.connect(‘%s,%s,%s,%s,%d‘ % (db_str)

        self.cur = self.conn.cursor()

    def exec_sql(self, sql):

        self.cur.execute(sql)

        self.conn.commit()

    def select_sql(self, sql):

        self.cur.execute(sql)

        self.tmpdata = self.cur.fetchall()

    #自動斷開遊標和串連

    def __del__(self):

        self.cur.close()

        self.conn.close()



db_str = (‘10.1.1.1‘,‘username‘,‘123456‘,‘db_name‘,3306)

db_obj = MysqlLogin(db_str)

sql = ‘select * from tabname limit 10‘

db_obj.select_sql(sql)

print db_obj.tmpdata


mysql oracle python串連

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.