0

I'm trying to connect to an oracle db using python through PyCharm, below is my code and a screenshot of the connection details

Code:

import cx_Oracle   
try:
    conn = cx_Oracle.connect('sys/123@//localhost:1521/XEPDB1')    
except:
    print("Connection Error")
    exit()

Output

Connection Error enter image description here

2 Answers 2

2

There are multiple ways to do that, either with SID or service name

SID :

import cx_Oracle
dsn_tns = cx_Oracle.makedsn('server', 'port', 'sid')
conn = cx_Oracle.connect(user='username', password='password', dsn=dsn_tns)

Service name :-

import cx_Oracle
dsn_tns = cx_Oracle.makedsn('server', 'port', service_name='service_name')
conn = cx_Oracle.connect(user='username', password='password', dsn=dsn_tns)

You can refer to this documentation HERE

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks! it worked. However, I faced another error ORA-28009: connection as SYS should be as SYSDBA or SYSOPER
this is how I wrote it: tns = cx_Oracle.makedsn("localhost","1521", service_name="XEPDB1") conn = cx_Oracle.connect(user="sys", password="123", dsn=tns)
Regarding connecting as SYSDBA, search the doc link in the answer for the syntax to use: mode=cx_Oracle.SYSDBA. A general note: Avoid using SIDs; their use was obsoleted a decade or more ago.
0

For new versions:

import oracledb

conn = oracledb.connect(user='sys', password='pass', dsn='localhost:1521/database', mode=oracledb.SYSDBA) 

for normal users use: mode=oracledb.AUTH_MODE_DEFAULT

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.