Pyodbc if connection fails retry - python

Hi there I have the following python code to connect to my SQL-Server DB
class CDBTools:
details = {
'server' : 'localhost',
'database' : 'MyDB',
'username' : 'me',
'password' : 'myPass'
}
conn = None
def __init__(self, server, database, username, password):
self.details["server"] = server
self.details["database"] = database
self.details["username"] = username
self.details["password"] = password
def connect_to_db(self):
connect_string = 'DRIVER={{FreeTDS}};SERVER={server}; DATABASE={database};UID={username};PWD={password}'.format(**self.details)
try:
self.conn = pyodbc.connect(connect_string, autocommit=True)
#print(connect_string)
#Logger.Log(self.conn, "info")
except pyodbc.Error as e:
print(e, "error")
def execute_select_query(self, query):
try:
curr = self.conn.cursor()
out = curr.execute(query).fetchall()
except pyodbc.IntegrityError as e:
out = []
print('ms-sql error: {0}'.format(e))
except pyodbc.OperationalError as err: #Something happend with db, so try again
out = []
print('ms-sql Operation Error: {0}'.format(err))
except AttributeError as err:
out = []
print('Connection to DB failed')
pass
try:
curr.close()
except:
print('Connection to DB failed')
return out
def execute_inset_query(self, query):
try:
database_cursor = self.conn.cursor()
database_cursor.execute(query)
except pyodbc.DataError as e:
print('ms-sql error: {0}'.format(e))
except pyodbc.IntegrityError as e:
print('ms-sql error: {0}'.format(e))
except pyodbc.OperationalError as err: #Something happend with db, so try again
print('ms-sql error: {0}'.format(e))
then in my main program I am trying this and it works just fine, until I disconnect the network
DBT = CDBTools("192.168.1.2\instance4", "my_db", "my_username", "my_passowrd")
DBT.connect_to_db()
while(True):
print("[{0}]: {1}".format(time.strftime("%H:%M:%S"), DBT.execute_select_query("SELECT Name FROM Persons WHERE ID='1'")))
When I disconnect the network I get no error, just time doesn't count anymore (of course because query is failing) but when I reconnect the network query never sucseeds anymore
so does anyone maybe know how I can modify execute_select_query and execute_inset_query so when connection to the db is restored it will start to work again :)
Thanks for Anwsering and Best Regards

Try this, it'll connect each time you use the with clause, and automatically disconnect when you leave it.
class CDBTools:
details = {
'server' : 'localhost',
'database' : 'MyDB',
'username' : 'me',
'password' : 'myPass'
}
conn = None
def __init__(self, server, database, username, password):
self.details["server"] = server
self.details["database"] = database
self.details["username"] = username
self.details["password"] = password
def connect_to_db(self):
connect_string = 'DRIVER={{FreeTDS}};SERVER={server}; DATABASE={database};UID={username};PWD={password}'.format(**self.details)
try:
conn = pyodbc.connect(connect_string, autocommit=True)
#print(connect_string)
#Logger.Log(self.conn, "info")
except pyodbc.Error as e:
print(e, "error")
return conn
def execute_select_query(self, conn, query):
try:
curr = conn.cursor()
out = curr.execute(query).fetchall()
except pyodbc.IntegrityError as e:
out = []
print('ms-sql error: {0}'.format(e))
except pyodbc.OperationalError as err: #Something happend with db, so try again
out = []
print('ms-sql Operation Error: {0}'.format(err))
except AttributeError as err:
out = []
print('Connection to DB failed')
pass
def execute_inset_query(self, conn, query):
try:
database_cursor = conn.cursor()
database_cursor.execute(query)
except pyodbc.DataError as e:
print('ms-sql error: {0}'.format(e))
except pyodbc.IntegrityError as e:
print('ms-sql error: {0}'.format(e))
except pyodbc.OperationalError as err: #Something happend with db, so try again
print('ms-sql error: {0}'.format(e))
then:
DBT = CDBTools("192.168.1.2\instance4", "my_db", "my_username", "my_passowrd")
while True:
with DBT.connect_to_db() as conn:
print("[{0}]: {1}".format(time.strftime("%H:%M:%S"), DBT.execute_select_query(conn, "SELECT Name FROM Persons WHERE ID='1'")))
I'd probably make a method to return the cursor rather than the connection. For example:
class CDBTools:
details = {
'server' : 'localhost',
'database' : 'MyDB',
'username' : 'me',
'password' : 'myPass'
}
conn = None
def __init__(self, server, database, username, password):
self.details["server"] = server
self.details["database"] = database
self.details["username"] = username
self.details["password"] = password
def get_cursor(self):
connect_string = 'DRIVER={{FreeTDS}};SERVER={server}; DATABASE={database};UID={username};PWD={password}'.format(**self.details)
try:
conn = pyodbc.connect(connect_string, autocommit=True)
#print(connect_string)
#Logger.Log(self.conn, "info")
except pyodbc.Error as e:
print(e, "error")
return conn.cursor()
DBT = CDBTools("192.168.1.2\instance4", "my_db", "my_username", "my_passowrd")
with DBT.get_cursor() as cursor:
cursor.execute("SELECT Name FROM Persons WHERE ID='1'")
for row in cursor.fetchall():
print(row)
Good luck!

