FAQ
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.

Search Discussions

  • Ben Finney at Jun 14, 2006 at 2:30 am

    "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

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
grouppython-list @
categoriespython
postedJun 13, '06 at 11:24p
activeJun 14, '06 at 2:30a
posts2
users2
websitepython.org

2 users in discussion

Fyleow: 1 post Ben Finney: 1 post

People

Translate

site design / logo © 2022 Grokbase