Recent

Author Topic: TSqlQuery and filter with like  (Read 12579 times)

dario

  • New member
  • *
  • Posts: 9
TSqlQuery and filter with like
« on: July 26, 2010, 10:43:46 am »
Hello, i was testing lazarus for the first time, coming from long use of Delphi.
In delphi i use this way with TClientDataset with no problems, but now in Lazarus ...

I have a TSqlQuery to filter pressing a button in this way:

SQLQuery1.Filtered:= false;
SQLQuery1.filter := 'descr like ' + QuotedStr('%' +Edit1.text+'%');
SQLQuery1.Filtered:= true; 

Where "descr" is the name oy my field where i want to search, but gives the error

Index based on unknown field "like".

This also using "*" instead of "%".
What is wrong? why lazarus thinks "like" is a field ?
If i use "descr = " instead of "descr like" its works fine!

The DB is Sqllite... Thanks for help,
Dario

Lacak2

  • Guest
Re: TSqlQuery and filter with like
« Reply #1 on: August 02, 2010, 09:48:14 am »
Try use properties ServerFilter and ServerFiltered (instead of Filter and Filtered).

Problem with Filter is caused by Filter parser, which does not recognize/support LIKE operator ...

stonefull

  • Jr. Member
  • **
  • Posts: 54
Re: TSqlQuery and filter with like
« Reply #2 on: August 02, 2010, 02:57:39 pm »
Why don't you use ExecSQL? I think is better than Filter.

merlin352

  • New Member
  • *
  • Posts: 13
Re: TSqlQuery and filter with like
« Reply #3 on: August 04, 2010, 04:02:37 pm »
Hello

or use Zeos to access your table. It has a LIKE-operator for the filter expression

aurimenes

  • Newbie
  • Posts: 2
Re: TSqlQuery and filter with like
« Reply #4 on: November 12, 2020, 09:31:09 pm »
For future reference: as there's no "like" statement in SQLDB's filter property, you can use the event "OnFilterRecord" just like this:

Code: Pascal  [Select][+][-]
  1. procedure TfrmInativarItens.qryProdutosFilterRecord(DataSet: TDataSet;
  2.   var Accept: Boolean);
  3. begin  
  4.   if dteFiltroDescr.Text <> '' then
  5.     Accept := (Pos(dteFiltroDescr.Text, qryProdutosPRO_DESCRICAO.AsString) > 0);
  6. end;
  7.  

Changing Accept to True or False (for each record) will filter your recordset.

Remember to set Filtered property to true, or else this will not work!

While this is a way to filter your records locally, it is almost always a better idea to filter your records on server-side, as it will decrease your network traffic. I suggest using this only when changing the filter options are constant (for example, filter while the user is typing) and the amount of data is small.

(sorry if my English isn't perfect :) )
Aurimenes Silva
Recife, PE - Brazil

 

TinyPortal © 2005-2018