MENU
依赖的库
pip install cx_Oracle
关于oracle的配置
userName = 'usr_name'
password = 'your_pass_word'
host = '[your_ip_address]:1521'
instance = 'xxdb'
代码
import cx_Oracle


class Oracle():

    def __init__(self, userName, password, host, instance):
        self._conn = cx_Oracle.connect('%s/%s@%s/%s' % (userName, password, host, instance))
        self.cursor = self._conn.cursor()

    # 返回列名
    def queryTitle(self, sql, nameParams={}):
        if len(nameParams):
            self.cursor.execute(sql, nameParams)
        else:
            self.cursor.execute(sql)

        colNames = []
        for i in range(0, len(self.cursor.description)):
            colNames.append(self.cursor.description[i][0])

        return colNames

    # 提交
    def commit(self):
        self._conn.commit()

    # 批量插入
    def insertBatch(self, sql, nameParams=[]):
        self.cursor.prepare(sql)
        self.cursor.executemany(None, nameParams)
        self.commit()

    # 查询1条
    def queryOne(self, sql):
        self.cursor.execute(sql)
        return self.cursor.fetchone()

    # 查询所有
    def queryAll(self, sql):
        self.cursor.execute(sql)
        return self.cursor.fetchall()

    def __del__(self):
        if hasattr(self, 'cursor'):
            self.cursor.close()

        if hasattr(self, '_conn'):
            self._conn.close()