Skip to content

Instantly share code, notes, and snippets.

@oldarmyc
Created December 10, 2018 18:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save oldarmyc/bd0d4d6e097e0b9841eb68dcc833be91 to your computer and use it in GitHub Desktop.
Save oldarmyc/bd0d4d6e097e0b9841eb68dcc833be91 to your computer and use it in GitHub Desktop.
Oracle example connection
"""
In order to connect to oracle the following packages will need to be installed from conda.
cx_oracle
oracle-instantclient
libaio
"""
# Import the library needed
import cx_Oracle
# Import config parser to read the ini file setup as a secret
import configparser
# Setup the credentials
config = configparser.ConfigParser()
config.read('/var/run/secrets/user_credentials/oracle_credentials')
# Define some variables read from secret that was defined as an ini file
username = config.get('default', 'username')
password = config.get('default', 'password')
uri = config.get('default', 'uri')
port = config.get('default', 'port')
# Create the connection and setup the cursor
conn = cx_Oracle.connect(f'{username}/{password}@//{uri}:{port}/XEPDB1')
cur = conn.cursor()
# Execute select statement and fetch all results
cur.execute("SELECT 'Hello World!' FROM dual")
res = cur.fetchall()
# Print results
print(res)
# Close the connection
conn.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment