mysqldb duplicate entry error handling

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • baur79

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

    try:
    db.query(sql)
    except IndentationErro r:
    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 'email@domain.c om' for key 1")

    thanks for your help

    Baurzhan Zhakashev
    Kazakhstan / Shymkent city

  • Chris Mellon

    #2
    Re: mysqldb duplicate entry error handling

    On 1 Feb 2007 10:17:31 -0800, baur79 <baur79@gmail.c omwrote:
    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.conne ct(host = "localhost" , user = db_user, passwd =
    db_pass, db = db_name)
    >
    try:
    db.query(sql)
    except IndentationErro r:
    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 'email@domain.c om' for key 1")
    >
    thanks for your help
    >
    Baurzhan Zhakashev
    Kazakhstan / Shymkent city
    >
    --
    If you want to catch IntegrityError, why are you actually catching
    IndentationErro r?

    Comment

    • baur79

      #3
      Re: mysqldb duplicate entry error handling

      now it gives this error

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

      any idea
      i have python 2.3.2 installed

      Comment

      • Chris Mellon

        #4
        Re: mysqldb duplicate entry error handling

        On 1 Feb 2007 10:51:09 -0800, baur79 <baur79@gmail.c omwrote:
        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.

        Comment

        • John Nagle

          #5
          Re: mysqldb duplicate entry error handling

          Dennis Lee Bieber wrote:
          On 1 Feb 2007 10:17:31 -0800, "baur79" <baur79@gmail.c omdeclaimed the
          following in comp.lang.pytho n:
          >>IntegrityErro r: (1062, "Duplicate entry 'email@domain.c om' for key 1")
          >
          So show us the schema for the database... My take: Your database
          ALREADY HAS a record with that "email@domain.c om" 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
          kmysqlduplicate entry = 1062 # MySQL error code when INSERT finds a duplicate
          try :
          ... # do INSERT
          except MySQLdb.Integri tyError, message:
          errorcode = message[0] # get MySQL error code
          if errorcode == kmysqlduplicate entry : # if duplicate
          ... # handle duplicate
          else:
          raise # unexpected error, reraise


          John Nagle

          Comment

          • baur79

            #6
            Re: mysqldb duplicate entry error handling

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

            thanks everybody again

            Baurzhan Zhakashev
            Kazakhstan / Shymkent city

            Comment

            Working...