Python-MySQLdb,如何访问 `OperationalError` 中的异常错误代码?

标签 python mysql-python

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

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

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

最佳答案

您可以像下面这样捕获错误号:

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` 中的异常错误代码?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43663130/

相关文章:

自定义类上的 Python bytes(...)

python - 考虑到 GIL,asyncio 怎么可能不是线程安全的?

Python、mysqldb 和 unicode

python - **StreamExecutor 设备 (0) : Host, 默认版本** 在 Tensorflow 中

python - 字典理解为内部字典中的每个键计算跨字典的统计数据

python - 实验性 python tcp-proxy 不工作

python - 使用 Python 在 MySQL 中使用键入的值更新和删除

python - MySQLdb 属性错误 : 'module' object has no attribute 'commit'

python - SQL 查询显示结果比 Python 表中存储的结果多或少 30 分钟

python - 默认情况下,Python 的 MySQLdb 游标本质上是阻塞的吗?