How do I ignore the errors thrown by the DB api?

Ben Finney bignose+hates-spam at benfinney.id.au
Tue Jun 13 22:30:04 EDT 2006


"fyleow" <fyleow at gmail.com> writes:

> It would be great if I could just insert values into the DB and let
> the uniqueness check at the DB level to either add or refuse the
> duplicate value.

The Pythonic way to do this is find out what exception is generated by
the event you want to handle, and handle that exception.

    record_stuff = build_new_record()
    try:
        insert_record(record_stuff)
    except FailedToInsert, e:
        handle_insert_failure(e, record_stuff)

Define each of those functions, name the actual exception class, and
you're done.

-- 
 \       "A lot of people are afraid of heights. Not me, I'm afraid of |
  `\                                        widths."  -- Steven Wright |
_o__)                                                                  |
Ben Finney




More information about the Python-list mailing list