Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
164 views
in Technique[技术] by (71.8m points)

python - Script doesn't contiinues after Exception with Try/Except:

I have the following function with a Try except in it to end the function if it generates any exception:

def bulk_insert(df, conn, table):
    try:
        print('Inserting dataframe into DB...')
        tuples = [tuple(x) for x in df.to_numpy()]
        cols = ','.join(list(df.columns))
        aux = '(' + '%s,' * (len(tuples[0]) - 1) + '%s)'
        query = "INSERT INTO " + table + "(" + cols + ") VALUES " + aux
        cursor = conn.cursor()
        psycopg2.extras.execute_batch(cursor, query, tuples)
        conn.commit()
        cursor.close()

    except:
        print('Erro: ' + str(sys.exc_info()))
        pass
    
    return 'Done'

Unfortunately I'm having the following exception raised:

Erro: (<class 'psycopg2.errors.InvalidDatetimeFormat'>, InvalidDatetimeFormat('invalid input syntax for type timestamp with time zone: "None"
LINE 1: ...','Sistema','CONSULTORA DE ATENDIMENTO - MISTA',1,'None',5,'...
                                                             ^
'), <traceback object at 0x7f1a4c08afc0>)
Inserting missing keys_elos...
Traceback (most recent call last):
  File "complete_sched.py", line 218, in <module>
    insert_elos_key(conn, table)  # Inserting elos_key
  File "complete_sched.py", line 151, in insert_elos_key
    cursor.execute("""UPDATE """+ table +""" es
psycopg2.errors.InFailedSqlTransaction: current transaction is aborted, commands ignored until end of transaction block

After that the script does not continues.

question from:https://stackoverflow.com/questions/65845684/script-doesnt-contiinues-after-exception-with-try-except

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...