cx_Oracle 操作oracle数据库

class MyOracle():
    def __init__(self, host_name="ip", port=1521, sid='sid', username='username', password='password'):
        self.hostname = host_name
        self.port = port
        self.sid = sid
        self.username = username
        self.password = password

    def get_connection(self):
        try:
            # tnsname = cx_Oracle.makedsn(self.hostname, self.port, service_name='service_name')  #service_name连接数
            tnsname = cx_Oracle.makedsn(self.hostname, self.port, self.sid) #sid连接数
            self.con = cx_Oracle.connect(self.username, self.password, tnsname)
        except Exception as e:
            print('连接数据库出错:', e)

    def get_cur(self):
        self.cur = self.con.cursor()

    def excute_one(self, sql):
        try:
            self.cur.execute(sql)
            line = self.cur.fetchone()
        except Exception as e:
            print('数据库查询出错:', e)
            self.cur_close()
            self.con_close()
        # cur.prepare('select * from t_emp a where a.empid=:id')
        # cur.execute(None,{'id':id})
        return line

    def excute_some(self, sql):
        try:
            self.cur.execute(sql)
            lines = self.cur.fetchall()
        except Exception as e:
            print('数据库查询出错:', e)
            self.cur_close()
            self.con_close()
        return lines

    def cur_close(self):
        self.cur.close()

    def con_close(self):
        self.con.close()
01-12 20:37