如何使用Python 3.6连接到Oracle 11G(11.2.0.3.0)数据库


import cx_Oracle
print('connection start')
db_connection = cx_Oracle.connect("jdbc:oracle:thin:@(DESCRIPTION = (ADDRESS = (PROTOCOL = TCP)(HOST = <Host1>)(PORT = <port_number>)) (ADDRESS = (PROTOCOL = TCP)(HOST = <Host2>)(PORT = <port_number>)) (FAILOVER=true)(LOAD_BALANCE=true) (CONNECT_DATA = (SERVER = DEDICATED) (SERVICE_NAME = <service_name>) (FAILOVER_MODE=(TYPE=SELECT)(METHOD=BASIC)(RETRIES=180)(DELAY=5))))", "<username>", "<password>")
print(db_connection)
print('connection successful')

我正在尝试使用Python 3.6.1(Anaconda 4.4 Dist)连接到Oracle 11G数据库,但是我遇到以下错误。

DatabaseError: ORA-12154: TNS:could not resolve the connect identifier specified

我能够使用Oracle-SQL-Developer版本17(具有相同的连接字符串)成功连接到同一Oracle 11G数据库。

所以,我想知道我的代码是否有任何问题,或者CX_oracle需要支持驱动程序等。任何帮助都将不胜感激。

python cx_oracle是基于C(例如PHP的OCI8,Node.js的node-oracledB,Ruby的Ruby-oci8等),并使用与JDBC的其他连接语法(或SQL开发人员 - 是Java-java-如@baski所说,基于)。

看看https://github.com/oracle/node-oracledb/blob/blob/v2.0.13-doc/doc/doc/api.md#connectionstrings,显示各种连接方法服务名称和完整的嵌入式字符串。甚至还有一个关于将JDBC连接字符串更改为基于C语言可用的字符串的部分。

在您的情况下,您的cx_oracle连接字符串将是:

"(DESCRIPTION = (ADDRESS = (PROTOCOL = TCP)(HOST = <Host1>)(PORT = <port_number>)) (ADDRESS = (PROTOCOL = TCP)(HOST = <Host2>)(PORT = <port_number>)) (FAILOVER=true)(LOAD_BALANCE=true) (CONNECT_DATA = (SERVER = DEDICATED) (SERVICE_NAME = <service_name>) (FAILOVER_MODE=(TYPE=SELECT)(METHOD=BASIC)(RETRIES=180)(DELAY=5))))"

(为什么要再恢复这么多?)

相关内容

  • 没有找到相关文章

最新更新