2

Is there any special behavior when decrementing a variable in the except clause? sid keeps incrementing until it first gets into the exception clause, then it just keeps the same value for the rest duration of the for loop.

7 out of 105 tries throw an exception

there is no printout "Fehlercode:", errorcode

Here's my code:

for bid in range(bidStart, bidEnd + 1):
    for syn in getSynsProBeitrag(bid):
        try:
            sid += 1
            query = "INSERT INTO zuord (bid, hid, sid) VALUES(%s, %s, %s)"
            cursor.execute(query, [bid, hid, sid])
            query2 = "INSERT INTO synonyme (synonym) VALUE (%s)"
            cursor.execute(query2, syn)

        except MySQLdb.IntegrityError, message:
            errorcode = message[0]      
            if errorcode == 1062:       
                sid -= 1
                print sid
            else:
                print "Fehlercode:", errorcode

solved: after the query2 throws it's first exception the first query is causing also an (the same) IntegrityError and then it just goes back and forth like colleen said

Mirko
  • 67
  • 5

2 Answers2

1

Well you're decrementing it in the except clause, and then incrementing it in the try, so it's just going back and forth.... if it fails the first time, it's going to keep failing.

e.g.

try:
1
try:
1+1=2 ->fail->id-1=1
try:
1+1=2 ->fail->id-1=1
try:
1+1=2 ->fail->id-1=1
try:
1+1=2 ->fail->id-1=1
....

etc.

If you're trying to skip the id that failed, don't decrement it.

Colleen
  • 23,899
  • 12
  • 45
  • 75
  • hi, I tried to just don't decrement it when the exception is thrown but then I have gaps in the ID's. the exception gets thrown when execution query2 (the table(column) has an unique index) – Mirko Nov 20 '12 at 19:28
  • I'm confused by what you mean by "when execution query2 (the table(column) has an unique index"-- if you know the problem is coming from query2 only, then put the try/except just around query2 and move the increment outside the try. – Colleen Nov 20 '12 at 20:22
0

There isn't any special behavior when decrementing in the except clause.

Is it because the same exception keeps getting thrown so it keeps decrementing? I notice you are only printing it out when you get the same errorcode, are you ever getting printouts for "Fehlercode: errorcode"?

David Yen
  • 78
  • 1
  • 7
  • hi, thanks for your answers, the exception gets thrown 7 out of 105 times, I just don't understand why the value isn't changing anymore after the first exception, I get no printouts for "Fehlercode:" errorcode – Mirko Nov 20 '12 at 19:02