Python-MySQLdb,如何在`OperationalError`中访问异常错误代码? [英] Python-MySQLdb, how do you access the exception error-code in `OperationalError`?

查看:487
本文介绍了Python-MySQLdb,如何在`OperationalError`中访问异常错误代码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要捕获特定的OperationalError异常.异常文本使用错误代码2006.库在MySQLdb.constants.CR.SERVER_GONE_ERROR = 2006处定义错误代码.

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.

如何从异常中获取错误代码?

当我检查MySQLdb._mysql_exceptions时,有OperationalError异常的定义,但没有构造函数或如何访问异常错误代码的描述.

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.

推荐答案

您可以捕获如下所示的错误号:

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

参考: https://www.programcreek.com/python/example /2584/MySQLdb.OperationalError

这篇关于Python-MySQLdb,如何在`OperationalError`中访问异常错误代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