473,769 Members | 5,724 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

mysqldb duplicate entry error handling

Hi guys
i try to run this code in loop and to pass even the entry is
duplicated

def email_insert_in _db(email):
sql="INSERT INTO emails (email) values ('%s') "%(email)
db=_mysql.conne ct(host = "localhost" , user = db_user, passwd =
db_pass, db = db_name)

try:
db.query(sql)
except IndentationErro r:
print "duplicate"
pass

also try to (raise, continue)
but can't continue in loop

error output is:
File "inser_in_db.py ", line 85, in email_insert_in _db
db.query(sql)
IntegrityError: (1062, "Duplicate entry 'e****@domain.c om' for key 1")

thanks for your help

Baurzhan Zhakashev
Kazakhstan / Shymkent city

Feb 1 '07 #1
5 23042
On 1 Feb 2007 10:17:31 -0800, baur79 <ba****@gmail.c omwrote:
Hi guys
i try to run this code in loop and to pass even the entry is
duplicated

def email_insert_in _db(email):
sql="INSERT INTO emails (email) values ('%s') "%(email)
db=_mysql.conne ct(host = "localhost" , user = db_user, passwd =
db_pass, db = db_name)

try:
db.query(sql)
except IndentationErro r:
print "duplicate"
pass

also try to (raise, continue)
but can't continue in loop

error output is:
File "inser_in_db.py ", line 85, in email_insert_in _db
db.query(sql)
IntegrityError: (1062, "Duplicate entry 'e****@domain.c om' for key 1")

thanks for your help

Baurzhan Zhakashev
Kazakhstan / Shymkent city

--
If you want to catch IntegrityError, why are you actually catching
IndentationErro r?
Feb 1 '07 #2
now it gives this error

except IntegrityError, NameError:
NameError: global name 'IntegrityError ' is not defined

any idea
i have python 2.3.2 installed

Feb 1 '07 #3
On 1 Feb 2007 10:51:09 -0800, baur79 <ba****@gmail.c omwrote:
now it gives this error

except IntegrityError, NameError:
NameError: global name 'IntegrityError ' is not defined

any idea
i have python 2.3.2 installed
IntegrityError will most likely be defined in the namespace of
whatever you're using for MySQL access.
Feb 1 '07 #4
Dennis Lee Bieber wrote:
On 1 Feb 2007 10:17:31 -0800, "baur79" <ba****@gmail.c omdeclaimed the
following in comp.lang.pytho n:
>>IntegrityErro r: (1062, "Duplicate entry 'e****@domain.c om' for key 1")

So show us the schema for the database... My take: Your database
ALREADY HAS a record with that "em***@domain.c om" value AND emails.email
is defined as a unique key field.
Right. That's not a bug; it's supposed to do that when you try
to add a duplicate. What do you want to happen?

If you want to allow duplicates, remove the UNIQUE on that
field from the schema.

If you want the new entry to replace the old entry, use REPLACE
instead of INSERT.

If you just want to detect the problem, provide

import MySQLdb
kmysqlduplicate entry = 1062 # MySQL error code when INSERT finds a duplicate
try :
... # do INSERT
except MySQLdb.Integri tyError, message:
errorcode = message[0] # get MySQL error code
if errorcode == kmysqlduplicate entry : # if duplicate
... # handle duplicate
else:
raise # unexpected error, reraise
John Nagle
Feb 2 '07 #5
thanks John
this solves my problem
except MySQLdb.Integri tyError, message:

thanks everybody again

Baurzhan Zhakashev
Kazakhstan / Shymkent city

Feb 2 '07 #6

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

Similar topics

1
2859
by: monika | last post by:
hi ... I want to do error handling in my application. I have made a complete application. but when I encounter errors (like mentioned below) I want to do error handling. how can I do it? I mean like in this case I can see if the record is being added duplicate then I can always check for duplicate records and throw a message. but I will face so many errors like this??? do we have a list ..whereby I can get all the error numbers and...
0
3111
by: Gary Lundquest | last post by:
I have an application with MS Access 2000 as the front end and MySQL as the back end. All was well until I upgraded the MySQL (Linux) server. The Problem: I insert data into a cumulative table. Before when I did this, and there were duplicate entries, the duplicate entries were rejected and I got a return code with the number of affected rows (number added). Now, I get a MyODBC error and the application stops when trying to add...
3
3097
by: WindAndWaves | last post by:
I am writing error handling procedures at the moment. Here are some questions: 1. Can you write a procedure that picks up any error and deals with it no matter where it happens in the database? 2. Does Access keep a list of errors in a "secret" table? 3. Is there a way to handle common errors (such as - "you need to choose from the list" or "a related record is required in ....") with just one
6
3492
by: John J. Hughes II | last post by:
Normally when I use the Adaptor update function and it work fine but sometimes I get a "duplicate entry" error after the merge. I believe the problem is because the primary key is modified and the merge function is unable compensate for this event. I see three possible solutions to this problem. One being not to let them update the key which is not an option. Two being to determine if the key has changed and refilling the entire dataset...
1
6434
by: Joseph Chase | last post by:
I am running version 4.1.13a-log on a Mac XServe. How can I receive a 'duplicate entry' error for an UPDATE? An update isn't creating an entry, so why this error message? ____________________________________________________ 060427 11:57:23 Slave SQL thread initialized, starting replication in log 'kiosk1-bin.000006' at position 7233, relay log './royaldemo-2-relay-bin.000007' position: 7321 060427 11:57:23 Slave: Query caused...
8
3949
by: Iona | last post by:
Hi Allan, I'm using a nifty piece of code you put on here some time back to do a duplicate entry check as below. I'm using to check for duplicate names. However I am getting an error message on this line: Set rs = db.OpenRecordset("SELECT ID FROM Contacts WHERE (" & sWhere & ");") Contacts being the main table. I am using access 2003 The error message states that there are; Too few parameters. Expected 1 I have no idea how to...
4
9491
by: anisu | last post by:
Hi, I am trying to do error handling during insert in MS Access 2002 (OS: MS XP) The problem is that when a duplicate record is added for the primary key field or a null value included in a unique filed, an error appears: "Microsoft Access cannot append all the records in the append query!" What I want is to suppress this message and display my own message, but both this message and mine one comes while I do the error handling. ...
3
2073
by: patelxxx | last post by:
I'm trying to update a template on our Content Management System, however getting the following error. I know its difficult to know what the problem is without looking at the PERL coding, however any advise would be considered: DBD::mysql::db do failed: Duplicate entry 'Lisa.Wulf@<RMed By Mod>' for key 2 at cgi-bin/gen_main.pl line 87 main::__ANON__('DBD::mysql::db do failed: Duplicate entry \'Lisa.Wulf@<RMed By Mod>...',...
1
2251
by: waqasahmed996 | last post by:
Hi I am facing a strange problem. I have a duplicate entry message even though i am using IGNORE in query. My query is insert ignore into table1 select * from table2 Error is: Error Code : 1062 Duplicate entry '1450011-DE' for key 1
0
9589
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10211
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9863
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8870
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7408
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6673
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
3958
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3561
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2815
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.