【问题标题】:Error while connecting with Oracle 12c using cx_oracle使用 cx_oracle 连接 Oracle 12c 时出错
【发布时间】:2021-08-06 17:36:27
【问题描述】:

我正在尝试使用 cx_oracle 连接 Oracle 12c 数据库。我的代码如下:

import cx_Oracle
from cx_Oracle import DatabaseError
import pandas as pd
import credaws
import os

os.system('export ORACLE_HOME=/opt/app/oracle/product/client_12_2')
os.system('export PATH=$ORACLE_HOME/bin:$PATH')
os.system('export LD_LIBRARY_PATH=$ORACLE_HOME/lib')

try:
    # cx_Oracle.init_oracle_client(lib_dir=libdir)
    dsn_tns=cx_Oracle.makedsn(credaws.host_name,credaws.port_number,service_name=credaws.service_name)
    conn = cx_Oracle.connect(user=credaws.user,password=credaws.password,dsn=dsn_tns)
    if conn:
        cursor = conn.cursor()
        print('Connection Successful')
except DatabaseError as e:
    err, = e.args
    print("Oracle-Error-Code:", err.code)
    print("Oracle-Error-Message:", err.message)
    
finally:
    cursor.close()
    conn.close()

我仍然收到此错误:

Oracle 12c 安装在 /opt/app/oracle/product/client_12_2 位置。我做错了什么?

编辑 1:我在调用 cx_oracle 连接方法之前设置了 ORACLE_HOME、PATH 和 LD_LIBRARY_PATH 环境变量。但是,仍然出现相同的错误。

编辑 2:以 oracle 用户身份运行此脚本时,出现以下错误:

【问题讨论】:

  • 我在使用 Oracle 19c 时遇到了同样的问题。如果有人能回答这个问题,我将不胜感激。
  • 我尝试设置 ORACLE_HOME、PATH 和 LD_LIBRARY_PATH 变量。但是,仍然得到同样的错误。更新了问题中的代码。

标签: python python-3.x cx-oracle


【解决方案1】:

这个问题对我帮助很大。我一直在努力连接到 Oracle 数据库。我只是直接传入连接字符串来连接到数据库,它就可以工作了。这是有风险的,但脚本仅供我个人使用。我是 python 新手。 这是我的代码。

import os  
import cx_Oracle  

os.system('set ORACLE_HOME=C:\\oraclexe\\app\\oracle\\product\\10.2.0\\server')  
os.system('set PATH=$ORACLE_HOME/bin:$PATH')  
os.system('set LD_LIBRARY_PATH=$ORACLE_HOME/lib')  

#Test to see if the cx_Oracle is recognized  
print(cx_Oracle.version) # this returns 8.2.1  

cx_Oracle.clientversion()  
# I just directly passed in the connection string
con = cx_Oracle.connect('USERNAME/PWD@SERVER:PORT/DATABASENAME')  
cur = con.cursor()  
try:  
    # test the connection by getting the db version  
    print("Database version:", con.version)  

    cur.execute("select * from products order by 1")  
    res = cur.fetchall()  
    print('Number of products is ',len(res))  
    for row in res:  
        print(row)  
except cx_Oracle.DatabaseError as e:  
    err, = e.args  
    print("Oracle-Error-Code:", err.code)  
    print("Oracle-Error-Message:", err.message)  
finally:  
    cur.close()  
    con.close()  

【讨论】:

  • 您的os.system() 调用混合使用了Windows 和Linux 语法。在正在运行的进程中设置 LD_LIBRARY_HOME 也永远不会起作用 - 它必须在进程启动之前设置。最好遵循 cx_Oracle 文档和初始化示例:cx-oracle.readthedocs.io/en/latest/user_guide/…
猜你喜欢
  • 1970-01-01
  • 2018-08-08
  • 2018-06-11
  • 2016-07-23
  • 2018-12-14
  • 1970-01-01
  • 2014-05-18
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多