Installation guide

mxODBC - Python ODBC Database Interface
10.4.2 Examples
Here's an example of an error handler that allows to flexibly ignore warnings or
only record messages.
# Error handler configuration
record_messages_only = 0
ignore_warnings = 0
# Error handler function
def myerrorhandler(connection, cursor, errorclass, errorvalue):
""" Default mxODBC error handler.
The default error handler reports all errors and warnings
using exceptions and also records these in
connection.messages as list of tuples (errorclass,
errorvalue).
"""
# Append to messages list
if cursor is not None:
cursor.messages.append((errorclass, errorvalue))
elif connection is not None:
connection.messages.append((errorclass, errorvalue))
# Ignore warnings
if (record_messages_only or
(ignore_warnings and
issubclass(errorclass, mx.ODBC.Error.Warning))):
return
# Raise the exception
raise errorclass, errorvalue
# Installation of the error handler on the connection
connection.errorhandler = myerrorhandler
In case the connection or one of the cursors created from it cause an error,
mxODBC will call the
myerrorhandler() function to let it decide what to do
about the error situation.
Possible error resolutions are to raise an exception, log the error in some way,
ignore it or to apply a work-around.
Typical use-cases for error handlers are situations where warnings need to be
masked or an application requires an on-demand reconnect.
If you need to catch errors or warnings at connection time, you can use the
optional keyword argument
errorhandler to have the error handler installed
early enough to be able to deal with such errors or warnings:
connection = mx.ODBC.Windows.DriverConnect('DSN=test',
errorhandler=myerrorhandler)
162