[Zope] Searching a Gadfly database
   
    Fernando Martins
     
    fmartins@hetnet.nl
       
    Wed, 26 Mar 2003 23:25:49 +0100
    
    
  
Matthew Lindfield Seager wrote:
[...]
> The manual says something about using python and regular expressions or
> something instead of "LIKE" but it's enough of a battle for me puzzling
> out this Zope stuff let alone learning some Python too...
>
a) Consider a Gadfly table User(ID, Name, ...) and you want to search for
names in it.
b) Make a Z SQL called Names, which retrieves the Primary Key and the field
where you want to search, like this:
 Select ID, Name from User
c) Have a form with an input field called 'name' and  a submit button
calling  findName.
d) Create a Python Script called findName with code like this:
_________
rs = container.qryNames()           # retrieve ALL the records
ids = []                            # make an empty list
for rec in rs:                      # loop over all the records
    if rec.NAME.find(name) > -1:    # found a sub-string in the current
record?
        ids.append(rec.STAFFID)     # append it to the list
return ids
__________
AND an argument called 'name'
That's it. You can use the list with ids in a zope page template or a dtml
page to show, e.g., the full records.
Look for 'sequences' and strings in Python docs. You can also use list
comprehension instead of the loop. Probably faster.
HTH,
Fernando