[yunqa.de] Re: D2007-D2010: corrupted text in database

  • From: Delphi Inspiration <delphi@xxxxxxxx>
  • To: yunqa@xxxxxxxxxxxxx
  • Date: Sun, 24 Jan 2010 21:34:56 +0100

At 15:40 24.01.2010, Ïåòðîâ Äìèòðèé Þðüåâè÷ wrote:

>The databases created with same code in Delphi 2007 and Delphi 2010 appears to 
>be incompatible when using Russian characters.

Before I point out the error in your code below, please know that database 
files created by DISQLite3 are compatible accross Delphi versions (D4 up to 
D2010 at the time of this writing) as well as accross different platforms 
(SQLite applications on Windows, Linux, etc.).

>Example with source code: http://www.fox-manager.com.ua/download/db.zip
>For Example this code runs great in D2007 and D2010, but when I try to read a 
>database created in D2007 in a program compiled in D2010 I get corrupted text:
>
>SQL := 'Create table if not exists Test (SomeText string);';
>DB.Execute(SQL);

The above code is wrong and D2010 results in the following compiler warning:

[DCC Warning] MainUnit.pas(39): W1057 Implicit string cast from 'string' to 
'UTF8String'

Reason:

The TDISQLite3Database.Execute methods asks for a Utf8String, hence the SQL you 
pass to it must be in UTF-8 encoding. However, literal strings default to 
UnicodeString in D2010 but to ANSI in D2007.

Solution:

To solve your problem, use the equivalent UnicodeString / WideString function:

  TDISQLite3Database.Execute16(const SQL: UnicodeString);
                            ^^

This takes a UTF-16 LE encoded string (UnicodeString / WideString) and works 
with both D2007 and D2010.

Sidebar:

While I do not speak Russian, the characters in the example do not look 
Cyrillic to me in either D2007 or D2010. However, this might be due to my 
language settings. Anyway, please ensure that you type in the correct literal 
strings. In D2010, this might result in UTF-8 encoded source code units, 
depending on your system locale.

In my experience, literal Unicode characters and strings are generally not well 
preseverd accross Delphi versions, especially when opening and reopening them 
with Unicode and non-Unicode versions of Delphi or accross different locales. 
If you really need to support multiple Delphi versions reliably, it might be 
best to load literal strings from external sources or store them as Unicode 
code point arrays and create the strings dynamically.

Ralf 

_______________________________________________
Delphi Inspiration mailing list
yunqa@xxxxxxxxxxxxx
//www.freelists.org/list/yunqa



Other related posts: