using PYTHON >= 3.6; here it works by doing this:
OBS.: here I'm using 2 databases types ORACLE 12g and MYSQL 5.6 ; and the main server is the ORACLE, mysql is used only by some parts of the software ... then ... the code of my connection class:
here is comming from shellscript code... then i just bring a string to show here...
myp3 = 'XXXXXXXXXXXXXXXXXXXXXXXXXX PASSWD MYSQL XXXXXXXXXXXXXXXXXXXXXXXXXXX'
import cx_Oracle
import MySQLdb
import MySQLdb.cursors
class oracle(object):
def __init__(self, serverORACLE=3, MYSQL='N', serverMYSQL=3):
## ORACLE connection!
try:
if serverORACLE == 1:
self.db = cx_Oracle.connect('sys/[xxxxPASSWORDxxxxx]@[xxxxxIP/HOSTxxxxxx]:1521/DATABASE_X')
print('ORACLE [ system/[xxxxPASSWORDxxxxx]@[xxxxxIP/HOSTxxxxxx]:1521 ] ===> CONNECTED')
if serverORACLE == 2:
self.db = cx_Oracle.connect('system/[xxxxPASSWORDxxxxx]@[xxxxxIP/HOSTxxxxxx]:1521/DATABASE_X')
print('ORACLE [ system/[xxxxPASSWORDxxxxx]@[xxxxxIP/HOSTxxxxxx]:1521 ] ===> CONNECTED')
if serverORACLE == 3:
self.db = cx_Oracle.connect('userLEVEL1/[xxxxPASSWORDxxxxx]@[xxxxxIP/HOSTxxxxxx]:1521/DATABASE_X')
print('ORACLE [ userLEVEL1/[xxxxPASSWORDxxxxx]@[xxxxxIP/HOSTxxxxxx]:1521 ] ===> CONNECTED')
if serverORACLE == 4:
self.db = cx_Oracle.connect('userLEVEL2/[xxxxPASSWORDxxxxx]@[xxxxxIP/HOSTxxxxxx]:1521/DATABASE_X')
print('ORACLE [ userLEVEL2/[xxxxPASSWORDxxxxx]@[xxxxxIP/HOSTxxxxxx]:1521 ] ===> CONNECTED')
self.cursor = self.db.cursor()
except Exception as e:
count = 0
S1 = ''
for W in str(e):
S1+=W
count+=1
if count >= 40:
S1+=' \n'
count = 0
print('\n\n ORACLE DATABASE ===> CONECTION FAILED!',
'\n error - module: ', S1)
##conexao MYSQL
if MYSQL=='S':
try:
if serverMYSQL == 1:
self.dbMySQL = MySQLdb.connect(user='root', host='XXXXXX HOST XXXXX', use_unicode=True, cursorclass=MySQLdb.cursors.DictCursor,
charset='utf8', port=3306, passwd=myp3, db='XXXX')
print('MySQL [ root / XXXXXX HOST XXXXX:3306 ] ===> CONNECTED')
if serverMYSQL == 2:
self.dbMySQL = MySQLdb.connect(user='XXXX USER XXXXX', host='XXXXXX HOST XXXXX', use_unicode=True, cursorclass=MySQLdb.cursors.DictCursor,
charset='utf8', port=3306, passwd=myp3, db='XXXX')
print('MySQL [ XXXX USER XXXXX / XXXXXX HOST XXXXX:3306 ] ===> CONNECTED')
self.cursorMYSQL = self.dbMySQL.cursor()
except Exception as e:
count = 0
S1 = ''
for W in str(e):
S1+=W
count+=1
if count >= 40:
S1+=' \n'
count = 0
print('\n\n MYSQL DATABASE ===> CONECTION FAILED!',
'\n error - module: ', S1)
"""