DB-API-oriented example scripts?

Boudewijn Rempt boud at rempt.xs4all.nl
Fri Dec 10 02:16:58 EST 1999


Skip Montanaro <skip at mojam.com> wrote:

> I'm gearing up to try and replace a lot of homegrown database code with
> MySQL using the MySQLdb module.  I'm not much of a relational database
> person, however, so I'm fairly unfamiliar with the computational model
> expected by the Python DB API.  I can muddle my way through SQL.  The
> MySQLdb module comes with no example code as far as I could tell however,
> and relies heavily on the 2.0 version of the Python DB API spec for docs,
> which contains no examples.

The DB-API II stuff is quite easy to use. You must however separate
the SQL statements from the Python statements - SQL is another
language, that takes a bit of learning. For the rest, it's really
simple. Open a connection, throw the SQL together with a dictionary
of parameters at the connection, and you get the returned rows.

One example (still not perfect - there are a few problems with
relf-referring tables, and I wonder wether the object hierarchy is
as clean as it should be), is my dbObjects module. This constructs
a datadictionary representing the relations between tables, and then
generates the relevant SQL, sends it to the connection, together with
the dictionary of values and returns the values in objects.

There are complications, like deferred fetching and things like that,
but MySQL doesn't really support it. It's better to ask for small
sets of data, instead of large sets that you retrieve in many passes.

> Any suggestions?

I'm not sure about _good_, but take a look at it:

http://www.xs4all.nl/~bsarempt/python

For an example of an application using it:

http://www.xs4all.nl/~bsarempt/linguistics - but that
example also needs PyKDE/PyQt.

-- 

Boudewijn Rempt  | http://denden.conlang.org



More information about the Python-list mailing list