Python-MySQLdb, how do you access the exception error-code in `OperationalError`?

僤鯓⒐⒋嵵緔 提交于 2020-01-05 02:29:55

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!