Installation guide

mxODBC - Python ODBC Database Interface
6.4.2 Errors due to missing Transaction Support
If you get an exception during connect telling you that the driver is not capable or
does not support transactions, e.g.
mxODBC.NotSupportedError: ('S1C00',
84, '[Microsoft][ODBC Excel Driver]Driver not capable ', 4226)
, try
to connect with
clear_auto_commit set to 0. mxODBC will then keep auto-
commit switched on and the connection will operate in auto-commit mode.
6.5 Connection objects as context managers
6.5.1 Introduction to Context Managers
Python 2.5 introduced the new concept of context manager to Python. Context
managers are Python objects implementing the context manager API based on the
methods .__enter__() and .__exit__().
The context managers can be used together with the Python with-statement to
wrap sections of a program into a block (the context) that is entered and exited in
a controlled way:
with context_manager as context:
context.do_something()
When entering the block, the context_manager's .__enter__() method is
called and the returned object assigned to
context. When exiting the block, the
context.__exit__() is called, either with the exception that caused the block to
be left or without exception in case the block was left normally.
6.5.2 Using connection objects as context object
Connection objects implement this API and use it to automatically commit or roll
back the current transaction.
from mx.ODBC.Manager import DriverConnect
connection = DriverConnect(…)
with connection:
cursor = connection.cursor()
cursor.execute('INSERT INTO table VALUES (?, ?)', (1, 2))
… other tasks …
cursor.close()
This code will automatically commit the INSERT to the database backend in cae
the with-block is left without exception. If the other tasks trigger an unhandled
transaction, the connection is rolled back when leaving the block.
For code which doesn't have to do more complex error handling, using the with-
statement block can greatly simplify the resulting code. It also gives the
transaction section a visible resemblance in the code.
90