Python and Databases

Bradley D. Larson blarson at crary.com
Fri Jun 27 17:57:40 EDT 2003


I'm surprised this hasn't been brought up thus far.... I use it on multiple
databases and multiple platforms. I can move applications from one platform to
another without modifications.

I have found the most practical (and portable) method of accessing databases is
with the dbi/odbc that comes with python. Not as fast as direct access and it
requires you to load and set up an ODBC data source for each DB server and Access
DB but.... the benefits far outweigh the speed issues.

I have migrated entire applications from one DB to another without with minimal
code changes. As an application matures or the database grows I move the data to a
more robust database server.

I have situations where I am accessing four (4) different data base engines within
the same program without having to remember the different syntax for each database
driver... I access all of them in the same manner! That is probably the biggest
benefit. Less training, fewer bugs, and better understanding across multiple
developers.

The following example would work for any of the four databases listed.


import dbi, odbc

    fs_db = odbc.odbc('MSS-Flex Reports/mylogin/mypasswd')  # Proprietary Fourth
Shift Manufacturing DB

    mysql_db  = odbc.odbc('mysql_odbc_db/mylogin/mypasswd')  # web server, I don't
even know the location

    postgres_db  = odbc.odbc('postgres_odbc_db/mylogin/mypasswd') # my postgres
server

    access_db = odbc.odbc('access_odbc_db')  # one of our many MS Access databases
that seem to proliferate.


    cur = postgres_db.cursor()

    sql = "select field1,field2, field3 from mytable where field1 = 'myname';"
    cur.execute(sql)
    list = cur.fetchone()

    if list:
         for f1,f2,f3 in list:
             print f1,f2,f3

    cur.close()
    cur = None

    postgres_db.commit()
    postgres_db.close()
    postgres_db = None




Geraldo Lopes de souza wrote:

> Hi,
>
> ... you have just one choice: the mxODBC package.
> It looks like a great package , it worths the price, but with java for
> example, I get more much more ( in library terms) for less.
>
> ... To increase this the python community needs a standard ODBC library in the
> language.
>
> ... I know there is the DB API but we need ODBC.
> ODBC is the standard way of communicating with databases, if you create a
> database the first thing you need is an odbc driver to make the existing
> applications to talk with your database.
>
>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: blarson.vcf
Type: text/x-vcard
Size: 400 bytes
Desc: Card for Bradley D. Larson
URL: <http://mail.python.org/pipermail/python-list/attachments/20030627/e63b3bd1/attachment.vcf>


More information about the Python-list mailing list