[Zope-dev] saving dictionaries in MySQL database with using existing connection

Leonardo Rochael Almeida leo@hiper.com.br
13 Aug 2002 20:59:26 -0300


On Tue, 2002-08-13 at 18:52, Axel Bock wrote:
> On Tue, 2002-08-13 at 20:54, R. David Murray wrote:
> > On 13 Aug 2002, Axel Bock wrote:
> > > it into the database), but this is not what I intend.
> > > I want to say something like this (very, very roughly!):
> > > 		db.save(dictionary)
> > > ...and Zope should put my dict into the db. Serialized.
> > >
> > > Does anyone know whether this is possible or not, and if yes, how?
> > 
> > Pickle it into a string and store the string.  Unpickle it when you
> > retreive it.  But why not just store it in the zodb?
> 
> well, pickle is cool. :-)
> 
> now I have two new problems: 
> 1. I don't know how to get the database connection

Don't try to get the database connection. It's impossible (well, not
really). Instead try to realise the truth: use an sql method.

> 2. I don't know how to store this string without making some queer
> arrangements to avoid getting \' chars in the SQL query - which are
> quite deadly to them.

Don't bother. Let sqlvar do the quoting work for you:


Add a ZMySQLDA Connector object to your site (let's call it 'conn').
Add a ZSQLMethod to your site (let's call it 'save') pointed at the
'conn' connector, with parameters: 

name dict

and text:

insert into myDictTable (dictNameColumn, dictPickleColumn) values 
  (<dtml-sqlvar name type='string'>,
   <dtml-sqlvar dict type='string'>)

and in your python script you just do

...
container.save("my dictionary name", myDict).

considerations about database schema and retrieval of the dictionary are
left as an exercise for the reader.

Cheers, Leo

-- 
Ideas don't stay in some minds very long because they don't like
solitary confinement.