Related

What should I put for the first parameter when calling "get_Tables_byName" function?

I'm writing a python code to read from mysql database:
def create_server_connection(host, user, password):
connection = None
try:
connection = pymysql.connect(host='localhost',
user='root',
password='pwd',
database='raw_data',
cursorclass=pymysql.cursors.DictCursor)
print("MySQL Database connection successful")
except err as error:
print(f"Error: '{error}'")
return connection
def read_query(connection, query):
cur = connection.cursor()
result = None
try:
cur.execute(query)
result = cur.fetchall()
return result
except err as error:
print(f"Error: '{error}'")
return cur
def get_Tables_byName(cursor, tableName):
q1 = f'''
SELECT table_name FROM raw_data.tables
where table_name like '{tableName}'; '''
res = []
cursor.execute(q1)
for row in cursor:
res.append(row[0])
return res
get_Tables_byName(cursor,'data_31942010201')
If I want to call get_Tables_byName function, what should I put in the first parameter? If I put cursor, the error message shows NameError: name 'cursor' is not defined

Not getting get_secret_value_response from client.get_secret_value()

I am trying to get secret but i am getting exception - 'NoneType' object is not subscriptable as I'm not getting secret from get_secret_value.
Below are my codes:
class OracleConnection:
"""
This is a helper class for making authenticated connections to Oracle.
"""
def init(self):
self.evars = os.environ
print("1")
self.secret_name = self.evars.get("ORACLE_SECRET_ARN", "cci-acoe-secrets-dss-oracle-uet-dev")
self.region_name = self.evars.get("REGION", "us-east-1")
self.encoding = 'UTF-8'
self.connection = None
self.pool = None
def get_secret(self):
global secret
print("2")
session = boto3.session.Session()
print("3")
client = session.client(
config=config,
service_name='secretsmanager',
region_name=self.region_name
)
print("4")
try:
print(self.secret_name)
get_secret_value_response = client.get_secret_value(
SecretId=self.secret_name
)
print(get_secret_value_response)
print("5")
except ClientError as e:
if e.response['Error']['Code'] == 'DecryptionFailureException':
raise e
elif e.response['Error']['Code'] == 'InternalServiceErrorException':
raise e
elif e.response['Error']['Code'] == 'InvalidParameterException':
raise e
elif e.response['Error']['Code'] == 'InvalidRequestException':
raise e
elif e.response['Error']['Code'] == 'ResourceNotFoundException':
raise e
else:
print("6")
if 'SecretString' in get_secret_value_response:
secret_val = get_secret_value_response['SecretString']
else:
secret_val = base64.b64decode(get_secret_value_response['SecretBinary'])
secret = json.loads(secret_val)
def __enter__(self):
"""
This method opens the Oracle connection.
"""
logger.debug("OracleConnection initializing connection with Oracle.")
logger.debug("Gathering Oracle credentials.")
try:
print("7")
self.get_secret()
print(secret)
info = {
'user': secret['username'],
'password': secret['password'],
'host': secret['host'],
'service': secret['dbname'],
'port': int(secret['port'])
}
# Make DSN address
self.user = info.get('user')
self.pwd = info.get('password')
self.host = info.get('host')
self.port = info.get('port')
self.service = info.get('service')
self.dsn = cx_Oracle.makedsn(self.host, self.port, service_name=self.service)
# Create the session pool
self.pool = cx_Oracle.SessionPool(
self.user,
self.pwd,
self.dsn,
min=100,
max=100,
increment=0,
encoding=self.encoding
)
# Acquire a connection from the pool
self.connection = self.pool.acquire()
print(self.connection.version)
except cx_Oracle.Error as error:
logger.critical("Failed to establish Oracle connection.\nError: {}".format(error))
raise
logger.debug("Opening Oracle Connection.")
return self
It's printing 1 2 3 4 also secret name but don't know why i am not getting any response from client.get_secret_value()? Please someone help.
I was having the same problem. Is your lambda attached to a VPC? If so, make sure you choose public subnets for it. It has something to do with private subnets cannot connect to the internet...and your secrets manager call will not work. I am also figuring this out.

