DEV Community

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

Posted on

1 2

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)

👋 Kindness is contagious

Explore a trove of insights in this engaging article, celebrated within our welcoming DEV Community. Developers from every background are invited to join and enhance our shared wisdom.

A genuine "thank you" can truly uplift someone’s day. Feel free to express your gratitude in the comments below!

On DEV, our collective exchange of knowledge lightens the road ahead and strengthens our community bonds. Found something valuable here? A small thank you to the author can make a big difference.

Okay