DEV Community

Cover image for A simple Oracle DB connection in Python
Andrew Pazikas
Andrew Pazikas

Posted on

A simple Oracle DB connection in Python

If you are managing a python backend with an wanting an Oracle database to serve you data you will likely need to use the cx_Oracle module to create an Oracle DB connection. Heres a little Python snippet to create an Oracle DB connection.

Why Oracle?

Many of use small time use common Opensource DBs like mySQL and PostgreSQL but in large corporations many of the database estate is Oracle therefore it helps to have an idea on how to work with it.

import cx_Oracle
import os

inst = os.environ["ORACLE_SID"]

try:
  dbconn = cx_Oracle.connect (mode=cx_Oracle.SYSDBA)
  print ("Connected to Oracle instance " + inst + " (" + dbconn.version +")")
  release = ((dbconn.version).split ('.'))[0:2]
except cx_Oracle.DatabaseError as e:
  error, = e.args
  print("Error connecting to instance: " + inst + " : " + error.message")



db = dbconn.cusrsor()```

Enter fullscreen mode Exit fullscreen mode

Top comments (0)