0

I am trying to connect to Oracle database using the following script;

import cx_Oracle
username = user
password = pwd
host = host
port = '1521'
service = service
dbconn = cx_Oracle.connect(username + '/' + password + '@'+ host + ':' + port + '/' + service)

But I got the following error; TNS:Connect timeout occurred

Can anybody please suggest me what is going wrong here?

Ishwor Bhatta
  • 35
  • 1
  • 3

1 Answers1

0
# importing module 
import cx_Oracle 


# Create a table in Oracle database 
try: 

    con = cx_Oracle.connect('scott/tiger@localhost') 
    
    # Now execute the sqlquery 
    cursor = con.cursor() 
    
    # Creating a table srollno heading which is number 
    cursor.execute("create table student(srollno number, \ 
                    name varchar2(10), efees number(10, 2)") 
                    
    print("Table Created successful") 
    
except cx_Oracle.DatabaseError as e: 
    print("There is a problem with Oracle", e) 

# by writing finally if any error occurs 
# then also we can close the all database operation 
finally: 
    if cursor: 
        cursor.close() 
    if con: 
        con.close() 
  • A quick tip: if you use triple-quotes around the SQL string, you won't need the backslash. This can make it easier to edit & work with SQL in Python. (PS your answer is off topic, since it doesn't address the connection error, which is likely a connection string issue, or a network issue!) – Christopher Jones Dec 06 '20 at 21:40