MS Access db (mdb): viewing table attributes

BartlebyScrivener rpdooling at
Fri Mar 10 21:38:56 CET 2006


I'm a beginner and had fits connecting to my Access 2003 DB of
quotations. The best advice that finally worked for me was to use
mxODBC. In fact, you might just search this forum for Access and

I can't customize to fit your situation, but here is the connection
part of a script I use to extract a random quote. I'm on Windows XP
using newest Python from ActiveState.

# Based on
# Instructions for customizing are at:

import mx.ODBC.Windows as odbc
import random
import textwrap

driv='DRIVER={Microsoft Access Driver (*.mdb)};DBQ=d:/Access

conn = odbc.DriverConnect(driv)
c = conn.cursor()

# Just counts the quotes, doesn't select them
c.execute ("SELECT COUNT(Quote) FROM Table1")

# Yields the number of rows with something in the quote field
total_quotes = c.fetchone()

# Get a random number somewhere between
# 1 and the number of total quotes

quote_number = random.randint(1, total_quotes[0])

# Select a quote where the ID matches that number
c.execute ("SELECT Author, Quote FROM QUOTES7 WHERE ID = %d" %

quote = c.fetchone()

The rest of the script just deals with formatting and presentation of
the quote.

Hope this helps.


More information about the Python-list mailing list