[Zope] DTML sql construction and IN keyword

Mike Renfro renfro@tntech.edu
Fri, 12 Jul 2002 15:57:44 -0500


On Fri, Jul 12, 2002 at 04:37:32PM -0400, Shane Landrum wrote:

> I'm writing some DTML to create a SQL query which uses the IN
> keyword. I need to select a set of records with a particular
> set of record IDs, like so:
> 
> SELECT * FROM tablename WHERE id IN (42,27,83);

If this particular snippet doesn't *have* to be DTML, try the following:

## Script (Python) "foosql"
##bind container=container
##bind context=context
##bind namespace=
##bind script=script
##bind subpath=traverse_subpath
##parameters=idlist
##title=
##
return 'SELECT * FROM tablename WHERE id IN (%s)' % str(idlist)

If passed a string argument of '42,27,83' (without quotes if you use
the test tab), it returns:

SELECT * FROM tablename WHERE id IN (42,27,83)

If you want to pass in the parentheses in the arguments, just remove
them from the return line in the script.

-- 
Mike Renfro  / R&D Engineer, Center for Manufacturing Research,
931 372-3601 / Tennessee Technological University -- renfro@tntech.edu