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

fyleow fyleow at gmail.com
Wed Jun 14 01:24:59 CEST 2006


Here's the code I have in place right now.

cursor.execute("SELECT link FROM feeds_feed WHERE link=%s",
(self.link,))
            db.commit()

            if cursor.rowcount == 0:
                cursor.execute("INSERT INTO feeds_feed (release_group,
title, link, category, pub_date) VALUES (%s, %s, %s, %s, now())",
(self.group, self.title, self.link, self.category,))
                db.commit()
                print "Inserting"
            else:
                print "Already Exists"

Basically the uniqueness of the link attribute is enforced in the
database so if I try to insert a value that already exists I get an
error that breaks the program.  Right now I do a SELECT query first to
check if the value already exists.  The insert is only executed only if
the value doesn't already exist.

This is a really bad way of doing things because I'm accessing the DB
way more than I need to.  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.  I'm not really interested if
a particular value is rejected or added, I just want the most efficient
way to insert values.

I would really appreciate any suggestions or tips.

Thanks.




More information about the Python-list mailing list