Trevor Boyd Smith
Trevor Boyd Smith

Reputation: 19273

Python-MySQLdb, how do you access the exception error-code in `OperationalError`?

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.

How do you get the error-code from the exception?

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.

Upvotes: 5

Views: 5985

Answers (1)

Augmented Jacob
Augmented Jacob

Reputation: 1577

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

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

Upvotes: 2

Related Questions