Lost connection to MySQL server at 'localhost:3306', system error: Connection not available

servlet2.py
import mysql.connector
from mysql.connector import Error
from mysql.connector import errorcode
try:
connection = mysql.connector.connect(host='localhost',
database='*project',
user='*****',
password='*****',
)
print("Connection Established")
except mysql.connector.Error as e:
if e.errno == errorcode.ER_ACCESS_DENIED_ERROR:
print('Somethign is wrong with username or password')
elif e.errno == errorcode.ER_BAD_DB_ERROR:
print('Database does not exist')
else:
print(e)
c = connection.cursor()
def insertSQL(user):
mySql_insert_query = "INSERT INTO python_project.test (Name) VALUES (?)", (user)
print("user:", user)
c.execute(mySql_insert_query)
print(c.rowcount, "Record inserted successfully into test table")
def username():
user = input("Please Enter Your Name: " )
insertSQL(user)
test_script.py
import servlet2
servlet2.username()
I was able to established connection to the database, however, once I enter name. It will fail to connect and prompt this error.
OperationalError: Lost connection to MySQL server at 'localhost:3306', system error: Connection not available.
import mysql.connector
from mysql.connector import Error
from mysql.connector import errorcode
try:
connection = mysql.connector.connect(host='127.0.0.1',
database='python_project',
user='root',
password='catdog123',
)
print("Connection Established")
#cursor = connection.cursor()
#cursor.execute("""INSERT INTO test (Name) VALUES ('harry')""")
#connection.commit()
#cursor.close()
except mysql.connector.Error as e:
if e.errno == errorcode.ER_ACCESS_DENIED_ERROR:
print('Somethign is wrong with username or password')
elif e.errno == errorcode.ER_BAD_DB_ERROR:
print('Database does not exist')
else:
print(e)
c = connection.cursor()
def insertSQL(user):
#mySql_insert_query = """INSERT INTO test (Name) VALUES (?)""", user
print("user:", user)
print(c.rowcount, "Record inserted successfully into test table")
c.execute("""INSERT INTO test (Name) VALUES ('{}')""".format(user))
connection.commit()
def username():
user = input("Please Enter Your Name:" )
insertSQL(user)
I manage to solve it by changing the insert statement VALUES to ('{}').format(user)

Python mysql-connector commit not working

