473,396 Members | 1,847 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,396 software developers and data experts.

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.connect(host = "localhost", user = db_user, passwd =
db_pass, db = db_name)

try:
db.query(sql)
except IndentationError:
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.com' for key 1")

thanks for your help

Baurzhan Zhakashev
Kazakhstan / Shymkent city

Feb 1 '07 #1
5 22924
On 1 Feb 2007 10:17:31 -0800, baur79 <ba****@gmail.comwrote:
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.connect(host = "localhost", user = db_user, passwd =
db_pass, db = db_name)

try:
db.query(sql)
except IndentationError:
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.com' for key 1")

thanks for your help

Baurzhan Zhakashev
Kazakhstan / Shymkent city

--
If you want to catch IntegrityError, why are you actually catching
IndentationError?
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.comwrote:
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.comdeclaimed the
following in comp.lang.python:
>>IntegrityError: (1062, "Duplicate entry 'e****@domain.com' for key 1")

So show us the schema for the database... My take: Your database
ALREADY HAS a record with that "em***@domain.com" 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
kmysqlduplicateentry = 1062 # MySQL error code when INSERT finds a duplicate
try :
... # do INSERT
except MySQLdb.IntegrityError, message:
errorcode = message[0] # get MySQL error code
if errorcode == kmysqlduplicateentry : # if duplicate
... # handle duplicate
else:
raise # unexpected error, reraise
John Nagle
Feb 2 '07 #5
thanks John
this solves my problem
except MySQLdb.IntegrityError, 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
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...
0
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....
3
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?...
6
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...
1
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? ...
8
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...
4
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...
3
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...
1
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 :...
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: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
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...

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.