Installation guide
5. mxODBC Overview
This makes it easy to define your own factory functions to programmatically
define row classes based on the
cursor.description or other cursor
parameters.
Row Factories and multiple Result Sets
If you are using multiple result sets, the cursor.rowfactory is called for the first
fetch in each of the result sets you are reading from the database.
Predefined Row Factories
mxODBC comes with a set of useful row factories which provide both sequence
index access as well as named attribute access. These are defined in the
mx.ODBC.Misc.RowFactory module, which is imported into all mxODBC
subpackages for convenience. See section 13 mx.ODBC.Misc.RowFactory Module
for the API documentation.
The module defines these row factories:
RowFactory.TupleRowFactory
This is a factory which is a subtype of the Python tuple type and provides a
standard tuple index based access to the row column values, as well as an
attribute based one which is derived from the lower-cased column names
found in
cursor.description.
The row objects are immutable, just like standard tuples, but you can also slice
them or index them as usual.
Example:
from mx.ODBC.Windows import RowFactory
cursor.rowfactory = RowFactory.TupleRowFactory
cursor.execute('select x, Y, z from mytable')
row = cursor.fetchone()
print (row[0], row[1], row[2], row.x, row.y, row.z)
Because the row objects implement the sequence protocol, they are also
usable as input parameters to the
cursor.execute*() methods.
Example:
cursor.execute('insert into mytable values (x, Y, z)', row)
RowFactory.ListRowFactory
This factory uses a subtype of the Python list type and also provides a
sequence index based access, as well as a named attribute access, just like the
TupleRowFactory.
Unlike for the
TupleRowFactory, the row objects created by the
ListRowFactory are mutable lists, so you can assign to the indexes as well as
the attributes.
Example:
from mx.ODBC.unixODBC import RowFactory
83