cx_Oracle连接数据库报DPI-1047: Cannot locate a 64-bit Oracle Client library

时间:2024-11-11 22:11:14

针对这个问题查了很多资料,大部分的说法是python版本、oracle客户端版本、操作系统都要是64位的。其实只需要pthon版本和oracle客户端版本一致即可,即都为64位或都为32位。

但我这里要讲的是如何解决这个问题的过程:

在python中的报错中提示了这个:

DPI-1047: Cannot locate a 64-bit Oracle Client library: "The specified module could not be found". See /en/latest/user_guide/ for help

所以最靠谱的方法是去/en/latest/user_guide/ 这里找资料。根据文档中的方法,我们可以看到这个:

 cx_Oracle.init_oracle_client() to set the Oracle Client directory:

import cx_Oracle
import sys
import os

try:
    if ("darwin"):
        lib_dir = (("HOME"), "Downloads",
                               "instantclient_19_8")
        cx_Oracle.init_oracle_client(lib_dir=lib_dir)
    elif ("win32"):
        lib_dir=r"C:\oracle\instantclient_19_9"
        cx_Oracle.init_oracle_client(lib_dir=lib_dir)
except Exception as err:
    print("Whoops!")
    print(err);
    (1);

使用这个的好处是:不用到处去设置变量让cx_Oracle去查找oracle client的路径。

我自己的情况,设置了这个之后,不会报DPI-1047错误了,但报了另一个:DPI-1072: the Oracle Client library version is unsupported, 这个问题查了很久,最后在这篇文中找到了答案: /odpi/doc/installation

ODPI-C dynamically loads available Oracle Client libraries at runtime. This allows code using ODPI-C to be built only once, and then run using any available Oracle Client 21, 19, 18, 12, or 11.2 libraries. If Oracle Client libraries are not found, the error “DPI-1047: Cannot locate an Oracle Client library” is raised. If an Oracle Client library is found but the version cannot be determined, the error “DPI-1072: the Oracle Client library version is unsupported” is raised. For example, this could be because the Oracle Client library version is too old (prior to 10g) or entirely unsupported (such as Oracle Client 19 on Windows 7).

里面说win7 不支持 oracle client 19 。

后面换了18.5的版本,还不是行,干脆换成11.1版本了,换成了这个报错 :DPI-1050: Oracle Client library is at version 11.1 but version 11.2 or higher is needed

看到这个大喜过望, 直接换成了11.2版本,就可以了。

后面还报了另一个错:

: (cx_Oracle.DatabaseError) ORA-12514: TNS:listener does not currently know of service requested in connect descriptor

这个是由于 python连接cx_oracle时,要用SERVICE_NAME,而不是sid,这个在你要连接的库中查询一下就好:

select value from v$parameter where name like '%service_name%';

然后替换一下这里标红的地方即可:

dsnstr=cx_Oracle.makedsn('','1521',service_name='sidxx')