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 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…