這篇文章主要介紹“Python連接oracle的問題如何解決”的相關(guān)知識,小編通過實際案例向大家展示操作過程,操作方法簡單快捷,實用性強(qiáng),希望這篇“Python連接oracle的問題如何解決”文章能幫助大家解決問題。
成都創(chuàng)新互聯(lián)公司主要從事做網(wǎng)站、成都做網(wǎng)站、網(wǎng)頁設(shè)計、企業(yè)做網(wǎng)站、公司建網(wǎng)站等業(yè)務(wù)。立足成都服務(wù)集賢,十多年網(wǎng)站建設(shè)經(jīng)驗,價格優(yōu)惠、服務(wù)專業(yè),歡迎來電咨詢建站服務(wù):13518219792
開發(fā)語言:Python,數(shù)據(jù)庫:oracle,第三方庫:cx_Oracle(用于python和oracle的連接),prettytable(用于表格化輸出展示數(shù)據(jù))
pip install cx_Oracle
直接使用了chatgpt提供的代碼,因為我只用到了查詢方法,所以只查沒有增刪改,另外考慮到要同時查詢多次數(shù)據(jù),所以自己修改了實現(xiàn)了一個連接池的功能。
import cx_Oracle
import queue
class OracleDatabase:
# 構(gòu)造函數(shù),傳入數(shù)據(jù)庫連接參數(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ù)庫連接
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ù)庫連接全部關(guān)閉<<<<")
pass
# 執(zhí)行查詢語句
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
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("輸入訂單號: ").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(['**號', '**時間', '當(dāng)前狀態(tài)', '單號', '機(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)
使用效果如下:粘貼訂單號回車,直接返回下面所需要的信息數(shù)據(jù)(測試數(shù)據(jù)):
第一個問題就是安裝 cx_Oracle的時候出錯:
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)選項安裝即可。
報錯信息
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客戶端(客戶端下載見問題3)目錄中的oci,oraocci11,oraociei11的3個DLL粘貼到你的Paython目錄的Lib/site-packages文件夾下面。
報錯信息
cx_Oracle.DatabaseError: DPI-1072: the Oracle Client library version is unsupported
下載oracle客戶端,并解壓安裝。下載地址:oracle.github.io/odpi/doc/installation 我出現(xiàn)這個問題,是因為我本機(jī)原來安裝的是19.18版本,換成11.2版本的客戶端,按照問題2的操作,將三個dll文件重新復(fù)制過去,解決問題。
將sql語句集中放到配置文件里面,并配置表頭,可以實現(xiàn)多查詢自由擴(kuò)展。
通過bat腳本調(diào)用執(zhí)行,真正實現(xiàn)一鍵查詢。
關(guān)于“Python連接oracle的問題如何解決”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識,可以關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,小編每天都會為大家更新不同的知識點(diǎn)。
網(wǎng)站名稱:Python連接oracle的問題如何解決
分享路徑:http://aaarwkj.com/article28/jegijp.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供面包屑導(dǎo)航、搜索引擎優(yōu)化、品牌網(wǎng)站建設(shè)、營銷型網(wǎng)站建設(shè)、服務(wù)器托管、全網(wǎng)營銷推廣
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)