简体   繁体   English

使用 cx_Oracle 在 python 中连接 oracle 时出错

[英]error when connecting oracle in python using cx_Oracle

I was trying to connect oracle database using python like below.我正在尝试使用 python 连接 oracle 数据库,如下所示。

import cx_Oracle
conn = cx_Oracle.connect('user/password@host:port/database')

I've faced an error when connecting oracle.连接oracle时遇到错误。 DatabaseError: DPI-1047: 64-bit Oracle Client library cannot be loaded: "libclntsh.so: cannot open shared object file: No such file or directory". DatabaseError:DPI-1047:无法加载 64 位 Oracle 客户端库:“libclntsh.so:无法打开共享对象文件:没有这样的文件或目录”。 See https://oracle.github.io/odpi/doc/installation.html#linux for help.请参阅https://oracle.github.io/odpi/doc/installation.html#linux获取帮助。

I've been struggling to figure it out.我一直在努力弄清楚。 I used my user name, password, host, port and database('orcl') for example,例如,我使用了我的用户名、密码、主机、端口和数据库('orcl'),

'admin/admin@10.10.10.10:1010/orcl' . 'admin/admin@10.10.10.10:1010/orcl'

Why coudn't it connect?为什么连不上?

Ahh, btw I'm running all the code in azure notebooks.啊,顺便说一句,我在 azure notebooks 中运行所有代码。

That error indicates that you are missing a 64-bit Oracle client installation or it hasn't been configured correctly.该错误表明您缺少 64 位 Oracle 客户端安装或未正确配置。 Take a look at the link mentioned in the error message.查看错误消息中提到的链接。 It will give instructions on how to perform the Oracle client installation and configuration.它将提供有关如何执行 Oracle 客户端安装和配置的说明。

[Update on behalf of Anthony: his latest cx_Oracle release doesn't need Oracle Client libraries so you won't see the DPI-1047 error if you upgrade. [代表 Anthony 更新:他最新的 cx_Oracle 版本不需要 Oracle 客户端库,因此如果您升级,您将不会看到 DPI-1047 错误。 The driver got renamed to python-oracledb but the API still supports the Python DB API 2.0 specification.驱动程序已重命名为 python-oracledb,但 API 仍支持 Python DB API 2.0 规范。 See the homepage .]主页。]

这似乎是 6.X 版本的问题。这个问题没有出现在 5.X 中。但是对于我的情况,一个小解决方法奏效了。我安装在我的物理机器上,我唯一需要做的就是重启电脑或重新打开终端正如我在环境变量的路径中添加的那样。您可以尝试在物理机中安装,而不是使用 azure notebooks。

This error come when your Oracle Client is not installed or LD_LIBRARY_PATH is not set where libclntsh.so is present.当您的 Oracle 客户端未安装或未在 libclntsh.so 存在的位置设置 LD_LIBRARY_PATH 时出现此错误。

if you have Oracle client installed then search for libclntsh.so and set the LD_LIBRARY_PATH as如果您安装了 Oracle 客户端,则搜索 libclntsh.so 并将 LD_LIBRARY_PATH 设置为

"export LD_LIBRARY_PATH=/app/bds/parcels/ORACLE_INSTANT_CLIENT/instantclient_11_2:$LD_LIBRARY_PATH" “导出 LD_LIBRARY_PATH=/app/bds/parcels/ORACLE_INSTANT_CLIENT/instantclient_11_2:$LD_LIBRARY_PATH”

Here is the full program to connect Oracle using python.这是使用 python 连接 Oracle 的完整程序。 First, you need to install cx_Oracle.首先,您需要安装 cx_Oracle。 to install it fire the below command.安装它触发以下命令。

pip install cx_Oracle

 import cx_Oracle def get_databse_coonection(): try: host='hostName' port ='portnumber' serviceName='sid of you database' user = 'userName' password = 'password' dns = cx_Oracle.makedsn(host,port,service_name=serviceName) con = cx_Oracle.connect(user, password, dns) cursor = con.cursor() query ="select * from table" cursor.execute(query) for c in cursor: print(c) except cx_Oracle.DatabaseError as e: print("There is a problem with Oracle", e) finally: if cursor: cursor.close() if con: con.close() get_databse_coonection()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM