[Zope] Looking if a record with a given key exists in a database several times, in dtml

Thomas B. Passin tpassin@mitretek.org
Mon, 18 Feb 2002 11:25:20 -0500


You probably will do best to use an sql select statement that reads
something like this:

SELECT * FROM TABLE
WHERE KEY IN (a,b,c,d)

where a,b,c,d, are values you get from your iteration.

Here is an easy way to get a comma-separated list of values:

<dtml-let YESNO="{0:',',1:''}">
   <dtml-in "[1,2,3]"><!-- your list variable would go here -->
     <dtml-var "YESNO[_['sequence-index']==0]">&dtml-sequence-item;
   </dtml-in>
</dtml-let>

This inserts a comma before each value unless it is the first one.

Your zsql method could look like this:

select * from theTable
where the_id in (&dtml-ids;)

You pass it the comma-separated list as a variable named "ids".  If you need
to avoid duplicates, just use "select distinct"

Cheers,

Tom P

[Igor Leturia]

  I have a problem: in dtml, I have to iterate through a very big list
and for each item I must look if there is a record with that key in a
database. I can't run a 'select' ZSQLMethod for each, because it would
take too long. What I want to do is run the 'select' ZSQLMethod once and
load the results in a variable (an array or dictionary) which then will
be faster to look up in, or look up in the results of that query in some
fast way.