(20210317Solved) Catch the IntegrityError about PostgreSQL in Python

  • Overview

    通过python api向postgresql存入数据出现重复数据,会报错IntegrityError,如何捕捉?

  • Solution

    通过sqlalchemy使用Python API。

    The sqlalchemy wraps the psycopg2 exception to its own exception.

    from sqlalchemy.exc import IntegrityError
    try:
        ...
    except IntegrityError as e:
        print("right")
    
  • References

  1. Why is psycopg2 IntegrityError not being caught?

猜你喜欢

转载自blog.csdn.net/The_Time_Runner/article/details/115261449