By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
431,816 Members | 1,332 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 431,816 IT Pros & Developers. It's quick & easy.

mysqldb duplicate entry error handling

P: n/a
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
Share this Question
Share on Google+
5 Replies


P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
thanks John
this solves my problem
except MySQLdb.IntegrityError, message:

thanks everybody again

Baurzhan Zhakashev
Kazakhstan / Shymkent city

Feb 2 '07 #6

This discussion thread is closed

Replies have been disabled for this discussion.