[Zope] Generic SQL insert
Robert (Jamie) Munro
jamie at textmatters.com
Thu Apr 13 14:23:22 EDT 2006
Whenever I'm using SQL databases in zope, I always seem to have to make
a ZSQL instance for inserting into every table in my database, and they
are all nearly the same - they just have a list of all the fields in the
database in the parameters, then they say:
insert into [table] ([list of fields]) values ([list of <dtml-sqlvar>s])
I'd much rather have a dictionary of fields and values, and just throw
it at the DB, not having to make those queries for every table. I have
acheived it like so:
mydict = {"field1":"value1" , "field2":"value2" ,...}
(fields,values)=zip(*myDict.items())
context.genericInsert(table='table name',fields=fields,values=values)
Where generic insert is the following ZSQL method:
insert into "<dtml-var table>"
(<dtml-in expr="fields">"<dtml-var sequence-item>"<dtml-if
sequence-end><dtml-else>,</dtml-if></dtml-in>)
values (<dtml-in expr="values"><dtml-sqlvar sequence-item
type=string><dtml-if sequence-end><dtml-else>,</dtml-if></dtml-in>);
with parameters:
* table - table name
* fields - list of fieldnames
* values - list of values in the same order
What do other people think of this? Is it a really bad idea?
Robert Munro
More information about the Zope
mailing list