Problem with query on XE7

Discussion of open issues, suggestions and bugs regarding UniDAC (Universal Data Access Components) for Delphi, C++Builder, Lazarus (and FPC)
Post Reply
kko
Posts: 3
Joined: Sat 30 May 2015 15:10

Problem with query on XE7

Post by kko » Sat 30 May 2015 15:18

Why this code work:

Code: Select all

procedure TForm1.Edit1KeyUp(Sender: TObject; var Key: Word; Shift: TShiftState);
begin
case RadioGroup1.ItemIndex of
0: UniQuery1.SQL.Text:='Select Name, address, Phone, email, status, uslogin from clients where Name like :text order by name';
1: UniQuery1.SQL.Text:='Select Name, address, Phone, email, status, uslogin from clients where Phone like :text order by name';
2: UniQuery1.SQL.Text:='Select Name, address, Phone, email, status, uslogin from clients where email like :text order by name';
end;
UniQuery1.ParamByName('text').Value:='%'+Edit1.text+'%';
UniQuery1.Execute;
end;
But this code doesn't work:

Code: Select all

procedure TForm1.Edit1KeyUp(Sender: TObject; var Key: Word; Shift: TShiftState);
begin
UniQuery1.SQL.Text:='Select Name, address, Phone, email, status, uslogin from clients where :st like :text order by name';
case RadioGroup1.ItemIndex of
0: UniQuery1.ParamByName('st').Value:='Name';
1: UniQuery1.ParamByName('st').Value:='Phone';
2: UniQuery1.ParamByName('st').Value:='email';
end;
UniQuery1.ParamByName('text').Value:='%'+Edit1.text+'%';
UniQuery1.Execute;
end;
What am i doing wrong? why it won't work?

FCS
Posts: 176
Joined: Sat 23 Feb 2013 18:46

Re: Problem with query on XE7

Post by FCS » Sat 30 May 2015 16:38

Hello,

Maybe you should call UniQuery1.Prepare before execute it.
I'm afraid that you can't send field name as a parameter.
Read about macros in the documentation of UniDac.

Regards
Michal

kko
Posts: 3
Joined: Sat 30 May 2015 15:10

Re: Problem with query on XE7

Post by kko » Sun 31 May 2015 04:16

Thank you for your suggestion!
But it won't work with Prepare too

Code: Select all

procedure TForm1.Edit1KeyUp(Sender: TObject; var Key: Word; Shift: TShiftState); //поиск клиента
begin
UniQuery1.SQL.Text:='Select Name, address, Phone, email, status, uslogin from clients where :st like :text order by name';
case RadioGroup1.ItemIndex of
0: UniQuery1.ParamByName('st').Value:='Name';
1: UniQuery1.ParamByName('st').Value:='Phone';
2: UniQuery1.ParamByName('st').Value:='email';
end;
UniQuery1.ParamByName('text').Value:='%'+Edit1.text+'%';
UniQuery1.Prepare;
if UniQuery1.Prepared then UniQuery1.Execute;
end;

azyk
Devart Team
Posts: 1119
Joined: Fri 11 Apr 2014 11:47
Location: Alpha Centauri A

Re: Problem with query on XE7

Post by azyk » Tue 02 Jun 2015 08:06

FCS is right. To change the field name in the WHERE clause of an SQL query, you can use UniDAC macros. For this, specify a string value with the name of the field in the TMacro.Value macro property. Your code with the mentioned modifications will look like the following:

Code: Select all

procedure TForm1.Edit1KeyUp(Sender: TObject; var Key: Word; Shift: TShiftState);
begin
UniQuery1.SQL.Text:='Select Name, address, Phone, email, status, uslogin from clients where &st like :text order by name';
case RadioGroup1.ItemIndex of
0: UniQuery1.MacroByName('st').Value:='Name';
1: UniQuery1.MacroByName('st').Value:='Phone';
2: UniQuery1.MacroByName('st').Value:='email';
end;
UniQuery1.ParamByName('text').Value:='%'+Edit1.text+'%';
UniQuery1.Execute;
end;
See more details about macros in our online documentation: https://www.devart.com/unidac/docs/#dev ... macros.htm

kko
Posts: 3
Joined: Sat 30 May 2015 15:10

Re: Problem with query on XE7

Post by kko » Fri 05 Jun 2015 06:57

azyk
Thank you! Now it works.

azyk
Devart Team
Posts: 1119
Joined: Fri 11 Apr 2014 11:47
Location: Alpha Centauri A

Re: Problem with query on XE7

Post by azyk » Fri 05 Jun 2015 09:49

If any other questions come up, please contact us.

Post Reply