how can i make my search box jump from one keyword to another without it check every row of the database? [closed]

旧时模样 提交于 2020-01-07 05:29:05

问题


I got this code working for my app's search box, unfortunately when it search or filters, it checks every row on the database and then when the searched word is found, it will display it on the tlabels,

procedure Tspcb.dccolbtnClick(Sender: TObject);
begin
  zdctable.First;
  while not zdctable.EOF do
  begin
     if (zdctable.FieldByName('Collector').AsString = dcedit.Text)
     then begin
        cn.Caption := zdctable.FieldByName('Client_Name').AsString;
        col.Caption := zdctable.FieldByName('Collector').AsString;
        pay.Caption := zdctable.FieldByName('Daily_Payment').AsString;
        date.Caption := zdctable.FieldByName('Date').AsString;
        ddate.Caption := zdctable.FieldByName('Due_Date').AsString;
        id.Caption := zdctable.FieldByName('ID').AsString;
        la.Caption := zdctable.FieldByName('Loan').AsString;
        tc.Caption := zdctable.FieldByName('Total_Collectibles').AsString;
     end;

     ShowMessage('click ok for next profile');
     zdctable.Next;
  end;
end;

what i want to do is for it to check rows that has the searched word on it, and not searching every row in the database.

i need help on analyzing this code, im just a beginner who is learning how to use delphi 7 and to program.

thank you


回答1:


The problem basically is with the line

if (zdctable.FieldByName('Collector').AsString = dcedit.Text)

Here you're looking for equality, so you're never going to be able to match partial strings. I suggest that you change your query (ie zdctable) so that it looks something like this

select * from zdctable
where collector like :p1

Your program would then be

zdctable.params[0].asstring:= '%' + dcedit.text + '%';
zdctable.open;

If your table is linked to a grid via a datasource, then all the matching records will be displayed; there will be no need to extract each field and show it in a different label. If you want to show one record at a time with a navigator, then turn your labels into dbtext components which are linked to the dataset; if these components are set up correctly then each field in your dataset will be displayed in a different dbText component.

Another possibility is using the 'locate' method. If I convert your statement to use 'locate', then you have written the equivalent of

zdctable.locate ('collector', dcedit.text, [])

In order to locate partial matches, you would need to write

zdctable.locate ('collector', dcedit.text, [loPartialKey])


来源:https://stackoverflow.com/questions/15956541/how-can-i-make-my-search-box-jump-from-one-keyword-to-another-without-it-check-e

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!