Vineeth T Sunny
Vineeth T Sunny

Reputation: 59

How to connect to Oracle 12c Database using cx_Oracle

sqlplus sys/Oracle_1@pdborcl as sysdba;

i'm using this command to connect to Oracle 12c from Command Prompt. How can i connect to the db using cx_Oracle. I'm new to Oracle DB.

Upvotes: 0

Views: 7686

Answers (2)

Bobby Durrett
Bobby Durrett

Reputation: 1293

I think this is the equivalent of the sqlplus command line that you posted:

import cx_Oracle

connect_string = "sys/Oracle_1@pdborcl"
con = cx_Oracle.connect(connect_string,mode=cx_Oracle.SYSDBA)

I tried it with a non-container database and not with a pdb so I can't verify that it would work with a pdb. You may not want to connect as sys as sysdba unless you know that you need that level of security.

Bobby

Upvotes: 3

Shankar Nag
Shankar Nag

Reputation: 93

You can find the documentation here cx_Oracle docs

To query the database, use the below algorithm

import cx_Oracle

dsn = cx_Oracle.makedsn(host, port, sid) 
connection = cx_Oracle.connect(dsn,mode = cx_Oracle.SYSDBA)
query = "SELECT * FROM MYTABLE"
cursor = connection.cursor()
cursor.execute(query)
resultSet=cursor.fetchall()
connection.close()

The above code works to fetch data from MYTABLE connecting to the above dsn. Better to go through cx_Oracle docs.

Upvotes: 3

Related Questions