问题
I need to catch a specific OperationalError
exception. The exception text uses the error-code 2006. The library defines the error-codes at MySQLdb.constants.CR.SERVER_GONE_ERROR = 2006
.
How do you get the error-code from the exception?
When I check the MySQLdb._mysql_exceptions, there is a definition of the OperationalError exception but it has no constructor or description of how to access the exception error code.
回答1:
You can catch the error number like the following:
try:
# Adding field 'Bug.bize_size_tag_name'
db.add_column('search_bug', 'bize_size_tag_name', orm['search.bug:bize_size_tag_name'])
except MySQLdb.OperationalError, errorCode:
if errorCode[0] == 1060:
pass
else:
raise
Reference: https://www.programcreek.com/python/example/2584/MySQLdb.OperationalError
来源:https://stackoverflow.com/questions/43663130/python-mysqldb-how-do-you-access-the-exception-error-code-in-operationalerror