Use Parameters in TLiteQuery

Discussion of open issues, suggestions and bugs regarding LiteDAC (SQLite Data Access Components) for Delphi, C++Builder, Lazarus (and FPC)
Post Reply
bjaustin
Posts: 3
Joined: Sun 27 Jan 2013 13:00

Use Parameters in TLiteQuery

Post by bjaustin » Sun 27 Jan 2013 14:18

I am creating a login form and I need to set the parameters of a query component through the code. How should I do that (both the parameters are UnicodeStrings)?

AlexP
Devart Team
Posts: 5530
Joined: Tue 10 Aug 2010 11:35

Re: Use Parameters in TLiteQuery

Post by AlexP » Mon 28 Jan 2013 10:08

Hello,

You should enable the UseUnicode option in LiteConnection before connectiong to the database and assign parameter values using the asSttring property.

Code: Select all

  LiteConnection1.Options.UseUnicode := True;
  LiteConnection1.Connect;
  LiteQuery1.SQL.Text := 'select :p1';
  LiteQuery1.ParamByName('p1').AsString := Edit1.text;
  LiteQuery1.Open;

bjaustin
Posts: 3
Joined: Sun 27 Jan 2013 13:00

Re: Use Parameters in TLiteQuery

Post by bjaustin » Thu 31 Jan 2013 23:01

Thank you. I have another question. How would one check to see if a query returned results?

AlexP
Devart Team
Posts: 5530
Joined: Tue 10 Aug 2010 11:35

Re: Use Parameters in TLiteQuery

Post by AlexP » Fri 01 Feb 2013 11:16

Hello,

To define DataSet or not, you can use two properties: IsEmpty and RecordCount,
the first one returns True if the DataSet is empty, the second one - 0, for example:

Code: Select all

  LiteQuery1.SQL.Text := 'select :p1 where 1<>1';
  LiteQuery1.ParamByName('p1').AsInteger := 1;
  LiteQuery1.Open;
  ShowMessage(VarToStr(LiteQuery1.IsEmpty)); //True
  ShowMessage(VarToStr(LiteQuery1.RecordCount = 0)); //True

bjaustin
Posts: 3
Joined: Sun 27 Jan 2013 13:00

Re: Use Parameters in TLiteQuery

Post by bjaustin » Sat 02 Feb 2013 13:31

When trying the code that you gave:

Code: Select all

ShowMessage(VarToStr(LiteQuery1.IsEmpty));
I get True, but it should return False as the values that I enter are in my database.

The database values are

Code: Select all

uname=admin;passw=admin
And my code is

Code: Select all

this->LiteQuery1->ParamByName("uname")->AsString = uname; //admin
	   this->LiteQuery1->ParamByName("passw")->AsString = pwd; //admin
	   ShowMessage(VarToStr(this->LiteQuery1->IsEmpty()));

AlexP
Devart Team
Posts: 5530
Joined: Tue 10 Aug 2010 11:35

Re: Use Parameters in TLiteQuery

Post by AlexP » Mon 04 Feb 2013 11:40

Hello,

Please check that, after opening the DataSet, the RecordCount property returns a value greater than 0, and that the data in the database is equal to that you set in the parameters (no spaces, the case of symbols is the same).

Code: Select all

this->LiteQuery1->ParamByName("uname")->AsString = uname; //admin
this->LiteQuery1->ParamByName("passw")->AsString = pwd; //admin
this->LiteQuery1->Open;
ShowMessage(VarToStr(this->LiteQuery1->IsEmpty()));
ShowMessage(VarToStr(this->LiteQuery1->RecordCount));
assert((this->LiteQuery1->RecordCount = 0) = this->LiteQuery1->IsEmpty());

ecig12
Posts: 1
Joined: Fri 08 Feb 2013 09:41

Re: Use Parameters in TLiteQuery

Post by ecig12 » Fri 08 Feb 2013 09:52

Version 1.6.4 has add the TLiteSQL and TLiteQuery.
Data base connection make sure.

Requirement is C++,Turbo.

AlexP
Devart Team
Posts: 5530
Joined: Tue 10 Aug 2010 11:35

Re: Use Parameters in TLiteQuery

Post by AlexP » Fri 08 Feb 2013 13:42

Hello,

We cannot reproduce the problem, please send the DB file to alexp*devart*com

Post Reply