newbie database question

Bill Tate tatebll at aol.com
Tue Jan 8 22:21:29 EST 2002


"james kowalka" <jkowalka at nycap.rr.com> wrote in message news:<0cI_7.33086$bZ5.5274497 at typhoon.nyroc.rr.com>...
> I am brand new to Python, and need to develop some database programs to
> read/write some old DBF file info.  I installed mxODBC, but can't seem to
> figure out how to access a particular field to read or write it's data.  How
> can I access fields directly through a connection cursor (ie, db->fname =
> "Steve", or firstname = db->fname), like in c++ or delphi.
> 
> Thanks in advance,
> 
> Jim Kowalka

Jim,
I hadn't used plain dbf files before so I just tried this and it
seemed to work ok. Create a Data Source Name via the ODBC facility -
select Microsoft dbase driver which should be present.  Specify the
directory that the source *.dbf files are located - assume you called
the DSN - 'TEST_DBF'

Create a new python script that does essentially the following 

import odbc
conn = odbc.odbc('TEST_DBF') #normally its like
"DSN/UserName/Password"
#use connection to create cursor
cursor = conn.cursor()
#create a simple sql string - leave off the <>'s obviously
sql = 'Select <field name1>, <field name2>,.... from <dbf-table>'
#probably best if wrap the following in try - except clause akin to 
try:
   cursor.execute(sql)
   dbList = cursor.fetchall()
   for item in dbList:
      #iterate over your list and do something with it.
except:
   #handle exception

conn.close() #close the connection when done with it

Bill



More information about the Python-list mailing list