Handling errors in psycopg2 - one error seems to create more?
donkopotamus' answer is pretty good. It has a problem in that it rolls back the entire transaction, which could definitely be undesirable behavior if you're executing this query as part of a larger transaction block. In case this query is part of a larger transactional block, consider using savepoints:
try:
cursor.execute("savepoint my_save_point")
cursor.execute(query)
except:
cursor.execute("rollback to savepoint my_save_point")
finally:
cursor.execute("release savepoint my_save_point")
The issue here is the following:
try:
# it is this specific line that causes an error
cursor.execute(q, (county, county))
# this never happens, so the transaction is still open
conn.commit()
except Exception, e:
...
# you never issued a rollback on the transaction ... its still open
As you can see, if cursor.execute
fails then you neither attempt to commit the transaction or roll it back. The next iterations through the loop will be attempting to execute SQL on an already aborted but not rolled back transaction.
Instead you need to follow this kind of pattern
try:
cursor.execute(...)
except Exception, e:
conn.rollback()
else:
conn.commit()