Lazarus

Programming => Databases => Topic started by: Pe3s on May 11, 2022, 06:00:57 pm

Title: [SOLVED] SQLite search edit background color
Post by: Pe3s on May 11, 2022, 06:00:57 pm
Hello, is it possible to make such a search engine functionality
1. empty edit control white
2. if found green
3. if not found color red
4. if we clear the control it returns to white

Code: Pascal  [Select][+][-]
  1. procedure TForm1.Edit1Change(Sender: TObject);
  2. begin
  3.   with ZQuery1 do begin
  4.   SQL.Clear;
  5.   SQL.Text := 'SELECT * FROM baza WHERE Nazwisko LIKE :Nazwisko';
  6.   Active := False;
  7.   ParamByName('Nazwisko').AsString := Edit1.Text + '%';
  8.   Active := False;
  9.   Open;
  10. end;
  11. end;
Title: Re: SQLite search edit background color
Post by: dseligo on May 12, 2022, 02:02:37 am
Try this:

Code: Pascal  [Select][+][-]
  1. procedure TForm1.Edit1Change(Sender: TObject);
  2. begin
  3.   If (Sender as TEdit).Text = '' then begin
  4.     (Sender as TEdit).Color := clDefault;
  5.     Exit;
  6.   end;
  7.  
  8.   with ZQuery1 do begin
  9.     // SQL.Clear; // you don't need this if you directly set SQL.Text property
  10.     SQL.Text := 'SELECT * FROM baza WHERE Nazwisko LIKE :Nazwisko'; // If you use ZEOSdbo you don't need Active := False at beginning
  11.     // Active := False; // don't need this here
  12.     ParamByName('Nazwisko').AsString := (Sender as TEdit).Text + '%';
  13.     // Active := False; // don't need this here
  14.     Open;
  15.     If Eof then (Sender as TEdit).Color := clRed
  16.            else (Sender as TEdit).Color := clGreen;
  17.   end;
  18. end;
Title: Re: SQLite search edit background color
Post by: Zvoni on May 12, 2022, 08:15:31 am
Wouldn't use the Change-Event, rather KeyUp
OnChange is only useful in this context if you Copy/Paste your search-Text into the TEdit

btw: Don't you have to close the Query first?
With this code you'd open the query every time you type a new letter without having closed it

And don't use SELECT * FROM
Title: Re: SQLite search edit background color
Post by: dseligo on May 12, 2022, 02:28:15 pm
Wouldn't use the Change-Event, rather KeyUp
OnChange is only useful in this context if you Copy/Paste your search-Text into the TEdit

Why, what's wrong with OnChange event? Wouldn't OnKeyUp react also to arrow keys and similar, causing unnecessary queries to the database?

btw: Don't you have to close the Query first?
With this code you'd open the query every time you type a new letter without having closed it

If you use ZEOSdbo and assign to SQL.Text, you don't need to close query first. I assume he uses ZEOS because query is named ZQuery1.
Although, I would rather use prepared query and only change parameter (then query should be closed first).
Title: Re: SQLite search edit background color
Post by: Zvoni on May 12, 2022, 03:02:39 pm
Why, what's wrong with OnChange event? Wouldn't OnKeyUp react also to arrow keys and similar, causing unnecessary queries to the database?
Exactly because of that. You can filter "illegal" keys.
the OnChange-Event doesn't ship the Key being pressed.

Now imagine having a "Search-History" (stored in a List or whatever).
And now imagine pressing the Arrow-Down-Button to quickswitch to the searchphrase before your current one.....

EDIT: And just looking at his First Post, i'm going to throw a Spanner into the gearbox, and enter a "%" as the first Character using his code....

EDIT2: btw: Or i'd use OnKeyDown in that case....
Title: Re: SQLite search edit background color
Post by: Pe3s on May 12, 2022, 06:57:49 pm
Thanks for your help and advice :)
Title: Re: SQLite search edit background color
Post by: dseligo on May 12, 2022, 07:18:37 pm
Why, what's wrong with OnChange event? Wouldn't OnKeyUp react also to arrow keys and similar, causing unnecessary queries to the database?
Exactly because of that. You can filter "illegal" keys.
the OnChange-Event doesn't ship the Key being pressed.

But OnChange doesn't fire when you press arrow keys, Page up/down, ...
It only fires when 'Text' is changed.
I don't understand the advantage of OnKeyUp if one only wants to know when content changes.

Now imagine having a "Search-History" (stored in a List or whatever).
And now imagine pressing the Arrow-Down-Button to quickswitch to the searchphrase before your current one.....

He asked about TEdit. But you can still use OnKeyDown for 'Search-History',.
Title: Re: [SOLVED] SQLite search edit background color
Post by: Zvoni on May 12, 2022, 10:29:19 pm
Have you read my edit?
Entering ‚%‘ as the first character (and OnChange you can‘t filter out keystrokes) would result in loading the whole table (PacketRecord not withstanding)
Title: Re: [SOLVED] SQLite search edit background color
Post by: dseligo on May 13, 2022, 01:51:52 am
Have you read my edit?
Entering ‚%‘ as the first character (and OnChange you can‘t filter out keystrokes) would result in loading the whole table (PacketRecord not withstanding)

Maybe he wants that. Or maybe he wants to search for actual '%' character in the table, and he will escape this characters in the text prior to running query.

He could also check text these characters and remove them prior to running query. Or he could use OnKeyUp for filtering, and OnChange for running query.
TinyPortal © 2005-2018