python 查询表数据以字典展示,将表字段作为key

import cx_Oracle as cx

class OraclePy():
    def __init__(self,userName,password,ip,host,SID):
        self.userName = userName
        self.password = password
        self.host = ip
        # self.con = cx.connect(userName,password,host)
        self.con = cx.connect(userName, password, cx.makedsn(ip, host, SID))
        self.cursor = self.con.cursor()

    def getData(self,sql,offlineTranAmount,onlineTranAmount):
        self.cursor.execute(sql)
        col = []
        resultSets = []
for i in self.cursor.description:
            col.append(i[0])
        for data in self.cursor.fetchall():
            list2 = (list(data))
            resultSets.append(dict(map(lambda x, y: [x, y], col, list2)))
     return resultSets
if __name__ == '__main__':
print(OraclePy('QAMODIFY','ubyledc6','192.168.14.143',1530,'isdb').getDefultRate('ol_alipay_rate'))

 注:使用cx_Oracle.makedsn连接oracle数据库时,如果用Service name不用SID,应该如下传参。

      dns_tns=cx_Oracle.makedsn('host',port,service_name='service_name')

原文地址:https://www.cnblogs.com/wsy0202/p/13275207.html