MS Access db (mdb): viewing table attributes

BartlebyScrivener rpdooling at gmail.com
Fri Mar 10 15:38:56 EST 2006


Gau,

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
mxODBC.

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
# http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/389535
# Instructions for customizing are at:
# http://www.egenix.com/files/python/mxODBC.html

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

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

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_number)

quote = c.fetchone()

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

Hope this helps.

rpd




More information about the Python-list mailing list