I looked at other issues regarding this particular error, but in those cases there was a route in the process where the variable is never defined and therefore fails when called later. However, with my program, I set the variables before the try method, these variables are changed in the try method according to certain criteria, and then the if statement calls them later, but it does not work. When Python reaches the if statement and tries to create variable text, it claims that the error and error1 variables are undefined (mentioned before the assignment).
err = 0
error = 'No Error Set'
error1 = 'No Error1 Set'
try:
conn = sqlite3.connect(db_dir)
for row in conn.execute("select max(date) from eod"):
mxdate = row
if mxdate == None:
pass
else:
for row in conn.execute('select date, status from eod where date = ?',(mxdate,)):
_lst_eod = row
for row in conn.execute("select * from temp"):
_crnt_eod = row
conn.close()
except Exception as error:
error = str(error)
err = 1
logged = 0
try:
conn.close()
time = str(right_datetime())[11:19].replace(':','')
conn = sqlite3.connect(db_dir)
conn.execute("insert into error_log (date, user, auth, error, _action) values (?,?,?,?,'Failed to select from eod/temp tables.')",(int(str(_date)+time),cred[0],cred[1],error,))
conn.commit()
conn.close()
logged = 1
except Exception as error1:
error1 = str(error1)
if err == 1:
text = '##Error## An error occured while trying to end the day:\n'+error
if logged == 0:
text = text+'\n\nA row was written to the error log.'
else:
text = text+'\n\nWrite to Error Log failed due to error:\n'+error1
else:
....carry on with the rest of the program.
source
share