How to access psycopg2 error wrapped in sqlalchemy error

前端 未结 2 813
没有蜡笔的小新
没有蜡笔的小新 2020-12-21 14:21

I\'m uploading a pandas data frame to a table in Postgres using SQLalchemy and psycopg2. How do I access the psycopg2 error that is within the SQLalchemy error?

I wa

2条回答
  •  没有蜡笔的小新
    2020-12-21 15:02

    Here is my final code for reference:

    try:
        df.to_sql(name='sql_table', con=engine, if_exists='append', index=False)
    except exc.DBAPIError as ex:
        if ex.orig.pgcode == '23502':
            print("Data could not be uploaded to sql_table: " + ex.orig.diag.message_primary)
        else:
            raise
    

提交回复
热议问题