Storing objects in relational database

nayden nayden at gmail.com
Fri May 23 17:07:19 EDT 2008


I started playing with python a few weeks ago after a number of years
of perl programming and I can say that my first impression is,
unsurprisingly, quite positive. ;)
The reason I am writing here is that I can't seem to figure out how to
save/restore python objects into a relational database.  The way I
used to do it in perl was to 'freeze' the object before storing it
into the database and 'thaw' it before restoring it. (For those not
familiar with the perl terminology freeze and thaw are method from
perl's persistence module Storable).  I found that the python's
corresponding module is Pickle but it doesn't seem to work for me.. I
do roughly the following:
class TestA:


def insert():
    i = TestA('asdf')
    output = cStringIO.StringIO()
    cPickle.dump(i, output, 2)
    print "output.getvalue(): %s" % output.getvalue()
    jd = libpq.PgQuoteBytea(output.getvalue())
    print "jd %s" % jd
    return dbpool.runOperation("insert into jobs (effective_job_id,
job_description) values (1, " + jd + ")")



More information about the Python-list mailing list