0

My project is to compare file names located on a folder with names saved in a PostgreSQL database.

  1. 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.

Chris
  • 29
  • 7
  • Sorry, I have updated the missing part. – Chris Sep 03 '19 at 13:29
  • It should be `try:` and indentation is relevant in Python. This code still gives a SyntaxError. – walnut Sep 03 '19 at 13:33
  • I know. It was difficult to format in SO editor. I assumed that a single snipet is sufficient. The code is not going to work with the database access anyway. – Chris Sep 03 '19 at 13:52
  • The syntax was and somewhat still is ambiguous the way you wrote it. It is not clear what you problem is. If you want to continue after `fetchmany` throws an exception, then why are you putting the `try-except` block around the whole `for` loop, instead of around the `fetchmany` call? – walnut Sep 03 '19 at 13:59
  • I changed my query to retrieve a number of records first. cur.rowcount. Then, I can handle incoming parameters without resorting to except condition. – Chris Sep 04 '19 at 13:34
  • I have an async module that takes care of all of this for you located here : https://stackoverflow.com/questions/48532301/python-postgres-psycopg2-threadedconnectionpool-exhausted/49366850#49366850 – eatmeimadanish Sep 06 '19 at 21:07

1 Answers1

0
    for k, v in files_dict.items():
        cur.execute(report_query, (k,v))

        if cur.rowcount == 0:
            print("The file is not in the database: " + v)
        else:
            query_result = cur.fetchmany(3)

            sta = str(query_result[0])

            file_name_length = len(sta)
            q_ry = sta[2: file_name_length-3]

            if v.lower() == q_ry.lower():
                pass

            else:
                print("no match after comparing names" + v)

except (Exception, psycopg2.Error) as error:
    print("Error fetching data from PG table " + k, error)

finally:
    cur.close()
Chris
  • 29
  • 7