[Zope] Convert Help

Matthew T. Kromer matt@zope.com
Tue, 26 Feb 2002 14:41:38 -0500


Todd Loomis wrote:

> All:
>
> I need to take the elements in the list ['1', '2', '3'] and make them 
> separate SQL Statements:
>
> insert into table values ('1')
> insert into table values ('2')
> insert into table values ('3')
>
> how can i do this?


Aha!

Here's where the direct python experience can help; its actually easier 
to do in Python than it is in DTML.

For database adapters which support positional binding, use (after 
having obtained a suitable cursor):

    cursor.executemany('INSERT INTO TABLE VALUES (:1)', (['1','2','3'],))

and the executemany statement will fire off the command once for each 
element in the list to be inserted.

Note the extra set of parenthesis about the values; this is because the 
API says that this SINGLE argument is the LIST of ALL argument LISTS -- 
ie, if I wanted to insert two values, I would:

    cursor.executemany('INSERT INTO TABLE VALUES(:1, :2)', (['1','a'], 
['2','b'], ['3','c']))

DCOracle2 processes them in row major order, not column major order (the 
spec is a bit ambiguous about this as I recall.)

You can do this with Zope with a python script; but you have to play a 
game with the connection object to get an actual cursor you can program 
with.


-- 
Matt Kromer
Zope Corporation  http://www.zope.com/