newbie database question

Steve Holden sholden at holdenweb.com
Tue Jan 8 22:51:18 EST 2002


"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
>
Although Bill doesn't mention it, you should know that the odbc module to
which he refers is a part of the Windows extensions. You will have htese
already if you installed ActiveState's distribution, or you can add them to
an existing standard Windows Python after downloading the Win32all
extensions from ActiveState's web site.

regards
 Steve
--
http://www.holdenweb.com/








More information about the Python-list mailing list