Delphi Pages Forums  

Go Back   Delphi Pages Forums > Delphi Forum > DB-Aware

Lost Password?

Reply
 
Thread Tools Display Modes
  #1  
Old 04-16-2009, 06:51 PM
beh.tehrani beh.tehrani is offline
Senior Member
 
Join Date: Mar 2009
Posts: 316
Default show Like words in database in dbgrid

hello

i want if enter word in edit3 for search, if like with other words in field, searched.

for example:

matrin is exist in name field. now if user enter in edit3, mart , project can searched.

thanks


[delphi] ADOTable1.First;
if ADOTable1.Locate('name',edit3.Text ,[]) then begin
Edit1.Text := ADOTable1.FieldByName('name').AsString;
Edit2.Text := ADOTable1.FieldByName('tell').AsString;
end
else
if ADOTable1.Locate('tell',edit3.Text ,[]) then begin
Edit1.Text := ADOTable1.FieldByName('name').AsString;
Edit2.Text := ADOTable1.FieldByName('tell').AsString;
end else
begin
Edit1.Text := '';
Edit2.Text := '';
end;[/delphi]

i'm using delphi 2009
Reply With Quote
  #2  
Old 04-16-2009, 07:21 PM
Jasser Jasser is offline
Moderator
 
Join Date: Jan 2005
Location: Saudi Arabia
Posts: 5,059
Default RE: show Like words in database in dbgrid

It is better to use a query for searching. Example:

[DELPHI]procedure TForm1.Button1Click(Sender: TObject);
begin
ADOQuery1.Close;
ADOQuery1.SQL.Clear;
ADOQuery1.SQL.Add(Format('SELECT * FROM Table1 WHERE Name = "%s" OR Tell = "%s"',[Edit3.Text,Edit3.Text]));
ADOQuery1.Open;
if ADOQuery1.RecordCount = 0 then begin
Edit1.Text := '';
Edit2.Text := '';
end else begin
Edit1.Text := ADOQuery1['Name'];
Edit2.Text := ADOQuery1['Tell'];
end;
end;[/DELPHI]

Regards,
Abdulaziz Jasser
Reply With Quote
  #3  
Old 04-16-2009, 08:15 PM
mshkolnik mshkolnik is offline
Senior Member
 
Join Date: Jul 2001
Posts: 4,195
Default RE: show Like words in database in dbgrid

Add the loPartialKey flag in Locate method:
if ADOTable1.Locate('name',edit3.Text ,[loPartialKey]) then begin
...

With best regards, Mike Shkolnik
http://www.scalabium.com
Reply With Quote
  #4  
Old 04-16-2009, 08:23 PM
beh.tehrani beh.tehrani is offline
Senior Member
 
Join Date: Mar 2009
Posts: 316
Default RE: show Like words in database in dbgrid

hello

but with this codes can't find like word in database.

for example:

if i enter "mar", project find "martin"

thanks

i'm using delphi 2009
Reply With Quote
  #5  
Old 04-16-2009, 08:46 PM
Jasser Jasser is offline
Moderator
 
Join Date: Jan 2005
Location: Saudi Arabia
Posts: 5,059
Default RE: show Like words in database in dbgrid

Very easy with SQL. Example:

[DELPHI]procedure TForm1.Button1Click(Sender: TObject);
begin
ADOQuery1.Close;
ADOQuery1.SQL.Clear;
ADOQuery1.SQL.Add(Format('SELECT * FROM Table1 WHERE Name = "%%s" OR Tell = "%%s"',[Edit3.Text,Edit3.Text]));
ADOQuery1.Open;
if ADOQuery1.RecordCount = 0 then begin
Edit1.Text := '';
Edit2.Text := '';
end else begin
Edit1.Text := ADOQuery1['Name'];
Edit2.Text := ADOQuery1['Tell'];
end;
end;[/DELPHI]

Regards,
Abdulaziz Jasser
Reply With Quote
  #6  
Old 04-16-2009, 08:47 PM
beh.tehrani beh.tehrani is offline
Senior Member
 
Join Date: Mar 2009
Posts: 316
Default RE: show Like words in database in dbgrid

thank you very much.

and my problem fixed.

now your code can do only english language.

but i want use this for persian language or arabic

for repair this problem, help me

thanks

i'm using delphi 2009
Reply With Quote
  #7  
Old 04-16-2009, 09:03 PM
mshkolnik mshkolnik is offline
Senior Member
 
Join Date: Jul 2001
Posts: 4,195
Default RE: show Like words in database in dbgrid

If your field in table (in database) is unicode, all will work without changes on client side.

With best regards, Mike Shkolnik
http://www.scalabium.com
Reply With Quote
  #8  
Old 04-16-2009, 09:05 PM
Jasser Jasser is offline
Moderator
 
Join Date: Jan 2005
Location: Saudi Arabia
Posts: 5,059
Default RE: show Like words in database in dbgrid

Put "%" at the beginning, at the end, or in both to make partial search. I forget to use "LIKE" clause. The code should be something like:

[DELPHI]ADOQuery1.SQL.Add(Format('SELECT * FROM Table1 WHERE Name LIKE "%%s%" OR LIKE = "%%s%s"',[Edit3.Text,Edit3.Text]));[/DELPHI]
NOTE: SQL is much powerful than filter property.

Regards,
Abdulaziz Jasser
Reply With Quote
  #9  
Old 04-16-2009, 09:28 PM
Jasser Jasser is offline
Moderator
 
Join Date: Jan 2005
Location: Saudi Arabia
Posts: 5,059
Default RE: show Like words in database in dbgrid

[DELPHI]ADOQuery1.SQL.Add(Format('SELECT * FROM Table1 WHERE Name LIKE "%%s%" OR LIKE "%%s%s"',[Edit3.Text,Edit3.Text]));[/DELPHI]

Regards,
Abdulaziz Jasser
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT. The time now is 04:18 PM.


Powered by vBulletin® Version 3.8.8
Copyright ©2000 - 2019, vBulletin Solutions, Inc.