# All the queries in the file need to be executed on the same database
# The code below can also be used to execute queries directly
''' I am not removing the imported modules not being used in the code below as its being used in my other part of code'''
import fnmatch
from stat import *
import time
import csv
import sys
import subprocess
import os
import cx_Oracle # For Connecting to Oracle Database
import pymysql # For connecting to MySQL Database
import shutil
import datetime
from sys import argv, exit
import sqlite3
import logging
def Process_SQL_File(databasetype, servername, databasename, username, password, filename):
'Reads the queries from the SQL file and executes them'
try:
f = open(filename, "r")
sql_file = f.read()
queries = (sql_file.split(';'))
for query in queries:
if query.strip():
Execute_Query(databasetype, servername, databasename, username, password, query)
f.close()
except:
logger.debug ('ERROR OCCURED: ' + str(sys.exc_info()[1]))
finally:
f.close()
def Execute_Query(databasetype, servername, databasename, username, password, query):
'Calls the appropriate function to make the db call based on the databasetype'
if query.strip():
# if Database is Oracle call Execute_Query_Oracle function
if databasetype.strip().upper() == "ORACLE":
Execute_Query_Oracle(servername, databasename, username, password, query)
elif databasetype.strip().upper() == "MYSQL":
Execute_Query_MySQL(servername, databasename, username, password, query)
else:
logger.debug(databasetype + " database is not yet supported")
def Execute_Query_Oracle(servername, databasename, username, password, query):
'Executes the query on Oracle database'
connection = cx_Oracle.connect((str(username) + '/' + str(password) + '@' + str(servername) + '/' + str(databasename)))
cursor = connection.cursor()
try:
logger.debug("Query: " + query.strip())
cursor.execute(query)
# if the query is insert or delete; perform commiit
if 'insert into'.strip().upper() in query.upper() or '''
delete from'''.strip().upper() in query.upper():
connection.commit()
# if the query is count(*) or count(1); get 1 record only as expected
elif "(*)" in query.upper() or "(1)" in query.upper():
count, = cursor.fetchone()
logger.debug(count)
# if several columns selection or select * is being done;
# return multiple columns and multiple rows'''
else:
result = cursor.fetchall()
for r in result:
logger.debug(Format_DB_Output(str(r)))
except:
logger.debug('ERROR - QUERY COULD NOT BE EXECUTED ' + str(sys.exc_info()[1]))
finally:
cursor.close()
connection.close()
def Execute_Query_MySQL(hostname, dbname, username, password, query):
'Connects to MySQL Database'
connection = pymysql.connect(host=hostname, user=username, passwd=password, db=dbname)
cursor = connection.cursor()
try:
logger.debug("Query: " + query.strip())
cursor.execute(query)
# if the query is insert or delete; perform commiit
if 'insert into'.strip().upper() in query.upper() or '''
delete from'''.strip().upper() in query.upper():
connection.commit()
# if the query is count(*) or count(1); get 1 record only as expected
elif "(*)" in query.upper() or "(1)" in query.upper():
count, = cursor.fetchone()
logger.debug(count)
# if several columns selection or select * is being done;
# return multiple columns and multiple rows'''
else:
result = cursor.fetchall()
for r in result:
logger.debug(Format_DB_Output(str(r)))
except:
logger.debug('ERROR - QUERY COULD NOT BE EXECUTED ' + str(sys.exc_info()[1]))
finally:
cursor.close()
connection.close()
Two Quetions:
ReplyDeleteWhere is the definititon for "logger" and what is it?
Where are the outputs? May it´s going on with the function "Format_DB_Output"?
Thanks Tom