MS Access db (mdb): viewing table attributes

Larry Bates larry.bates at websafe.com
Fri Mar 10 16:43:04 EST 2006


gau.tai at gmail.com wrote:
> I have an Access 2003 database, protected by a workgroup, that I am
> trying to view through python.  Currently, I'm attempting dao with the
> win32 package, and I'm able to view all of the table names, but I don't
> know how to view the attributes of the tables.
> 
> My code:
> 
> import win32com.client
> from pprint import pprint
> 
> #sample code to list all tables in the selected database
> daoEngine = win32com.client.Dispatch('DAO.DBEngine.36')
> dtbs = daoEngine.OpenDatabase('database_file.mdb')
> for table in dtbs.TableDefs:
>     if table.Name[:4] <> "MSys" and table.Name[:4] <> "~TMP":
>         pprint(table.Name.encode('utf-8'))
>         #the above works, but below does not:
>         for col in table.Fields:
>              pprint(col.Name, Col.Type, Col.Size)
> 
> I am getting that len(Fields) = 0, and I know the database tables are
> all full of columns (and data).  Is there also some resource that
> defines all the properties of TableDefs?  I wasn't able to find a
> python reference for this.
> 
> Thank You,
> Gau
> 
Not quite sure about DAO but when I use ODBC I get the fieldnames
by doing (stripped from working program):

crsr.execute(SQL_query)
fieldinfo=crsr.description
fieldnames=["%s" % i[0].lower() for i in fieldinfo]

Hope this helps.

-Larry Bates




More information about the Python-list mailing list