[python-win32] driving MS Access from python

Tim Roberts timr at probo.com
Tue Mar 14 00:20:11 CET 2006

Phill Atwood wrote:

> Thanks. I've downloaded and installed adodb for Python.  But I guess I
> need to install mxODBC as well.  But I'm not quite understanding the
> docs I'm reading on how to do this.  It seems very complex....

No, you don't need mxODBC, although I'm curious to know what led you to
believe that.  Go do a google search for "adodb connection strings", and
you find several samples.  Here's a simple sample that I used to access
an Access database.  Note that I have connection strings for either the
Jet OLEDB driver, or the Access ODBC driver.  This opens a "table"
recordset (the rs.Open function).

    import os
    import win32com.client

    conn = win32com.client.Dispatch("ADODB.Connection")

    # Either way works: one is the Jet OLEDB driver, the other is the
    # Access ODBC driver.  OLEDB is probably better.

    db = r"c:\dev\54nsdc\Volunteer.mdb"
    DSN="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + db
    #DSN="Driver={Microsoft Access Driver (*.mdb)};DBQ=" + db

    rs = win32com.client.Dispatch("ADODB.Recordset")
    rs.Open( "[Committees]", conn, 1, 3 )

    print rs.Fields.Count, " fields found:"
    for x in range(rs.Fields.Count):
        print rs.Fields.Item(x).Name,
        print rs.Fields.Item(x).Type,
        print rs.Fields.Item(x).DefinedSize,
        print rs.Fields.Item(x).Value

To execute a generic SQL statement, you create an ADODB.Command object
and connect it to the Connection:

    cmd = win32com.client.Dispatch("ADODB.Command")
    cmd.ActiveConnection = conn
    cmd.CommandText = "SELECT COUNT(*) FROM committees;"
    rs = cmd.Execute[0]

Now rs is a recordset.

Tim Roberts, timr at probo.com
Providenza & Boekelheide, Inc.

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.python.org/pipermail/python-win32/attachments/20060313/e91d9dde/attachment.html 

More information about the Python-win32 mailing list