mysql oracle python连接


#encoding:utf-8

#dataProc

#auth xiajikun

import sys

# oracle库连接模块

import cx_Oracle

# mysql库连接

import MySQLdb

import time

import os


#水电煤库

# SDM = 'username/password@ip: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


你可能感兴趣的:(oracle,mysql,python连接)