詳解Python連接oracle的問(wèn)題記錄與解決
昨日晚平臺(tái)升級(jí),我們成功送BUG上線,今天系統(tǒng)問(wèn)題又多了起來(lái),大多數(shù)時(shí)候的運(yùn)維問(wèn)題需要根據(jù)業(yè)務(wù)反饋的#訂單號(hào)# 查詢到當(dāng)前狀態(tài),然后再進(jìn)行反饋和處理。
每次看到運(yùn)維問(wèn)題前都要去打開(kāi)plsql等工具進(jìn)行數(shù)據(jù)庫(kù)查詢,或者登錄到系統(tǒng)里面進(jìn)行查看,然后再進(jìn)行具體的回復(fù)和處理。
以上是需求背景
能否寫(xiě)一個(gè)程序?實(shí)現(xiàn)一步操作把常用的信息都查詢出來(lái),省去打開(kāi)數(shù)據(jù)庫(kù)工具和重復(fù)編寫(xiě)/修改腳本的繁瑣操作。
通過(guò)查找資料,寫(xiě)了一個(gè),碰到不少問(wèn)題,特意記錄下來(lái)。
技術(shù)框架
開(kāi)發(fā)語(yǔ)言:Python,數(shù)據(jù)庫(kù):oracle,第三方庫(kù):cx_Oracle(用于python和oracle的連接),prettytable(用于表格化輸出展示數(shù)據(jù))
開(kāi)發(fā)步驟
一、安裝cx_Oracle
pip install cx_Oracle
參考官方說(shuō)明文檔: cx-oracle.readthedocs.io/en/latest/user_guide/installation.html
二、編寫(xiě)數(shù)據(jù)庫(kù)操作類
直接使用了chatgpt提供的代碼,因?yàn)槲抑挥玫搅瞬樵兎椒?,所以只查沒(méi)有增刪改,另外考慮到要同時(shí)查詢多次數(shù)據(jù),所以自己修改了實(shí)現(xiàn)了一個(gè)連接池的功能。
import cx_Oracle
import queue
class OracleDatabase:
# 構(gòu)造函數(shù),傳入數(shù)據(jù)庫(kù)連接參數(shù)
def __init__(self, user, pwd, dsn, size):
self.user = user
self.pwd = pwd
self.dsn = dsn
## 定義連接池
self.size = size
self.conn_queue = queue.Queue(maxsize=self.size)
for i in range(self.size):
self.conn_queue.put(self._create_connection())
# 創(chuàng)建數(shù)據(jù)庫(kù)連接
def _create_connection(self):
return cx_Oracle.connect(self.user, self.pwd, self.dsn)
# 從連接池里面獲取連接
def _get_conn(self):
conn = self.conn_queue.get()
if conn is None:
self._create_connection()
return conn
# 將連接put到連接池中
def _put_conn(self, conn):
self.conn_queue.put(conn)
# 關(guān)閉所有連接
def _close_conn(self):
try:
while True:
conn = self.conn_queue.get_nowait()
if conn:
conn.close()
except queue.Empty:
print(">>>>數(shù)據(jù)庫(kù)連接全部關(guān)閉<<<<")
pass
# 執(zhí)行查詢語(yǔ)句
def query(self, sql, params=None):
res = []
conn = self._get_conn()
cursor = conn.cursor()
try:
if params:
cursor.execute(sql, params)
else:
cursor.execute(sql)
rows = cursor.fetchall()
for row in rows:
res.append(row)
except Exception as e:
print(str(e))
finally:
cursor.close()
self._put_conn(conn)
return res三、輸入訂單號(hào),執(zhí)行查詢
if __name__ == '__main__':
user = "user_dba"
pwd = "user_password"
dsn = cx_Oracle.makedsn('0.0.0.0', '1521', service_name='s_demo_db')
db = OracleDatabase(user, pwd, dsn, 2)
cl_code = input("輸入訂單號(hào): ").strip()
print("數(shù)據(jù)信息展示:")
sql_1 = """select *
from table_demo c
where c.cl_code = :cl_code"""
results_1 = db.query(sql_1, [cl_code])
print(results_1)
# ......
四、格式化打印
安裝prettytable
pip install PrettyTable
示例代碼
from prettytable import PrettyTable ## 接著第三部分的代碼 tb_1 = PrettyTable(['**號(hào)', '**時(shí)間', '當(dāng)前狀態(tài)', '單號(hào)', '機(jī)構(gòu)']) for rs_1 in results_1: tb_1.add_row([rs_1[0], rs_1[1], rs_1[2], rs_1[3], rs_1[4]]) print(tb_1)
五、打印效果
使用效果如下:粘貼訂單號(hào)回車,直接返回下面所需要的信息數(shù)據(jù)(測(cè)試數(shù)據(jù)):

