python3使用cx_Oracle连接oracle时,怎么传参sid或service_name
使用cx_Oracle.makedsn连接oracle数据库时,如果用Service name不用SID,应该如下传参。
dns_tns=cx_Oracle.makedsn('host',port,service_name='service_name')
如果用SID不用Service name,应该如下传参。
dns_tns=cx_Oracle.makedsn('host',port,'sid_info')
或
dns_tns=cx_Oracle.makedsn('host',port,sid='sid_info')
import cx_Oracle as cx from quartz.comm.config import confSql class OraclePy(): def __init__(self,userName,password,host,port,connType,service_name): # 标识是根据SID还是SERVICE_NAME连接 self.userName = userName self.password = password self.host = host self.port = port if (connType == 'SID'): self.con = cx.connect(self.userName, self.password, cx.makedsn(self.host, self.port, service_name)) self.cursor = self.con.cursor() elif(connType == "SERVICE_NAME"): dns_tns = cx.makedsn(self.host, self.port, service_name=service_name) self.con = cx.connect(self.userName,self.password,dns_tns) self.cursor = self.con.cursor() else:print("不支持的数据库连接方式,当前只支持SID和SERVICE_NAME") # 批量执行增删改sql def executeSqls(self,sqls): for sql in sqls: self.cursor.execute(sql) self.con.commit() # self.cursor.close # self.con.close # 更新交易表 def updateTxnCtrl(self,sql,idTxn): sql = sql.replace("IDTXN",str(idTxn)) self.cursor.execute(sql) self.con.commit() def updateTxnCtrl1(self,sql,tunnelValue,idTxn): sql = sql.replace('tunnelValue',str(tunnelValue)).replace("IDTXN",str(idTxn)) self.cursor.execute(sql) self.con.commit() # self.cursor.close # self.con.close # 更新交易表 def updateTxnCtrl2(self, sql,value1 , value2, idTxn): sql = sql.replace('tunnelValue1', str(value1)).replace('tunnelValue2', str(value2)).replace("IDTXN",str(idTxn)) self.cursor.execute(sql) self.con.commit() # self.cursor.close # self.con.close # 查询清分明细表 def ClrTxnListQuery(self,sql,idTxn): sql = sql.replace('idTxn',str(idTxn)) 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 def T_RPT_INAMT(self,sql): 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 # 关闭数据连接 def disConnection(self): self.cursor.close self.con.close if __name__ == '__main__': #conn = OraclePy('partner','partner','192.168.127.121',1530,'SID','billdb') conn = OraclePy('QAMODIFY', 'bfjwc0qocjp0dlgf', '192.168.6.42', 1530, 'SERVICE_NAME', 'vposdbtaf') sql = "select t.* from expt t " TrptInamt = conn.T_RPT_INAMT(sql) print(len(TrptInamt)) print(TrptInamt) conn.disConnection()