使用 Python 连接到 Oracle 数据实例时出现问题

Problems Connecting to Oracle Data Instance with Python

我正在尝试使用 Python 脚本连接到 Oracle 数据实例 (ORAD)。

基本脚本如下:

import cx_Oracle
conn = cx_Oracle.connect("username/password@//server:1560/orad")
c = conn.cursor()
c.execute('select distinct * from table1')
for row in c:
   print(row)
conn.close()

我目前也有实例的 portSIDhostname,如果有帮助的话。

运行 此脚本产生一个:cx_Oracle.DatabaseError: ORA-12514: TNS:listener does not currently know of service requested in connect descriptor 错误,
使用其他连接(已注释掉)会产生错误 SyntaxError: invalid syntax

我不确定我做错了什么。我确实检查了我的 TNSNAMES.ORA 文件,其中包含一些 ifile 链接到 DBA 安全(我无权查看或编辑)其他文件。

我查看了 this post and this post,但我没有 IP,只有主机名。

如有任何帮助,我们将不胜感激。

以下答案和脚本有效:

def get_data(database, username, password, sql_statement):
    import cx_Oracle
        dsn_tns = cx_Oracle.makedsn('<server>', '<port>', '<sid>')
        connection = cx_Oracle.connect(username, password, dsn_tns)

    c = connection.cursor()
    c.execute(sql_statement)

    # Print the returning dataset
    for row in c:
        print(row)

    # Close the connection
    connection.close()