[DB-SIG] SQL storage for Python objects

Patrick Curtain patrick@swdev.com
Tue, 01 Sep 1998 21:56:45 -0800


Okay,

this is probably the most common question and might already be solved.
OR, it might be one of those thorny things that noone really has a
solution to.

Has anyone developed a useful idiom or pattern for storing python
objects to relational databases?  Said another way, is there a
well-defined API that improves on simply embedding all the SQL for the
operations on an object?

So far, I'm simply including a method for each object that, given a key
value, attempts to query the db (i'm using MySQL for now) for that
object, populate the members and so on.  I provide the class with a
write method, a read method and a delete method.

Another way to go about this, which i've also used, is to simply have a
function that grabs all the data from the db using the fetchall method
then iterates over the returned list of records and creates an object
instance of that type using the record as the argument to __init__.

I'm getting the work done, i guess.  Just hoping that someone's thought
through a better, more elegant way to go about it.  I suppose in some
ways i'm also pining for an OpenSource alternative to
GemStone/Smalltalk.  The object's storage was so intrinsic that you
never had to think about it.  :)

Thanks for any help and ideas!
--p
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  Patrick Curtain, Husband & Father (I also write Software)
  patrick@swdev.com,                  http://www.swdev.com/