使用JDBC,我们可以使用以下语法通过LDAP连接到Oracle数据库:
jdbc:oracle:thin:@ldap://host:1234/service_name,cn=OracleContext,dc=org,dc=com
我该如何使用cx_oracle通过LDAP连接?
使用JDBC,我们可以使用以下语法通过LDAP连接到Oracle数据库:
jdbc:oracle:thin:@ldap://host:1234/service_name,cn=OracleContext,dc=org,dc=com
[DomainContext]
的格式为cn=OracleContext,dc=foo,dc=bar
首先,您需要按照Oracle文档安装cx_Oracle。C:\path\to\instant_client_xx_yy
下。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
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)
最终我选择了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 )
https://docs.oracle.com/cd/B28359_01/network.111/b28317/ldap.htm