[Tutor] catching the row that raises IntegrityError in sqlite

Steven D'Aprano steve at pearwood.info
Sun Feb 9 10:39:24 CET 2014


On Sun, Feb 09, 2014 at 12:03:43PM +0530, Sivaram Neelakantan wrote:

>     try:
>         sql = "insert into %s (%s) values(%s)" %(name, cl, cvv)
>         conn.executemany(sql, to_db)
>         dbh.commit()
>     except sq.IntegrityError:
>         print('Record already exists') # but which record???
>         dbh.rollback()

When you get an error, and don't know how to interpret it, the first 
thing to do is to look at the exception and see what it says.

try:
    # as above
except sq.IntegrityError as exception:
    print(exception)
    dbh.rollback()


Often (although not always) the exception will give you details on what 
went wrong. That should always be the first thing to look at.



-- 
Steven


More information about the Tutor mailing list