使用python操做oracle數據庫,獲取表的某幾個字段做爲變量值使用。html
使用Popen
+sqlplus
的方法須要對格式進行控制,經過流獲取這幾個字段值不簡潔(我的觀點……)。(優勢是可以使用sqlplus
的方法直接訪問sql文件,不須要考慮打開/關閉鏈接,而且經過流向文件中寫入還挺好用的。不過優勢不是此次所關注的)python
使用cx-Oracle
將查詢結果返回爲tuple
格式,對返回結果的操做簡潔,知足需求。(要注意數據庫鏈接建立與關閉、sql的編寫、預處理與提交等等,看起來也不簡潔(一樣我的觀點……))sql
一、使用tns
串鏈接數據庫
oracle_tns = cx_Oracle.makedsn('XXX.XXX.XXX.XXX', 1521,'oracleName') connectObj = cx_Oracle.connect('oracleUserName', 'password', oracle_tns)
二、其餘簡潔方式oracle
db = cx_Oracle.connect('hr', 'hrpwd', 'localhost:1521/XE')
db1 = cx_Oracle.connect('hr/hrpwd@localhost:1521/XE')app
connectObj.close()
cursorObj = connectObj.cursor()
cursorObj.close()
一、單條插入:工具
sql = "INSERT INTO T_AUTOMONITOR_TMP(point_id) VALUES(:pointId)" cursorObj.prepare(sql) rown = cursorObj.execute(None, {'pointId' : pointId}) connectObj.commit()
二、多條插入:code
sql = "INSERT INTO T_AUTOMONITOR_TMP(point_id) VALUES(:pointId)" cursorObj.prepare(sql) rown = cursorObj.executemany(None, recordList) connectObj.commit()
sql = "DELETE FROM T_AUTOMONITOR_TMP t WHERE t.point_id = :pointId " cursorObj.prepare(sql) rown = cursorObj.execute(None, {'pointId' : pointId}) connectObj.commit()
sql = "UPDATE t_automonitor_other t\ SET t.active = '2'\ WHERE t.active = '1'\ AND t.point_id = :pointId\ " cursorObj.prepare(sql) cursorObj.execute(None, {'pointId' : pointId}) connectObj.commit()
sql = "SELECT t.describ FROM t_automonitor_tmp t WHERE t.point_id = :pointId" cursorObj.prepare(sql) cursorObj.execute(None, {'pointId' : pointId})
增、刪、改操做都須要當前鏈接進行commit()
htm
若使用一個遊標cursor
進行N次查詢,注意若再使用前N-1次查詢結果可能會存在異常。要進行多個查詢,我的建議使用完cursor
後將結果保留再關閉cursor
,屢次查詢重複該操做。ip
若是不使用prepare
,能夠直接使用execute
,如下查詢等價:
r1 = cursor.execute('SELECT * FROM locations WHERE country_id=:1 AND city=:2', ('US', 'Seattle'))
r2 = cursor.execute('SELECT * FROM locations WHERE country_id=:9 AND city=:4', ('US', 'Seattle'))
r3 = cursor.execute('SELECT * FROM locations WHERE country_id=:m AND city=:0', ('US', 'Seattle'))
sql語句
的語法與數據庫有關,不想使用綁定變量,能夠拼接sql字符串 (´•༝•`)
class baseUtilsX(): """baseUtils""" def __init__(self): self.connectObj = "" self.connCnt = 0 self.cursorCnt = 0 def initOracleConnect(self): oracle_tns = cx_Oracle.makedsn('XXX.XXX.XXX.XXX', 1521,'XX') if self.connCnt == 0: self.connectObj = cx_Oracle.connect('oracleUserName', 'password', oracle_tns) self.connCnt += 1 def getOracleConnect(self): self.initOracleConnect() return self.connectObj def closeOracleConnect(self, connectObj): connectObj.close() self.connCnt -= 1 def getOracleCursor(self): self.initOracleConnect() self.cursorCnt += 1 return self.connectObj.cursor() def closeOracleCursor(self, cursorObj): cursorObj.close() self.cursorCnt -= 1 if self.cursorCnt == 0: print "will close conn" self.closeOracleConnect(self.connectObj) def selectFromDbTable(self, sql, argsDict): # 將查詢結果由tuple轉爲list queryAnsList = [] selectCursor = self.getOracleCursor() selectCursor.prepare(sql) queryAns = selectCursor.execute(None, argsDict) for ansItem in queryAns: queryAnsList.append(list(ansItem)) self.closeOracleCursor(selectCursor) return queryAnsList