如何访问MySQL python库异常中的错误代码

如何访问MySQL python库异常中的错误代码,python,mysql,python-3.x,error-handling,mysql-python,Python,Mysql,Python 3.x,Error Handling,Mysql Python,我使用此问题中的代码捕获操作错误: 但是,我无法访问错误代码。我得到一个错误: File "main.py", line 835, in db_execute if errorCode[0] == 1213: TypeError: 'OperationalError' object does not support indexing 感谢@roganjosh对问题的评论,我发现了操作错误的属性: ['__cause__', '__class__', '__context__', '

我使用此问题中的代码捕获操作错误:

但是,我无法访问错误代码。我得到一个错误:

  File "main.py", line 835, in db_execute
    if errorCode[0] == 1213:
TypeError: 'OperationalError' object does not support indexing

感谢@roganjosh对问题的评论,我发现了操作错误的属性:

['__cause__', '__class__', '__context__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__setstate__', '__sizeof__', '__str__', '__subclasshook__', '__suppress_context__', '__traceback__', '__weakref__', 'args', 'with_traceback']

错误代码在属性
args[0]

中,请尝试
print(dir(errorCode))
并查看它是否列出了任何属性。您是否尝试访问
errorCode.args
?@roganjosh感谢您的代码,不过,注释可能会被删除,因此我认为有必要正式确定您用于解决此问题的步骤。如果我的评论被删除,你的回答将毫无意义。
['__cause__', '__class__', '__context__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__setstate__', '__sizeof__', '__str__', '__subclasshook__', '__suppress_context__', '__traceback__', '__weakref__', 'args', 'with_traceback']