Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > mysqldb duplicate entry error handling

Reply
Thread Tools

mysqldb duplicate entry error handling

 
 
baur79
Guest
Posts: n/a
 
      02-01-2007
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-Mail Removed)' for key 1")

thanks for your help

Baurzhan Zhakashev
Kazakhstan / Shymkent city

 
Reply With Quote
 
 
 
 
Chris Mellon
Guest
Posts: n/a
 
      02-01-2007
On 1 Feb 2007 10:17:31 -0800, baur79 <(E-Mail Removed)> wrote:
> 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-Mail Removed)' for key 1")
>
> thanks for your help
>
> Baurzhan Zhakashev
> Kazakhstan / Shymkent city
>
> --


If you want to catch IntegrityError, why are you actually catching
IndentationError?
 
Reply With Quote
 
 
 
 
baur79
Guest
Posts: n/a
 
      02-01-2007
now it gives this error

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

any idea
i have python 2.3.2 installed

 
Reply With Quote
 
Chris Mellon
Guest
Posts: n/a
 
      02-01-2007
On 1 Feb 2007 10:51:09 -0800, baur79 <(E-Mail Removed)> wrote:
> 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.
 
Reply With Quote
 
Dennis Lee Bieber
Guest
Posts: n/a
 
      02-02-2007
On 1 Feb 2007 10:17:31 -0800, "baur79" <(E-Mail Removed)> declaimed the
following in comp.lang.python:

> 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:


What's with "IndentationError" -- I know of NO database that would
ever raise such a condition.

> print "duplicate"
> pass
>


And what database adapter are you using. Normal/recommended MySQLdb
would look like:

def email_insert_in_db(email):
conn = MySQLdb.connect(host=...)
crsr = conn.cursor()
try:
crsr.execute("insert into emails (email) values (%s)",
email)
conn.commit()
except ????:
conn.rollback()
crsr.close()
conn.close()

> IntegrityError: (1062, "Duplicate entry '(E-Mail Removed)' for key 1")
>

So show us the schema for the database... My take: Your database
ALREADY HAS a record with that "(E-Mail Removed)" value AND emails.email
is defined as a unique key field.
--
Wulfraed Dennis Lee Bieber KD6MOG
http://www.velocityreviews.com/forums/(E-Mail Removed) (E-Mail Removed)
HTTP://wlfraed.home.netcom.com/
(Bestiaria Support Staff: (E-Mail Removed))
HTTP://www.bestiaria.com/
 
Reply With Quote
 
John Nagle
Guest
Posts: n/a
 
      02-02-2007
Dennis Lee Bieber wrote:
> On 1 Feb 2007 10:17:31 -0800, "baur79" <(E-Mail Removed)> declaimed the
> following in comp.lang.python:


>>IntegrityError: (1062, "Duplicate entry '(E-Mail Removed)' for key 1")

>
> So show us the schema for the database... My take: Your database
> ALREADY HAS a record with that "(E-Mail Removed)" 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
 
Reply With Quote
 
baur79
Guest
Posts: n/a
 
      02-02-2007
thanks John
this solves my problem
except MySQLdb.IntegrityError, message:

thanks everybody again

Baurzhan Zhakashev
Kazakhstan / Shymkent city

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Duplicate entry while reading from ifstream into vector steve.cpp@gmail.com C++ 2 12-28-2008 01:33 PM
Stumped! duplicate entry won't go away! richard HTML 6 08-03-2008 02:23 PM
Prevent duplicate entry to MS Access database mmazid@googlemail.com ASP .Net 0 02-08-2006 08:40 PM
preventing duplicate data entry on RELOAD Darrel ASP .Net 5 12-06-2004 04:42 PM
error handling-duplicate key monika ASP General 1 09-16-2003 12:59 PM



Advertisments