O'Reilly logo

Python Programming On Win32 by Mark Hammond, Andy Robinson

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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', ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required