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

Connecting with Data Access Objects

The Data Access Object hierarchy is installed on your system if you have Microsoft Office, Access, or Visual Basic, or if you have ever installed a VB application that works with MDB files. It doesn’t need an ODBC data source configured. We recommend running MakePy over the latest version of the Microsoft DAO Library on your system, as seen in Figure 13.3.

MakePy showing the DAO and ADO libraries
Figure 13.3. MakePy showing the DAO and ADO libraries

The hierarchy starts with a database engine (class DBEngine) and allows you to open multiple Database objects. Once a database is open you can create Recordset objects that are the broad equivalent of a cursor in the Python Database API. You can navigate through a Recordset and edit its fields. A Field is an object too, with a Name, a Value, and many more properties.

Let’s start by connecting to the same database as before. This time you don’t need the ODBC data source, but can go straight for the file:

>>> import win32com.client
>>> daoEngine = win32com.client.Dispatch('DAO.DBEngine')
>>> daoDB = daoEngine.OpenDatabase('C:\\MYDIR\\pydbdemos.mdb')
>>> daoRS = daoDB.OpenRecordset('SELECT ClientID, InvoiceDate, \
                    Consultant, Hours FROM Invoices')
>>> daoRS.MoveLast()     # need to do this to get an accurate size
>>> daoRS.RecordCount
4
>>>

Opening the database and getting the result set is no harder than with the Python API and ODBC. However, instead ...

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