The query is fine, but if you want to perform a search every time the control's content changes, you should place it in the OnChange event - OnKeyPress is triggered before the TEdit text changes.
Correct.
and he's comparing a
single KEY in his LIKE-Operation, not the entered TEXT of his TEdit
Example:
He wants to search for "pawel"
He hits the "p"-key, and his resulting LIKE is "... LIKE '%p%'"
He hits the "a"-key, and his resulting LIKE is "... LIKE '%a%'" !!!! NOT "%pa%"
Yes, in case he still wants to use KeyPress-Event, he could work around this with a Static Const-Var inside the Event, and then concatenate with each KeyPress,
but then it starts to get funny, when User presses backspace or positions the cursor within a word and hits delete or backspace
Bottom line: Wrong Event for the intended functionality. Plain and simple
EDIT: Just looked into pawel's sample-program
MasterQuery:
SELECT * FROM mastertab WHERE :parname='''' OR name LIKE :parname ORDER BY name
procedure TForm1.esearchChange(Sender: TObject);
begin
if esearch.Text = '' then
masterquery.ParamByName('parname').AsString := ''
else
masterquery.ParamByName('parname').AsString := '%' + esearch.Text + '%';
masterquery.Refresh;
end;
Pawel, why are you OR-ing for identical on empty input??
If esearch.Text is empty ('') his LIKE results in "... LIKE '%%'" which returns all records.
You are implying that the SQLite-Engine is short-circuiting the OR, and the opinions differ, if SQLite actually short-circuits a direct "OR"
(at least what i could find)
If you want to optimize the "... LIKE '%%'" away, i'd rather go for something like this (untested)
//Somewhere above
Const
MyWHEREClause=' WHERE name like :parname ';
//..............
masterquery.SQL.Text:='select * from mastertab {WHERECLAUSE} order by name;';
//Later....
procedure TForm1.esearchChange(Sender: TObject);
begin
if esearch.Text = '' then
masterquery.SQL.Text:=StringReplace(masterquery.SQL.Text, '{WHERECLAUSE}', '', [rfReplaceAll]) //Delete the "placeholder"
else
Begin
masterquery.SQL.Text:=StringReplace(masterquery.SQL.Text, '{WHERECLAUSE}', MyWHEREClause, [rfReplaceAll]);
masterquery.ParamByName('parname').AsString := '%' + esearch.Text + '%';
End;
masterquery.Refresh;
end;