How to get MySQL type error using PyMySQL?

I am making a Python application with MySQL and PyMySQL, and I would like to know the number of MySQL errors when I get it, so that I can do something else depending on it.

Is there a way to do this using the try-except statement or another way?

+6
source share
3 answers

Any exception in Python has an args element that shows you how it was created. For instance:

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

For pymysql, they are always built using (errno, errorvalue) . So:

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

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

+9
source
 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 there is a launch, but many other errors, for example. OperationalError

+7
source

pymysql displays mysql errors for python errors according to the following table:

 _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 errors, you have to catch ProgrammingError, DataError, IntegrityError, NotSupportedError and OperationalError individually. You can see exactly which mysql error was caught by forcibly excluding an exception from the string using str .

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

Source: https://habr.com/ru/post/973071/


All Articles