Question

I have a python script which downloads certain data from the internet (from a list of URLS) and stores it in a MySQL database. My code is something along these lines:

try:
    con = mdb.connect(host = 'localhost', user = 'root', passwd = 'gatech', db ='SpecialProb', charset = 'utf8');
    cur = con.cursor()
    print " Database Connection Parameters set"
except mdb.Error, e:  
    print "Error %d: %s" % (e.args[0],e.args[1])
    sys.exit(1)
try:
    for item in listURLs[index:]:
        try:
            r2 = requests.get(item)
        except:
            print "Could not fetch data"
            print "Exception: ", sys.exc_info()[0]
            continue

         ## Some code to fetch Name, ID from the internet, which I have tested and which works correctly. ##
         try:
             cur.execute("INSERT into TABLENAME (NAME, ID" 
             ") VALUES (%s, %s)",
             (totalName, ID))

             print("Value inserted in database.\n")
             countInsert += 1;
             con.commit() 
         except mdb.IntegrityError:
             print "Most likely cause: Database already contains this entry."
             print "Message from system: "
             print "Error %d: %s\n" % (e.args[0],e.args[1])
             continue
except:
    print "'For loop exception: ", sys.exc_info()[0]        
    sys.exit(0)

The data which I fetch could be duplicate and I don't want the duplicate data to be inserted into the database and the code should go over the next iteration etch the next data instead of storing the data. So I have the except mdb.IntegrityError: line there which takes care of the duplicates.

However, after catching the exception for the duplicate entry the code, instead of going over the next iteration goes to the except which I have set for the for loop. Here's what I get:

Most likely cause: Database already contains this entry. 
Message from system:
'For loop exception:  <type 'exceptions.NameError'>

Why does this happen? And how do I prevent from happening this?

Thanks a lot!

Was it helpful?

Solution

Your e would not be defined causing exception in your except block. Use mdb.IntegrityError,e like you did for except mdb.Error, e

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top