使用Python

时间:2017-05-16 15:42:08

标签: python oracle

我正在尝试使用以下脚本连接到Oracle数据库;

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

但是我收到了以下错误; TNS:Connect timeout occurred

有人可以告诉我这里出了什么问题吗?

1 个答案:

答案 0 :(得分: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()