欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

詳解Python連接oracle的問題記錄與解決

 更新時(shí)間:2023年04月17日 10:41:00   作者:Soyou  
這篇文章主要為大家詳細(xì)介紹了Python連接oracle時(shí)會出現(xiàn)的一些問題記錄與解決方法,文中的示例代碼講解詳細(xì),需要的小伙伴可以參考一下

昨日晚平臺升級,我們成功送BUG上線,今天系統(tǒng)問題又多了起來,大多數(shù)時(shí)候的運(yùn)維問題需要根據(jù)業(yè)務(wù)反饋的#訂單號# 查詢到當(dāng)前狀態(tài),然后再進(jìn)行反饋和處理。

每次看到運(yùn)維問題前都要去打開plsql等工具進(jìn)行數(shù)據(jù)庫查詢,或者登錄到系統(tǒng)里面進(jìn)行查看,然后再進(jìn)行具體的回復(fù)和處理。

以上是需求背景

能否寫一個(gè)程序?實(shí)現(xiàn)一步操作把常用的信息都查詢出來,省去打開數(shù)據(jù)庫工具和重復(fù)編寫/修改腳本的繁瑣操作。

通過查找資料,寫了一個(gè),碰到不少問題,特意記錄下來。

技術(shù)框架

開發(fā)語言:Python,數(shù)據(jù)庫:oracle,第三方庫:cx_Oracle(用于python和oracle的連接),prettytable(用于表格化輸出展示數(shù)據(jù))

開發(fā)步驟

一、安裝cx_Oracle

pip install cx_Oracle

參考官方說明文檔: cx-oracle.readthedocs.io/en/latest/user_guide/installation.html

二、編寫數(shù)據(jù)庫操作類

直接使用了chatgpt提供的代碼,因?yàn)槲抑挥玫搅瞬樵兎椒?,所以只查沒有增刪改,另外考慮到要同時(shí)查詢多次數(shù)據(jù),所以自己修改了實(shí)現(xiàn)了一個(gè)連接池的功能。

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

三、輸入訂單號,執(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("輸入訂單號: ").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(['**號', '**時(shí)間', '當(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ù)):

問題記錄

第一個(gè)問題就是安裝 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客戶端(客戶端下載見問題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è)問題,是因?yàn)槲冶緳C(jī)原來安裝的是19.18版本,換成11.2版本的客戶端,按照問題2的操作,將三個(gè)dll文件重新復(fù)制過去,解決問題。

后期優(yōu)化

  • 將sql語句集中放到配置文件里面,并配置表頭,可以實(shí)現(xiàn)多查詢自由擴(kuò)展。
  • 通過bat腳本調(diào)用執(zhí)行,真正實(shí)現(xiàn)一鍵查詢。

以上就是詳解Python連接oracle的問題記錄與解決的詳細(xì)內(nèi)容,更多關(guān)于Python連接oracle的資料請關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

最新評論