473,387 Members | 1,504 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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

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.

Jun 13 '06 #1
1 1302
"fyleow" <fy****@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

Jun 14 '06 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Thomas Bartkus | last post by:
The meaning of REPLACE INTO is clear to me. IF the new record presents new key values, then it is inserted as a new record. IF the new record has key values that match a pre-existing record, then...
12
by: Gary | last post by:
Hi! guys, I have a SQL agent job fails because it gets 10 warnings when it runs a stored procedure. These warnings are trivial and can be ignored. Can I make it ignore these warnings and...
1
by: Ersin Gençtürk | last post by:
We couldn't find why these errors happen.They doesn't appear everytime.And they appear different pages at different times.Is there somebody know why these happenes ? First user gets this error :...
2
by: George1776 | last post by:
All, I've recently upgraded our production ASP.NET/C# application from framework 1.1 to 2.0. Since then I've been plagued by out-of-memory errors and problems with the cache object (which may...
9
by: Justme | last post by:
Novice programmer needs help with using fgets to read and ignore the first two lines of a file. I've gone thru the previous posting regarding fgets, but none of them seems to help my situation. I...
7
by: iKiLL | last post by:
Hi all I am still pretty new to .Net and C#. I have come from a VB6 Background.
7
by: brett.estabrook | last post by:
I have written a multi-threaded c# windows service in .net 1.1 (Visual Studio .net 2003). The service has several threads that poll a Sql 6.5 database on another machine. Each thread will execute a...
0
by: Luc The Perverse | last post by:
I am designing an application which will upload files to a remote server (incidentally HTTP post if that makes any difference). This is a "client application" which, hopefully, hundreds of people...
1
by: tembil | last post by:
Hi, here's the query that I used I need to know how to ignore errors when executing LOAD DATA infile 'myFile12_out.txt' INTO TABLE itc_db.tbl_address_out fields TERMINATED BY '|' lines...
3
by: Michael Sharman | last post by:
Hi guys, I'm somewhat new to PHP, so please excuse what I hope is an easy question. I've experienced some issues lately where the code on my development environment throws no errors, but when...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.