mysqldb duplicate entry error handling

B

baur79

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 address removed)' for key 1")

thanks for your help

Baurzhan Zhakashev
Kazakhstan / Shymkent city
 
C

Chris Mellon

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 address 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?
 
B

baur79

now it gives this error

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

any idea
i have python 2.3.2 installed
 
C

Chris Mellon

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.
 
D

Dennis Lee Bieber

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 address removed)' for key 1")
So show us the schema for the database... My take: Your database
ALREADY HAS a record with that "(e-mail address removed)" value AND emails.email
is defined as a unique key field.
--
Wulfraed Dennis Lee Bieber KD6MOG
(e-mail address removed) (e-mail address removed)
HTTP://wlfraed.home.netcom.com/
(Bestiaria Support Staff: (e-mail address removed))
HTTP://www.bestiaria.com/
 
J

John Nagle

Dennis said:
On 1 Feb 2007 10:17:31 -0800, "baur79" <[email protected]> declaimed the
following in comp.lang.python:

So show us the schema for the database... My take: Your database
ALREADY HAS a record with that "(e-mail address 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
 
B

baur79

thanks John
this solves my problem
except MySQLdb.IntegrityError, message:

thanks everybody again

Baurzhan Zhakashev
Kazakhstan / Shymkent city
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top