So I've been at it for hours, and there is something really weird going on.
I am looping through code to check if a table value has changed. And if so run some code.
When i run the following program everything works totally fine! The 'lock state' goes from Open to Closed as it should be doing.
import time
import mysql.connector
host = '...'
database = '...'
user = '...'
password = '...'
conn = mysql.connector.connect(host=host, database=database, user=user, password=password)
conn.start_transaction(isolation_level='READ COMMITTED')
state = 'Open'
def get_web_lock_request():
try:
cursor = conn.cursor()
cursor.execute("SELECT * FROM `doorlock`.`state`")
state = ''.join(cursor.fetchone())
cursor.close()
return state
except mysql.connector.Error as e:
print 'get_lock_state ' + format(e)
return ''
def set_web_lock_request():
try:
cursor = conn.cursor()
cursor.execute("UPDATE `doorlock`.`state` SET `state`='" + state + "'")
conn.commit()
cursor.close()
except mysql.connector.Error as e:
print 'set_lock_state: ' + format(e)
while 1:
dbstate = get_web_lock_request()
set_web_lock_request()
if state == 'Open':
state = 'Closed'
else:
state = 'Open'
time.sleep(2)
print dbstate
But in my main code this 'listening for a change' is in a thread. And for some reason the UPDATE statement does not work! even with the conn.commit()
import mysql.connector
conn = mysql.connector.connect(host=host, database=database, user=user, password=password)
conn.start_transaction(isolation_level='READ COMMITTED')
def get_web_lock_request():
try:
cursor = conn.cursor()
cursor.execute("SELECT * FROM `doorlock`.`state`")
state = ''.join(cursor.fetchone())
cursor.close()
return state
except mysql.connector.Error as e:
print 'get_lock_state ' + format(e)
return ''
def set_web_lock_request(state):
try:
cursor = conn.cursor()
cursor.execute("UPDATE `doorlock`.`state` SET `state`='" + state + "'")
conn.commit()
cursor.close()
except mysql.connector.Error as e:
print 'set_lock_state: ' + format(e)
def web_request_listener():
global updating
try:
while 1:
print 'get_web_lock_request : ' + get_web_lock_request()
print 'current_state : ' + current_state()
if get_web_lock_request() != current_state() and not updating:
if get_web_lock_request() == 'Open':
open_lock()
print 'DoorLock: State is now open'
elif get_web_lock_request() == 'Closed':
close_lock()
print 'DoorLock: State is now closed'
time.sleep(1)
except KeyboardInterrupt:
exit()
if __name__=='__main__':
try:
button_listener_thread = threading.Thread(target=button_listener)
web_request_listener_thread = threading.Thread(target=web_request_listener)
except KeyboardInterrupt:
exit()
I could really use some help, I have literallyhave nothing else to try :)
Floris

Python and Flask local variable 'cursor' referenced before assignment

sorry for my english google translator.
I'm learning python and flask and I have a problem in a function.
#app.route('/addProperty',methods=['POST'])
def addProperty():
try:
if session.get('user'):
_tag = request.form['inputTag']
_idCategoria = request.form['inputIdCategoria']
_descricaoBem = request.form['inputDescricaoBem']
_valor = request.form['inputValor']
_fornecedor = request.form['inputFornecedor']
_nfNumero = resquest.form['inputNfNumero']
_nfSerie = request.form['inputNfSerie']
_dtCompra = resquest.form['inputDtCompra']
_departamento = request.form['inputDepartamento']
_local = request.form['inputLocal']
_responsavel = resquest.form['inputResponsavel']
_estadoBem = request.form['inputEstadoBem']
_dtUltimaRev = request.form['inputDtUltimaRev']
_dtProximaRev = request.form['inputDtProximaRev']
_idade = request.form['inputIdade']
_vidaUtil = request.form['inputVidaUtil']
_trocarReformar = request.form['inputTrocaReforma']
_valorTT = request.form['inputValorTT']
curDb.callproc('sp_addProperty',(_tag,_idCategoria,_descricaoBem,_valor,_fornecedor,
_nfNumero,_nfSerie,_dtCompra,_departamento,_local,
_responsavel,_estadoBem,_dtUltimaRev,_dtProximaRev,
_idade,_vidaUtil,_trocarReformar,_valorTT))
conn = mysql.connect()
cursor = conn.cursor()
cursor.callproc('sp_addProperty',(_departamento,_user))
data = cursor.fetchall()
if len(data) is 0:
conectaDb.commit()
return redirect('/userHome')
else:
return render_template('error.html',error = 'An error occurred!')
else:
return render_template('error.html',error = 'Unauthorized Access')
except Exception as e:
return render_template('error.html',error = str(e))
finally:
cursor.close()
conn.close()
This function is returning the following error:
UnboundLocalError: local variable 'cursor' referenced before assignment
And the function below is the same structure and does not return error.
#app.route('/addStatus',methods=['POST'])
def addStatus():
try:
if session.get('user'):
_status = request.form['inputStatus']
_user = session.get('user')
conn = mysql.connect()
cursor = conn.cursor()
cursor.callproc('sp_addStatus',(_status,_user))
data = cursor.fetchall()
if len(data) is 0:
conn.commit()
return redirect('/userHome')
else:
return render_template('error.html',error = 'An error occurred!')
else:
return render_template('error.html',error = 'Unauthorized Access')
except Exception as e:
return render_template('error.html',error = str(e))
finally:
cursor.close()
conn.close()
I would be very grateful if someone can help me with this problem.
I was assigning the parameter request.form the aliases incorrectly.
Some aliases was getting request.form where the right would request.form

Categories

Resources