問(wèn)題記錄
第一個(gè)問(wèn)題就是安裝 cx_Oracle的時(shí)候出錯(cuò):
ERROR: Could not build wheels for cx_Oracle, which is required to install pyproject.toml-based projects
解決方式:安裝Microsoft C++ 生成工具,Microsoft C++ 生成工具 - Visual Studio,更改安裝目錄,按照默認(rèn)選項(xiàng)安裝即可。
報(bào)錯(cuò)信息
cx_Oracle.DatabaseError: DPI-1047: Cannot locate a 64-bit Oracle Client library:"The specified module could not be found".See https://cx-oracle.readthedocs.io/en/latest/user_guide/installation.html for help
解決方式:復(fù)制oracle客戶端(客戶端下載見(jiàn)問(wèn)題3)目錄中的oci,oraocci11,oraociei11的3個(gè)DLL粘貼到你的Paython目錄的Lib/site-packages文件夾下面。
報(bào)錯(cuò)信息
cx_Oracle.DatabaseError: DPI-1072: the Oracle Client library version is unsupported
下載oracle客戶端,并解壓安裝。下載地址:oracle.github.io/odpi/doc/installation 我出現(xiàn)這個(gè)問(wèn)題,是因?yàn)槲冶緳C(jī)原來(lái)安裝的是19.18版本,換成11.2版本的客戶端,按照問(wèn)題2的操作,將三個(gè)dll文件重新復(fù)制過(guò)去,解決問(wèn)題。

后期優(yōu)化
- 將sql語(yǔ)句集中放到配置文件里面,并配置表頭,可以實(shí)現(xiàn)多查詢自由擴(kuò)展。
- 通過(guò)bat腳本調(diào)用執(zhí)行,真正實(shí)現(xiàn)一鍵查詢。
以上就是詳解Python連接oracle的問(wèn)題記錄與解決的詳細(xì)內(nèi)容,更多關(guān)于Python連接oracle的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Python range與enumerate函數(shù)區(qū)別解析
這篇文章主要介紹了Python range與enumerate函數(shù)區(qū)別解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-02-02
python 求某條線上特定x值或y值的點(diǎn)坐標(biāo)方法
今天小編就為大家分享一篇python 求某條線上特定x值或y值的點(diǎn)坐標(biāo)方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-07-07
Pytorch加載部分預(yù)訓(xùn)練模型的參數(shù)實(shí)例
今天小編就為大家分享一篇Pytorch加載部分預(yù)訓(xùn)練模型的參數(shù)實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-08-08
Python使用內(nèi)置json模塊解析json格式數(shù)據(jù)的方法
這篇文章主要介紹了Python使用內(nèi)置json模塊解析json格式數(shù)據(jù)的方法,結(jié)合實(shí)例形式分析了Python使用內(nèi)置的json模塊實(shí)現(xiàn)json格式數(shù)據(jù)的解析、轉(zhuǎn)換相關(guān)操作技巧,需要的朋友可以參考下2017-07-07
Python實(shí)現(xiàn)提取Excel指定關(guān)鍵詞的行數(shù)據(jù)
這篇文章主要為大家介紹了如何利用Python實(shí)現(xiàn)提取Excel指定關(guān)鍵詞的行數(shù)據(jù),文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起動(dòng)手試一試2022-03-03

