python 連接 oracle


  • pip install cx_Oracle
  • 下載oracel 客戶端 instantclient-basic-windows.x64-18.5.0.0.0dbru.zip ,不下載客戶端可能有以下報錯:

DatabaseError: DPI-1047: Cannot locate a 64-bit Oracle Client library: "The specified module could not be found". See https://oracle.github.io/odpi/doc/installation.html#windows for help

  • 解壓文件到腳本 的下級目錄,或者設置路徑到環境變量
#encoding=utf-8
import cx_Oracle as oracle
import os
#print(os.environ["path"])
set_path = os.environ["path"]
oracle_path = "..\\instantclient_18_5"
os.environ["path"] = ';'.join((set_path,oracle_path))  # oracle 路徑 暫時加到 環境變量 
#print(os.environ["path"])

def checkCodeFor2():
    print("##########查詢2號驗證碼#############\n")
    db = oracle.connect("賬號", "密碼", 'ip:1521/庫名')
    cur = db.cursor()
    while(1):
        emp_id = input("請輸入查詢的ID:\n")
        if emp_id:
            try:
                cur.execute('select a.code,a.id from db_test a where a.id={}'.format(emp_id))
                #cursor.execute('select 1;')
                data = cur.fetchone()
                print(data)
                check_sign = input("是否繼續查詢:Y/y 是 /N 否,其他任意字符都退出\n")
                if check_sign.lower() == "y":
                    pass
                else:
                    return 0
            except oracle.DatabaseError as e:
                msg_error = e
                print("可能數據輸入錯誤,請檢查數據正確性,錯誤:\n{}".format(msg_error))

        else:
            pass
if __name__=="__main__":
    checkCodeFor2()


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM