Pickling objects into database

Kresimir Kumericki kkumer at phy.hr
Fri Mar 28 04:19:29 EST 2003


I have a couple of python CGI scripts with MySQL database backend
and now I want to save a dictionary (the elements of which are only
tuples of strings) into a single database "blob" column.

The way I am doing it at the moment is by pickling it like this:


dict = { ... some items ...}

qry = 'UPDATE table SET blobcolumn="%s" WHERE id=someid' %\
        (MySQLdb.escape_string(cPickle.dumps(dict)), someid)

cursor.execute(qry)


This seems to be working properly but, being a newbie to all this, I am
somewhat concerned about forwarding cPickle.dumps() *ugly* strings to
database like this.  Is MySQLdb.escape_string up to the job of escaping
everything that should be escaped? Is there a better way of doing this?
Thanks for any advice.


-- 
Kresimir Kumericki    kkumer at phy.hr    http://www.phy.hr/~kkumer/
"Fizika svemira" - http://eskola.hfd.hr/fizika_svemira/svemir.html




More information about the Python-list mailing list