SQLite Forum

Help with a search command
Login
This is always going to be a slow query, but should work just fine.

What is the wrapper or Unit you are using? i.e. what Type is "dm"?

Anyway, the code you are probably looking for is:
(assuming "tstr2" is the second text to search by)

```
    tStr1:='%'+edSearch.Text+'%';
    tStr2:='%'+edSearch2.Text+'%';
    sqlStr:='SELECT * FROM sqlDiaryData '+
      ' WHERE (( fldKey0 LIKE '+QuotedStr(tStr1)+' ) '+
         ' OR  ( fldKey1 LIKE '+QuotedStr(tStr1)+' ) '+
         ' OR  ( fldKey2 LIKE '+QuotedStr(tStr1)+' ) '+
         ' OR  ( fldKey3 LIKE '+QuotedStr(tStr1)+' ) '+
         ' OR  ( fldKey4 LIKE '+QuotedStr(tStr1)+' ) '+
         ' OR  ( fldKey5 LIKE '+QuotedStr(tStr1)+' ) '+
         ' OR  ( fldKey6 LIKE '+QuotedStr(tStr1)+' ) '+
         ' OR  ( fldKey7 LIKE '+QuotedStr(tStr1)+' ) '+
         ' OR  ( fldKey8 LIKE '+QuotedStr(tStr1)+' ) '+
         ' OR  ( fldKey9 LIKE '+QuotedStr(tStr1)+' ) '+
         ') '+ 
         'AND (( fldKey0 LIKE '+QuotedStr(tStr2)+' ) '+
         ' OR  ( fldKey1 LIKE '+QuotedStr(tStr2)+' ) '+
         ' OR  ( fldKey2 LIKE '+QuotedStr(tStr2)+' ) '+
         ' OR  ( fldKey3 LIKE '+QuotedStr(tStr2)+' ) '+
         ' OR  ( fldKey4 LIKE '+QuotedStr(tStr2)+' ) '+
         ' OR  ( fldKey5 LIKE '+QuotedStr(tStr2)+' ) '+
         ' OR  ( fldKey6 LIKE '+QuotedStr(tStr2)+' ) '+
         ' OR  ( fldKey7 LIKE '+QuotedStr(tStr2)+' ) '+
         ' OR  ( fldKey8 LIKE '+QuotedStr(tStr2)+' ) '+
         ' OR  ( fldKey9 LIKE '+QuotedStr(tStr2)+' ) '+
         ')'+
         ' ORDER BY '+fldKey0;
  dm.sqlDiaryData.SQL.Text:=sqlStr;

```

A better solution would be a wrapper that allows preparing a statement and binding those fields, but the above should work fine.

May I further suggest, out of pedanticness, this algorithm:

```
    // Assuming tStr1 and tStr2 are the two filters and get filled from some text boxes:
    tStr1:=QuotedStr('%'+edSearch1.Text+'%');
    tStr2:=QuotedStr('%'+edSearch2.Text+'%');

    // These two nearly identical loops should really be made into a function.
    tStrP1:='';
    for f:=0 to 9 do begin
      tStrF:=Format('(fldKey%d LIKE %s)', [f,tStr1]);
      if (f>0) then tStrF := ' OR '+tStrF;
      tStrP1:=tStrP1+tStrF;
    end;
    tStrP2:='';
    for f:=0 to 9 do begin
      tStrF:= Format('(fldKey%d LIKE %s)', [f,tStr2]);
      if (f>0) then tStrF:=' OR '+tStrF;
      tStrP2:=tStrP2+tStrF;
    end;

    // Much easier to code and visualize now:
    sqlStr:='SELECT * FROM sqlDiaryData WHERE ('+tStrP1+') AND ('+tStrP2+');';

```