Please refer my post [ http://jeyaseelan-m.blogspot.com/2018/11/how-to-connect-oracle-database-using.html ] for basic setup or python to oracle connection
import sys,os,datetime,cx_Oracle,subprocess,traceback,logging
from pprint import pprint
# Global Variables declarations
global CONFIG_FILE
CONFIG_FILE=".BLACKOUT_CONFIG"
CONFIG_TEMPLATE = { 'SOURCE_DIR' : 'SOURCE_DIR', 'USERID' : 'USERID', 'PASSWORD' : 'PASSWORD', 'TNS_NAME' : 'TNS_NAME' }
# Print messages
def printError(module,message):
logging.error("Error at Module - {}: {} " .format(module,message))
sys.exit(1)
def printWarning(module,message):
print("Wanning at Module - {}: {} " .format(module,message))
# Get config values
def getConfig():
global configDict
configDict = {}
if os.path.exists(CONFIG_FILE):
with open(CONFIG_FILE) as config_file:
for row in config_file:
key,value = row.split(":")
configDict[key]=value.strip()
else:
printError("getConfig","{} doesnot exist".format(CONFIG_FILE))
CONFIG_TEMPLATE_set=set(CONFIG_TEMPLATE)
config_set =set(configDict)
if CONFIG_TEMPLATE_set ^ config_set :
printError("getConfig"," {} parameter is missing in the {} confgile file ".format(CONFIG_TEMPLATE_set ^ config_set,CONFIG_FILE) )
# Get DB details
def getDBconfig():
global cmdb_dBuser ,cmdb_dBpass,cmdb_dBname,cmdb_dBconection
cmdb_dBuser=configDict["USERID"]
cmdb_dBpass=configDict["PASSWORD"]
cmdb_dBconection=configDict["TNS_NAME"]
# Connect Database
def connectDB():
global cmdb_dBconection,cmdb_dBcursor
try:
cmdb_dBconection = cx_Oracle.connect(cmdb_dBuser,cmdb_dBpass,cmdb_dBconection)
cmdb_dBcursor=cmdb_dBconection.cursor()
except cx_Oracle.DatabaseError as e:
error, = e.args
printError(connectDB,error.message)
# run query
def runQuery(sql):
cmdb_dBcursor.execute(sql, REFERENCE_NUMBER=str(sys.argv[1]) )
result=cmdb_dBcursor.fetchall ()
for row in result:
print ( " Task Number : {0} ". format(row[0]))
print ( " Assigned To : {0} ". format(row[1]))
print ( " Assigment Group : {0} ". format(row[2]))
print ( " Change Number : {0} ". format(row[3]))
print ( " Task State : {0} ". format(row[4]))
print ( " Planned Start : {0} ". format(row[5]))
print ( " Planned End : {0} ". format(row[6]))
print ( " Task details : {0} ". format(row[11]))
No comments:
Post a Comment