How to deal with relational data?

StevenViCy Steven.dot.Suchting. at computer.dot.org
Mon Apr 1 16:32:53 EST 2002


How do we Python programmers typically deal with creating and using
objects in which the source data is stored in a relational data base?

I know and use the standard DB-API to access the data in relational DB,
and I can create the classes that I want to store the record data as
Python objects.  What's got me concerned is that I seem to be going down
the road of having to have a cursor in most of my routines that access
the data in the Python objects, and I'm wondering if there is a way to
avoid that.

In my application, I have objects (or tuples in a table), that reference
1-to-many other objects (tuples in another table), each of which
references 1-to-many other objects (or tuples in either the same or
another table), and so on in a sort of hierarchy of references to
objects of various classes.

One approach (the first that I tried), created very large and
complicated objects that were hard to comprehend, but worse, were
structured so that it was not very easy to use the various classes in
the heirarchy as building blocks when the classes needed to be used or
referenced by other objects.  To create these large objects, it was very
straightforward to just get the cursor and use it to pull the data
together, and then for subsequent operations using the class, I never
had to use the cursor again.  But, as I said, the objects became
unweildly, and some of the lower leaves of class ended up getting copied
(instead of just referenced) by multiple instances of the top level
object.  I couldn't really reuse the references to the lower tier
objects.

The next approach was to define the relationship between the top-level
object and the lower level objects using what were essentially key
identifiers.  I kept the lower level objects more logically separate
from the parent object, intending to eventually create a cache of some
kind to store the lower level objects so that I would not have
duplicates of the lower level objects in memory.  But in my attempts to
create methods for the top-level object that would need to access data
from the lower tier objects, I couldn't figure out a way to do the
access without passing a cursor into the top-level objects methods - and
this is what seems like a waste. It doesn't seem to make for a very
cohesive, standalone object.

Any recommendations?

Thanks in advance,
Steve S.




More information about the Python-list mailing list