mysqldb duplicate entry error handling

Discussion in 'Python' started by baur79, Feb 1, 2007.

  1. baur79

    baur79 Guest

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

    thanks for your help

    Baurzhan Zhakashev
    Kazakhstan / Shymkent city
    baur79, Feb 1, 2007
    #1
    1. Advertising

  2. baur79

    Chris Mellon Guest

    On 1 Feb 2007 10:17:31 -0800, baur79 <> 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 '' for key 1")
    >
    > thanks for your help
    >
    > Baurzhan Zhakashev
    > Kazakhstan / Shymkent city
    >
    > --


    If you want to catch IntegrityError, why are you actually catching
    IndentationError?
    Chris Mellon, Feb 1, 2007
    #2
    1. Advertising

  3. baur79

    baur79 Guest

    now it gives this error

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

    any idea
    i have python 2.3.2 installed
    baur79, Feb 1, 2007
    #3
  4. baur79

    Chris Mellon Guest

    On 1 Feb 2007 10:51:09 -0800, baur79 <> 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.
    Chris Mellon, Feb 1, 2007
    #4
  5. On 1 Feb 2007 10:17:31 -0800, "baur79" <> 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 '' for key 1")
    >

    So show us the schema for the database... My take: Your database
    ALREADY HAS a record with that "" value AND emails.email
    is defined as a unique key field.
    --
    Wulfraed Dennis Lee Bieber KD6MOG

    HTTP://wlfraed.home.netcom.com/
    (Bestiaria Support Staff: )
    HTTP://www.bestiaria.com/
    Dennis Lee Bieber, Feb 2, 2007
    #5
  6. baur79

    John Nagle Guest

    Dennis Lee Bieber wrote:
    > On 1 Feb 2007 10:17:31 -0800, "baur79" <> declaimed the
    > following in comp.lang.python:


    >>IntegrityError: (1062, "Duplicate entry '' for key 1")

    >
    > So show us the schema for the database... My take: Your database
    > ALREADY HAS a record with that "" 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
    John Nagle, Feb 2, 2007
    #6
  7. baur79

    baur79 Guest

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

    thanks everybody again

    Baurzhan Zhakashev
    Kazakhstan / Shymkent city
    baur79, Feb 2, 2007
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Darrel
    Replies:
    5
    Views:
    810
    darrel
    Dec 6, 2004
  2. Replies:
    0
    Views:
    4,015
  3. richard
    Replies:
    6
    Views:
    393
    Sherman Pendley
    Aug 3, 2008
  4. Replies:
    2
    Views:
    707
    James Kanze
    Dec 28, 2008
  5. mike
    Replies:
    6
    Views:
    216
    Tad McClellan
    Apr 22, 2005
Loading...

Share This Page