Skip to content

Instantly share code, notes, and snippets.

@sancau
Created January 20, 2017 09:40
Show Gist options
  • Save sancau/d92103730a1c9ffdd2c83aa64a1c59c4 to your computer and use it in GitHub Desktop.
Save sancau/d92103730a1c9ffdd2c83aa64a1c59c4 to your computer and use it in GitHub Desktop.
DBMS interaction cases (ORACLE, POSTGRESQL, SQLACLHEMY)
# RAW CX_ORACLE
import cx_Oracle
import os
DB_USERNAME = '...'
DB_PASSWORD = '...'
DB_SID = '...' # basicaly a schema
DB_IP = '...'
DB_PORT = int
def get_sql_connection():
"""Returns pre-configured Oracle connection.
"""
os.environ['NLS_LANG'] = '.UTF8' # to receive properly encoded data (if not english)
"""
TNS (Transparent Network Substrate) is Oracle's networking architecture.
TNS provides a uniform application interface to enable network
"""
dsn = cx_Oracle.makedsn(DB_IP, # dsn => data source name
DB_PORT,
DB_SID)
conn = cx_Oracle.connect(DB_USERNAME, DB_PASSWORD, dsn)
return conn
# RAW PSYCOPG2
import psycopg2
from psycopg2 import IntegrityError
conn = psycopg2.connect(database='...',
user='...',
password='...',
host='...',
port='...')
c = conn.cursor()
conn.autocommit = True
# SQL ALCHEMY
from sqlalchemy import create_engine, MetaData
CS = 'connection string' # your connection string
conn = create_engine(CS)
meta = MetaData(schema='data', bind=eng)
meta.reflect()
test = meta.tables['data.test']
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment