简体   繁体   中英

Connecting to Oracle Database Using Python

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM