newbie database question

james kowalka jkowalka at nycap.rr.com
Wed Jan 9 08:15:32 EST 2002


Bill,
    I was able to load the DBF and read it into the tuple, but isn't there
an easier way to navigate through the DBF and read/write from one record at
a time.  How would you write changes back into the table from a tuple,
especially if there were thousands of records?

Thanks,

Jim

"Bill Tate" <tatebll at aol.com> wrote in message
news:cb4ba455.0201081921.b8c8194 at posting.google.com...
> "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