UNICODE string getting problem from LiteQuery

Discussion of open issues, suggestions and bugs regarding LiteDAC (SQLite Data Access Components) for Delphi, C++Builder, Lazarus (and FPC)
Post Reply
AlexeyPikurov
Posts: 5
Joined: Thu 18 May 2017 10:56

UNICODE string getting problem from LiteQuery

Post by AlexeyPikurov » Thu 18 May 2017 11:05

LiteDAC 2.6.19, Delphi XE3, x32 project.

My code:

Code: Select all

var s: string;
....
    with LiteQuery do begin
      Close;         //       0          1         2         3       4
      SQL.Text := 'select internal_idx, dt_utc, uinfrom, msg_type, msg ' +
                  'from privates ' +
                  'where uinwith=:uinwith and uin_owner=:uin_owner and dt_utc>=:dtFrom and dt_utc<=:dtTo and servid=:servid ' +
                  'order by dt_utc';

      ParamByName('uinwith').AsInteger   := iUINWith;
      ParamByName('uin_owner').AsInteger := iUINOwner;
      ParamByName('servid').AsInteger    := iServID;
      ParamByName('dtFrom').AsDateTime   := dtUTCFrom;
      ParamByName('dtTo').AsDateTime     := dtUTCTo;

      Open;

        while not eof do begin
          s := FieldList[4].AsString;
          Next;
        end;
    end; 
DataBase: "L$ғарыш айлағы/ғарышалаң"
"s" variable: "L$?арыш айла?ы/?арышала?"

Why in "s" variable is stored a non-unicode string? In database all Ok. How can I get the original UTF8 string from DB in this query?

MaximG
Devart Team
Posts: 1030
Joined: Mon 06 Jul 2015 11:34

Re: UNICODE string getting problem from LiteQuery

Post by MaximG » Fri 19 May 2017 09:13

To use various national charsets when working with SQLite database, set the UseUnicode property of the TLiteConnection component to True.

For example:

Code: Select all

LiteConnection1.Options.UseUnicode := True;

AlexeyPikurov
Posts: 5
Joined: Thu 18 May 2017 10:56

Re: UNICODE string getting problem from LiteQuery

Post by AlexeyPikurov » Fri 19 May 2017 13:30

It doesn't work. UNICODE strings saving Ok into DB table (with this option or without it, no matter).

Image

When I read varchar from DB — I have a problem, LiteQuery component converts it to ANSI, with "?" symbols instead a national characters. I checked it under debugger.

AlexeyPikurov
Posts: 5
Joined: Thu 18 May 2017 10:56

Re: UNICODE string getting problem from LiteQuery

Post by AlexeyPikurov » Mon 22 May 2017 09:16

Please help me, I'm a registered customer.

MaximG
Devart Team
Posts: 1030
Joined: Mon 06 Jul 2015 11:34

Re: UNICODE string getting problem from LiteQuery

Post by MaximG » Tue 23 May 2017 07:15

Of course, we are ready to help you and will investigate the problem. For this, we will need DB file (it can include only the described table) and a full source code of a small project, execution of which causes the problem. Please send this data using the e-support form ( https://www.devart.com - menu «Support»\«Request Support» )

AlexeyPikurov
Posts: 5
Joined: Thu 18 May 2017 10:56

Re: UNICODE string getting problem from LiteQuery

Post by AlexeyPikurov » Wed 31 May 2017 14:52

Ok, I found the problem.

Line "LiteQuery.Connection.Options.UseUnicode := true;" MUST be after "LiteConnection.Connect;".
Only in this case UNICODE symbols do not converting to ANSI.

Code:

Code: Select all

  LiteConnection := TLiteConnection.Create(nil);
  LiteConnection.Options.UseUnicode := true;

  LiteQuery := TLiteQuery.Create(Nil);
  LiteQuery.Connection := LiteConnection;
// wrong!
// LiteQuery.Connection.Options.UseUnicode := true;

  LiteConnection.ConnectString := 'Direct=True;' +
                                  'Database=' + ExtractFilePath(ParamStr(0)) + 'testunicode.db;' +
                                  'Login Prompt=False';
  LiteConnection.Connect;

  // only right here!
  LiteQuery.Connection.Options.UseUnicode := true;

  LiteSQL := TLiteSQL.Create(Nil);
  LiteSQL.Connection := LiteConnection;
Full example code source with test database (Delphi XE3).

MaximG
Devart Team
Posts: 1030
Joined: Mon 06 Jul 2015 11:34

Re: UNICODE string getting problem from LiteQuery

Post by MaximG » Fri 02 Jun 2017 07:23

You can specify the connection parameters in LiteDAC in two ways: either using the LiteConnection.Options property, or by passing a string indicating the values of these parameters to the LiteConnection.ConnectString property. In the second case, if you did not specify any of the connection parameters (for example, UseUnicode) in the passed string, this parameter will get a default value when it is opened (for UseUniсode it is False). Add this parameter in the ConnectString :

Code: Select all

LiteConnection.ConnectString := 'Direct=True;' +
'Database=' + ExtractFilePath(ParamStr(0)) + 'testunicode.db;' +
'Login Prompt=False;' +
'UseUnicode=True'; 

AlexeyPikurov
Posts: 5
Joined: Thu 18 May 2017 10:56

Re: UNICODE string getting problem from LiteQuery

Post by AlexeyPikurov » Fri 02 Jun 2017 15:22

Thank you, Max, I'll try it!

MaximG
Devart Team
Posts: 1030
Joined: Mon 06 Jul 2015 11:34

Re: UNICODE string getting problem from LiteQuery

Post by MaximG » Tue 06 Jun 2017 07:23

We were glad to help you. Please don't hesitate to contact us with questions concerning LiteDAC usage.

Post Reply