Installation guide
7. mxODBC Cursor Objects
7.2 Subpackage Support
Cursor objects are supported by all subpackages included in mxODBC.
The extent to which the functionality and number of methods is supported may
differ from subpackage to subpackage, so you have to verify the functionality of
the used methods (esp. the catalog methods) for each subpackage and database
that you intend to use.
7.3 Cursor objects as context managers
Please see section 6.5.1. Introduction to Context Managers for an introduction to
context managers and the Python with-statement.
7.3.1 Using cursor objects as context objects
Cursor objects implement this API and use it to automatically close the cursor and
freeing resources in the ODBC driver when leaving a with-block. Instead of
writing:
cursor = connection.cursor()
try:
cursor.execute('INSERT INTO table VALUES (?, ?)', (1, 2))
… other tasks …
finally:
cursor.close()
you can write:
with connection.cursor() as cursor:
cursor.execute('INSERT INTO table VALUES (?, ?)', (1, 2))
… other tasks …
This not only looks a lot better and also takes care of freeing the resources in case
of an error in the block.
7.4 Cursor Type Object
mxODBC uses a dedicated object type for cursors.
Each subpackage defines its own object type, but all share the same name:
CursorType.
103