cx_Oracle LDAP连接字符串语法

3

使用JDBC,我们可以使用以下语法通过LDAP连接到Oracle数据库:

jdbc:oracle:thin:@ldap://host:1234/service_name,cn=OracleContext,dc=org,dc=com

我该如何使用cx_oracle通过LDAP连接?

你最后解决了这个问题吗? - Tyger Guzman
1
@TygerGuzman 是的,无法在cx_Oracle(或其底层OCI库)中使用LDAP字符串语法。如果您想要在cx_Oracle中使用LDAP,您必须使用Anthony在下面提到的ldap.ora文件。 - Matthew Moisen
3个回答

2
这里是我使用Python 3.7和cx_Oracle v.8.2.0在Win 10上的两分建议。
我想使用Python向Oracle数据库发出查询,我已经有了以下内容:
- 用户名(或模式) - 密码 - 一个JDBC连接字符串,看起来像: jdbc:oracle:thin:@ldap://[LDAPHostname1]:[LDAPPort1]/[ServiceName],[DomainContext] ldap://[LDAPHostname2]:[LDAPPort2]/[ServiceName],[DomainContext] 其中[DomainContext]的格式为cn=OracleContext,dc=foo,dc=bar 首先,您需要按照Oracle文档安装cx_Oracle。
请注意:
cx_Oracle需要一系列库文件,这些文件是Oracle Instant Client“Basic”或“Basic Light”包的一部分(可在此处获得)。假设我们将包解压到C:\path\to\instant_client_xx_yy下。
根据您所在的平台,可能需要满足其他要求(例如,在Windows上安装某些Visual Studio可再发行组件)。
对于LDAP部分,需要两个配置文件:
  • sqlnet.ora : This is the profile configuration file for Oracle, but mine was simply containing :

    NAMES.DIRECTORY_PATH = (LDAP)
    

    It tells the library to resolve names using LDAP only.

  • ldap.ora : This file tells where to look for when resolving names using LDAP. I knew I was accessing two OID servers, so mine was of the form :

    DIRECTORY_SERVERS=([LDAPHostname1]:[LDAPPort1], [LDAPHostname2]:[LDAPPort2])
    DEFAULT_ADMIN_CONTEXT="dc=foo,dc=bar"
    DIRECTORY_SERVER_TYPE=oid
    

    Important Note : I had to remove the cn=OracleContext from the DEFAULT_ADMIN_CONTEXT entry in order to make the name resolution work

    Let's say those two files were saved under C:\path\to\conf

现在是 Python 的部分。我使用了 cx_Oracle.init_oracle_client() 方法来指向库和配置文件。(请注意,还有其他方法可以让 cx_Oracle 访问这些文件,例如设置环境变量或将其放置在预定义的位置。这在 安装指南 中有解释。)
以下是一个小示例代码:

import cx_Oracle

# username and password retrieved here

cx_Oracle.init_oracle_client(lib_dir=r'C:\path\to\instant_client_xx_yy', config_dir=r'C:\path\to\conf')

try:
    with cx_Oracle.connect(user=username, password=password, dsn='[ServiceName]') as connection:
        cursor = connection.cursor()
        cursor.execute('SELECT * FROM ALL_TAB_COLUMNS')
        # Outputs tables and columns accessible by the user
        for row in cursor:
            print(row[1], '-', row[2])
        cursor.close()

except cx_Oracle.DatabaseError as e:
    print("Oracle Error", e)

2

最终我选择了jaydebeapi。

import pandas as pd 
import jaydebeapi
import jpype
import os
import sys

def run(f_name,command,username,pw ):
    jar='ojdbc8.jar'
    args = '-Djava.class.path=%s' % jar
    jvm_path = jpype.getDefaultJVMPath()
    jpype.startJVM(jvm_path, args)
    con = jaydebeapi.connect("oracle.jdbc.driver.OracleDriver", "jdbc:oracle:thin:@ldap://server.prod.company.com:3060/service,cn=OracleContext,dc=prod,dc=company,dc=com",[username, pw], jar)
    try:
        df= pd.read_sql(command,con)
        df.to_excel(f_name)
        print(df)
    except Exception as e:
        print(e)
    finally:
        con.close()



def Run_Program(myvars):
    os.chdir(sys._MEIPASS)    
    f_name = myvars.MyFileName
    command = myvars.plainTextEdit_CSVString.toPlainText()
    username = myvars.lineEdit_UserName.text()
    pw = myvars.lineEdit_Password.text()
    run(f_name,command,username,pw )

将 Oracle 客户端中的 ojdbc8.jar 文件保存在同一文件夹中,并在代码中指定位置。还需要将模块 JPype1 降级到 JPype1==0.6.3(它是 jaydebeapi 的一个要求)。 这样使用 pyinstaller 打包后就可以共享了(我创建了一个 pyqt5 用户界面供用户使用)。

1

1
如果我的应用程序需要封闭,并且不能依赖客户端的文件,该怎么办? - Diogo Santos

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接