Delphi 2009 With firebird 2.1 and UTF8

The alpha/beta tester's forum for ZeosLib 7.0.x series

Report problems concerning our Delphi 2009+ version and new Zeoslib 7.0 features here.

This is a forum that will be removed once the 7.X version goes into stable!!

Moderators: gto, EgonHugeist, olehs

Locked
ibrahim
Fresh Boarder
Fresh Boarder
Posts: 12
Joined: 29.05.2009, 14:16

Delphi 2009 With firebird 2.1 and UTF8

Post by ibrahim »

Hi to you all.
I'm writing an application with a firebird 2.1 database with utf8 char set.
I use Delphi 2009 and I'm getting strange characters in my fields when I use Zeos TRUNK REV803.
I created my database with UTF8 code page and one table that have a varchar field and I can insert data in it using SQL Manager Lite from EMS using arabic characters.

I made a little application and I put codepage=UTF8 in the properties field of mu connection and, after I connect to that database, if I try to insert data with my application using Zeos connection I get a lot of '?' in my field.
When I display the record inserted manually with sql manager I see strange characters in that field.
I added to my form a label and trigged the TDBedit OnChange event and I set the caption of the label to the contete of the field using

Label1.Caption := UTF8ToWideString((sender as TDBEdit).text);

I get the label displaying the right characters.

What I'm doing wrong?

Thanks


Any idea?
ibrahim
Fresh Boarder
Fresh Boarder
Posts: 12
Joined: 29.05.2009, 14:16

Post by ibrahim »

P.S: Is there any documentation for programmers that may help to understand the structure of the zeos componets.
I can give you some help with the Interbase part of development and testing.
I tried to create the same database with MySql and I'm getting the same results.

Thanks
ibrahim
Fresh Boarder
Fresh Boarder
Posts: 12
Joined: 29.05.2009, 14:16

Post by ibrahim »

I'm making some debug and I tried to use a TZQuery component to insert some data directly addig arabic string in it and IT WORKS!!!
I use the ExecSQL procedure to execute

INSERT INTO LANGUAGES VALUES (-1,'انجليزي')

and the record was inserted in the database without any problem.

I hope this may help.

Thanks
marcov
Senior Boarder
Senior Boarder
Posts: 95
Joined: 24.06.2010, 09:17

Post by marcov »

You do too much, what you do wrong is that tdbedit.text already is unicodestring (UTF16, like widestring). The utf8 conversion is not necessary.

The codepage=utf8 cshould already let Zeos do this translation (utf8 on the "wire" to utf16 in dbedit.text).

The only thing to check is that all your memo fields are tdbwidememo, not tdbmemo, see the last post in http://zeos.firmos.at/viewtopic.php?t=2580

p.s. I set client_encoding:=utf8 too. I don't know which one of these two (codepage, client_encoding) does what exactly, but it works fine.
ibrahim
Fresh Boarder
Fresh Boarder
Posts: 12
Joined: 29.05.2009, 14:16

Post by ibrahim »

I'm getting these problems with the tdbedit and not with memo fields.
I have the same result with

codepage=utf8
client_encoding=utf8

set in the connection properies or even without them set.
ibrahim
Fresh Boarder
Fresh Boarder
Posts: 12
Joined: 29.05.2009, 14:16

Post by ibrahim »

I made some debug and I found that if I replace line 1357 of the file
that looks like this
WStrCopy(Buffer, PWideChar(RowAccessor.GetUnicodeString(ColumnIndex, Result)));

by this one
WStrCopy(Buffer, PWideChar(UTF8Decode(PAnsiChar(Utf8ToAnsiEx(RowAccessor.GetUnicodeString(ColumnIndex, Result),1252)))));

I get the fields displayed well.

I will try to make other experiments hoping this may help.
ibrahim
Fresh Boarder
Fresh Boarder
Posts: 12
Joined: 29.05.2009, 14:16

Post by ibrahim »

Hi to all,

Since I noticed a similar question about UTF8 and My sql

(look here http://zeos.firmos.at/viewtopic.php?t=2652)

I made some debug for both databases (MySql and Firebird) and I
found some solutions for it with delphi 2009.
There is a common part in the above post that works also for Interbase/firebird.
Here below what code I modified to be able to write to database UTF8 strings.

1- In the ZDbcInterbase6Utils within the IZParamsSQLDA class definition (line 131) unit I changed the following line

Code: Select all

    procedure UpdateString(const Index: Integer; Value: AnsiString);

To

Code: Select all

    {$IFDEF DELPHI12_UP}
    procedure UpdateString(const Index: Integer; Value: WideString{AnsiString});
    {$ELSE}
    procedure UpdateString(const Index: Integer; Value: AnsiString);
    {$ENDIF}
The same thing shall be done in the TZParamsSQLDA class definition and implementation.

2- In the TZParamsSQLDA.UpdateString implementation I changed

Code: Select all

             SQL_TEXT      : EncodeString(SQL_TEXT, Index, Value);
             SQL_VARYING   : EncodeString(SQL_VARYING, Index, Value);
             SQL_TYPE_DATE : EncodeString(SQL_DATE, Index, Value);
to

Code: Select all

       {$IFDEF DELPHI12_UP}
               SQL_TEXT      : EncodeString(SQL_TEXT, Index, PAnsiChar(UTF8String(Value)));
               SQL_VARYING   : EncodeString(SQL_VARYING, Index, PAnsiChar(UTF8String(Value)));
               SQL_LONG: PInteger(sqldata)^ := StrToInt(Value);
       {$ELSE}
             SQL_TEXT      : EncodeString(SQL_TEXT, Index, Value);
             SQL_VARYING   : EncodeString(SQL_VARYING, Index, Value);
             SQL_TYPE_DATE : EncodeString(SQL_DATE, Index, Value);
       {$ENDIF}
This enabled me to write and read UTF8 strings withou any problem.
Please look at the other post for the other modifications to make.
Locked