This exception is the base class for all other exceptions in theerrors module. It can be used to catch all errors in a singleexcept statement.
The following example shows how we could catch syntax errors:
import mysql.connectortry: cnx = mysql.connector.connect(user='scott', database='employees') cursor = cnx.cursor() cursor.execute("SELECT * FORM employees") # Syntax error in query cnx.close()except mysql.connector.Error as err: print("Something went wrong: {}".format(err)) Initializing the exception supports a few optional arguments, namelymsg,errno,values andsqlstate. All of them are optional and default toNone.errors.Error is internally used by Connector/Python to raise MySQL client and server errors and should not be used by your application to raise exceptions.
The following examples show the result when using no arguments or a combination of the arguments:
>>> from mysql.connector.errors import Error>>> str(Error())'Unknown error'>>> str(Error("Oops! There was an error."))'Oops! There was an error.'>>> str(Error(errno=2006))'2006: MySQL server has gone away'>>> str(Error(errno=2002, values=('/tmp/mysql.sock', 2)))"2002: Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)">>> str(Error(errno=1146, sqlstate='42S02', msg="Table 'test.spam' doesn't exist"))"1146 (42S02): Table 'test.spam' doesn't exist" The example which uses error number 1146 is used when Connector/Python receives an error packet from the MySQL Server. The information is parsed and passed to theError exception as shown.
Each exception subclassing fromError can be initialized using the previously mentioned arguments. Additionally, each instance has the attributeserrno,msg andsqlstate which can be used in your code.
The following example shows how to handle errors when dropping a table which does not exist (when theDROP TABLE statement does not include aIF EXISTS clause):
import mysql.connectorfrom mysql.connector import errorcodecnx = mysql.connector.connect(user='scott', database='test')cursor = cnx.cursor()try: cursor.execute("DROP TABLE spam")except mysql.connector.Error as err: if err.errno == errorcode.ER_BAD_TABLE_ERROR: print("Creating table spam") else: raise Prior to Connector/Python 1.1.1, the original message passed toerrors.Error() is not saved in such a way that it could be retrieved. Instead, theError.msg attribute was formatted with the error number and SQLSTATE value. As of 1.1.1, only the original message is saved in theError.msg attribute. The formatted value together with the error number and SQLSTATE value can be obtained by printing or getting the string representation of the error object. Example:
try: conn = mysql.connector.connect(database = "baddb")except mysql.connector.Error as e: print "Error code:", e.errno # error number print "SQLSTATE value:", e.sqlstate # SQLSTATE value print "Error message:", e.msg # error message print "Error:", e # errno, sqlstate, msg values s = str(e) print "Error:", s # errno, sqlstate, msg valueserrors.Error is a subclass of the PythonStandardError.