[triangle-zpug] SQL search

A M Thomas triangle-zpug@starship.python.net
Fri, 09 Aug 2002 11:09:44 -0400


Hi Charles,

I did something similar recently; I wanted to have a 'keyword' search
that would compare test values against possibly multiple fields, and
wanted to search for words in any order -- I enclose my code snippet
below.  It splits input so that if, for instance, one searches for "leaf
virus curl" and passes in a fieldlist that includes ['ArticleTitle',
'Body', 'Author'], any record that contains all three search terms,
whether they occur in the same field or not, or are adjacent or not. 
Anyway, I like it.

here's my function wildcard_keywords, with these parameters:

   keywords, fieldlist = ['LastName']

# -----------------------------------------------------------
from string import split
# it occurs to me that the above line may not be necessary now

outtext = ''

# keywords is a space-separated list of words;
# turn it into a regular list
keywordlist = split(keywords)  # ???

keywordcount = 0

for curkeyword in keywordlist:
  if keywordcount > 0:
    outtext += ' AND'
  outtext += ' ('

  fieldcount = 0
  for curfieldname in fieldlist:

    if fieldcount > 0:
      outtext += ' OR'
    outtext += ' (' + curfieldname + " LIKE '%" + curkeyword + "%')"

    fieldcount += 1

  outtext += ')'
  keywordcount += 1

return outtext
# -----------------------------------------------------------

Hope this is useful for someone - I'd love to know if there are holes in
it.

- AM