Python 如何使用 PyMySQL 获取 MySQL 类型的错误?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25026244/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 19:35:42  来源:igfitidea点击:

How to get the MySQL type of error with PyMySQL?

pythonmysqlpymysql

提问by Adrian

I'm doing a Python application with MySQL and PyMySQL and I'd like to be able to know the number of the MySQL error when I get one so that I can do something different depending on it.

我正在用 MySQL 和 PyMySQL 做一个 Python 应用程序,当我得到一个错误时,我希望能够知道 MySQL 错误的数量,以便我可以根据它做一些不同的事情。

Is there a way to do that with a try-except statement or another way?

有没有办法用 try-except 语句或其他方式做到这一点?

采纳答案by abarnert

Any exception in Python has an argsmember that shows you how it was constructed. For example:

Python 中的任何异常都有一个args成员,向您展示它是如何构造的。例如:

>>> e = Exception(1, 2, 3, 4)
>>> e.args
(1, 2, 3, 4)

For pymysql, they're always constructed with (errno, errorvalue). So:

对于 pymysql,它们总是用(errno, errorvalue). 所以:

try:
    do_stuff()
except MySQLError as e:
    print('Got error {!r}, errno is {}'.format(e, e.args[0]))

I'm not sure this is guaranteed by the documentation, but you can see how it works pretty easily from the source.

我不确定文档是否保证了这一点,但是您可以从源代码中很容易地看到它是如何工作

回答by Andrew Johnson

pymysql maps mysql errors to python errors according to the following table:

pymysql根据下表将mysql错误映射到python错误:

_map_error(ProgrammingError, ER.DB_CREATE_EXISTS, ER.SYNTAX_ERROR,
       ER.PARSE_ERROR, ER.NO_SUCH_TABLE, ER.WRONG_DB_NAME,
       ER.WRONG_TABLE_NAME, ER.FIELD_SPECIFIED_TWICE,
       ER.INVALID_GROUP_FUNC_USE, ER.UNSUPPORTED_EXTENSION,
       ER.TABLE_MUST_HAVE_COLUMNS, ER.CANT_DO_THIS_DURING_AN_TRANSACTION)
_map_error(DataError, ER.WARN_DATA_TRUNCATED, ER.WARN_NULL_TO_NOTNULL,
       ER.WARN_DATA_OUT_OF_RANGE, ER.NO_DEFAULT, ER.PRIMARY_CANT_HAVE_NULL,
       ER.DATA_TOO_LONG, ER.DATETIME_FUNCTION_OVERFLOW)
_map_error(IntegrityError, ER.DUP_ENTRY, ER.NO_REFERENCED_ROW,
       ER.NO_REFERENCED_ROW_2, ER.ROW_IS_REFERENCED, ER.ROW_IS_REFERENCED_2,
       ER.CANNOT_ADD_FOREIGN, ER.BAD_NULL_ERROR)
_map_error(NotSupportedError, ER.WARNING_NOT_COMPLETE_ROLLBACK,
       ER.NOT_SUPPORTED_YET, ER.FEATURE_DISABLED, ER.UNKNOWN_STORAGE_ENGINE)
_map_error(OperationalError, ER.DBACCESS_DENIED_ERROR, ER.ACCESS_DENIED_ERROR,
       ER.CON_COUNT_ERROR, ER.TABLEACCESS_DENIED_ERROR,
       ER.COLUMNACCESS_DENIED_ERROR)

if you want to catch the errors then you will need to catch ProgrammingError, DataError, IntegrityError, NotSupportedError, and OperationalError, individually. You can see specifically which mysql error was caught by coercing the exception to a string using str.

如果要捕获错误,则需要分别捕获 ProgrammingError、DataError、IntegrityError、NotSupportedError 和 OperationalError。您可以通过使用str.

try:
    #interact with pymysql
except ProgrammingError as e:
    print "Caught a Programming Error:",
    print e

回答by Berto

for name, ddl in TABLES.iteritems():
    try:
        print("Creating table {}: ".format(name))
        db.execute(ddl)
    except pymysql.InternalError as error:
        code, message = error.args
        print ">>>>>>>>>>>>>", code, message

That's a start but loads of other errors exist eg. OperationalError

这是一个开始,但存在大量其他错误,例如。操作错误