This property can be assigned a value of True
or False
to enable or disable whether
warnings should raise exceptions. The default is
False
(default). The property can be invoked
to retrieve the current exceptions setting.
Setting raise_on_warnings
also sets
get_warnings
because warnings need to be
fetched so they can be raised as exceptions.
You might always want to set the SQL mode if you would like to have the MySQL server directly report warnings as errors (see Section 6.9.2.47, “MySQLConnection.sql_mode Property”). It is also good to use transactional engines so transactions can be rolled back when catching the exception.
Result sets needs to be fetched completely before any exception can be raised. The following example shows the execution of a query that produces a warning:
>>> cnx.raise_on_warnings = True
>>> cursor.execute('SELECT "a"+1')
>>> cursor.fetchall()
..
mysql.connector.errors.DataError: 1292: Truncated incorrect DOUBLE value: 'a'
Returns True
or False
.