My project is to compare file names located on a folder with names saved in a PostgreSQL database.
I looked into different responses on the SO but nothing worked for me. Please see below my comments within the source code.
try cur = db_conn().cursor() report query = "SELECT ev_report FROM stc_events_master WHERE si_station_num = %s AND" + " ev_ins_date BETWEEN to_date(" + "'" + "1/1/2018" + "'" + ",'mm/dd/yyyy')" + " and TO_DATE('12/31/2018', 'mm/dd/yyyy')" # files_dict content-> k = '123p34" , value = "DV03S_120124_5-7-2018.pdf" for k, v in files_dict.items(): cur.execute(report_query, (k,)) # the reports are unique in the database table query_result = cur.fetchmany(1) #incoming value:DV03S_120124_5-7-2018.pdf # query returns: sta = str(query_result[0]) # if a file is not found in the database, the exception happens # and the code below is not executed. It jumps # directly to Exception if len(sta) < 1: print("no report found in the database") print("query_results" + sta) else: if v.lower() == q_ry.lower(): print("match " + v) else: print("no match " + v) except Exception: pass print("In Exception area") finally: cur.close()
I want to continue the loop even if nothing is found the database and save a name of the file from the folder which caused the exception. Now, the code stops after the exception. Thank you for your help.