0

I am trying to connect to an Oracle Data instance (ORAD) with a Python script.

Here is the basic script:

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()

I currently have the instance's port, SID, and hostname, too, if that helps.

Running this script yields a: cx_Oracle.DatabaseError: ORA-12514: TNS:listener does not currently know of service requested in connect descriptor error,
while using the other connections (that is commented out) yields an error SyntaxError: invalid syntax

I am unsure of what I am doing wrong. I did check my TNSNAMES.ORA file which contains a few ifile links to DBA secured (I don't have access to see or edit) other files.

I have viewed this post and this post, but I don't have the IP, just the host name.

Any assistance would be appreciated.

artemis
  • 6,857
  • 11
  • 46
  • 99

1 Answers1

0

The following answer and script worked:

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()
artemis
  • 6,857
  • 11
  • 46
  • 99