Python and Flask local variable 'cursor' referenced before assignment - python

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

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.

Pyodbc if connection fails retry

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!

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

ChromeSessionParser syntax issue

I am running into an issue with parts of my code i have added my errors at the bottom. The issue is arising around the sqllite3.operationError part. i attempted removing it but when i do another error occurs for line 68 'def getpath():', i cant see why the errors are showing up any and all help is appreciated as always thanks. My code is generally for taking Login data out of my database and displaying in an csv file
import os
import sys
import sqlite3
try:
import win32crypt
except:
pass
import argparse
def args_parser():
parser = argparse.ArgumentParser(description="Retrieve Google Chrome Passwords")
parser.add_argument("--output", help="Output to csv file", action="store_true")
args = parser.parse_args()
if args.output:
csv(main())
else:
for data in main():
print (data)
def main():
info_list = []
path = getpath()
try:
connection = sqlite3.connect(path + "Login Data")
with connection:
cursor = connection.cursor()
v = cursor.execute('SELECT action_url, username_value, password_value FROM logins')
value = v.fetchall
for information in value:
if os.name == 'nt':
password = win32crypt.CryptUnprotectData(information[2], None, None, None, 0)[1]
if password:
info_list.append({
'origin_url': information[0],
'username': information[1],
'password': str(password)
})
except sqlite3.OperationalError as e:
e = str(e)
if (e == 'database is locked'):
print('[!] Make sure Google Chrome is not running in the background')
sys.exit(0)
elif (e == 'no such table: logins'):
print('[!] Something wrong with the database name')
sys.exit(0)
elif (e == 'unable to open database file'):
print('[!] Something wrong with the database path')
sys.exit(0)
else:
print (e)
sys.exit(0)
return info_list
def getpath():
if os.name == "nt":
# This is the Windows Path
PathName = os.getenv('localappdata') + '\\Google\\Chrome\\User Data\\Default\\'
if (os.path.isdir(PathName) == False):
print('[!] Chrome Doesn\'t exists')
sys.exit(0)
return PathName
def csv (info):
with open ('chromepass.csv', 'wb') as csv_file:
csv_file.write('origin_url,username,password \n' .encode('utf'))
for data in info:
csv_file.write(('%s, %s, %s \n' % (data['origin_url'], data['username'], data['password'])).encode('utf-8'))
print ("Data written to Chromepass.csv")
if __name__ == '__main__':
args_parser()
Errors
Traceback (most recent call last):
File "C:/Users/Lewis Collins/Python Project/ChromeDB's/ChromeSessionParser.py", line 90, in <module>
args_parser()
File "C:/Users/Lewis Collins/Python Project/ChromeDB's/ChromeSessionParser.py", line 19, in args_parser
for data in main():
File "C:/Users/Lewis Collins/Python Project/ChromeDB's/ChromeSessionParser.py", line 35, in main
for information in value:
TypeError: 'builtin_function_or_method' object is not iterable
Right way is:
except sqlite3.OperationalError as e:
And you main() should be like:
def main():
info_list = []
path = getpath()
try:
connection = sqlite3.connect(path + "Login Data")
with connection:
cursor = connection.cursor()
v = cursor.execute('SELECT action_url, username_value, password_value FROM logins')
value = v.fetchall
for information in value:
if os.name == 'nt':
password = win32crypt.CryptUnprotectData(information[2], None, None, None, 0)[1]
if password:
info_list.append({
'origin_url': information[0],
'username': information[1],
'password': str(password)
})
except sqlite3.OperationalError as e:
e = str(e)
if (e == 'database is locked'):
print '[!] Make sure Google Chrome is not running in the background'
sys.exit(0)
elif (e == 'no such table: logins'):
print '[!] Something wrong with the database name'
sys.exit(0)
elif (e == 'unable to open database file'):
print '[!] Something wrong with the database path'
sys.exit(0)
else:
print e
sys.exit(0)
return info_list

Categories

Resources