Python Tutorials: ADO and Python
Tuesday, August 14, 2018
Chapter 4: ADO and Python Basics
As mentioned in a previous chapter, Python can access ADO via a COM interface. To allow Python
to work with COM interfaces, you will need Mark Hammond's excellent set of
Python for Windows Extensions, in
particular his win32com module. If you don't already have the extensions installed, you
should download and install them now. Oh yeah, did I mention you need to be running on a
Windows platform already :).
In addition to Mark Hammond's extensions, you may need my list of ADO constants that I put in a Python module called adoconstants.py. This module is released under a BSD style license. This module merely contains a list of
constant values that you can use. The list has several commonly used values but is by no means
Once you've downloaded and installed the above, it is time to start exploring the different
ADO objects. All the programs presented in the following chapters are assumed to have the
following lines at the top of the file:
from win32com.client import Dispatch
from adoconstants import *
We're using "from adoconstants import *" instead of "import adoconstants" only because it keeps the
Another issue is the matter of Late Binding vs. Early Binding in COM objects. This is not ADO
specific, but rather an issue of using any COM object with Python. Basically, there are two ways that a
Python COM object can access its methods and properties. These two methods are called Late Binding and
Early Binding. If a Python COM object uses Late Binding, then every time you access a
method or property of the object, it goes through the IDispatch interface to find the method/property,
even if it is the same one being called each time. With Early Binding, we let Python know ahead
of time what methods and properties are available to an object. This speeds up things significantly,
especially inside loops, and the performance gains are actually quite substantial.
To enable Early Binding for ADO objects, we need to import the ADO library. To do this:
You can get by without importing the ADO library, but the performance gains are well worth it. Now, let's move on to exploring how to use the different ADO objects with Python.
- In PythonWin, go to Tools --> COM Makepy Utility
- In the dialog box that pops up, scroll down till you reach Microsoft ActiveX
Data Objects Library. If there are multiple versions, simply pick the latest one.
- Click on the OK button. The PythonWin environment will freeze for a little bit, while
the library is being imported. In a little while, you should see a message on the Interactive
Window that says that a file was generated.
- You've successfully generated a Python type library for ADO. From now on, Python will automatically
use the type library to early-bind any ADO objects.
Copyright © 2004 Mayukh Bose. All rights reserved.
This work may be freely reproduced provided this copyright notice is preserved.
OPTIONAL: Please consider linking to this website (http://www.mayukhbose.com/
) as well.