Sam Rushing’s ODBC Module
There is a third Python module for
working with ODBC that operates on different lines. Sam
Rushing
(http://www.nightmare.com)
specializes in low-level work with Python and Windows and has
produced a package named
calldll
that allows Python programs to
dynamically load and call any function in a DLL. We discuss how this
works in Chapter 22. It has allowed Sam to produce
an ODBC module that wraps and exposes almost every function in
Microsoft’s ODBC.DLL. To install this, you need to download the
two files
calldll.zip
and
dynwin.zip
from his site. The latter contains a
number of modules relating to ODBC. Naturally, one of these is called
odbc.py, so you need to watch for
name collisions. If you’ve been
using the previous two modules, rename them out of the way before
starting.
As with the other modules, calldll provides a
safe, easy-to-use high-level interface for querying databases, but it
bears no resemblance to the DBAPI. Here’s the high-level usage:
>>> import odbc >>> env = odbc.environment() >>> conn = env.connection() >>> conn.connect('accessdemo') >>> import pprint >>> pp = pprint.pprint >>> results = conn.query('SELECT * FROM Invoices') >>> pp(results[0]) #field information [('InvoiceID', 4, 10, 0, 1), ('ClientID', 12, 10, 0, 1), ('InvoiceDate', 11, 19, 0, 1), ('Consultant', 12, 50, 0, 1), ('PeriodEnding', 11, 19, 0, 1), ('Hours', 8, 15, 0, 1), ('HourlyRate', 2, 19, 4, 1), ('Expenses', 2, 19, 4, 1), ('ExpenseDetails', 12, 50, 0, 1), ('TaxRate', ...Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access