TurboDB VCL Component Library

Storing ANSI and Unicode Strings

Previous  Top  Next

In Delphi and C++ Builder up to version 2007 the standard string is AnsiString which can easily be stored in string columns and memo columns. In these versions Unicode strings must be declared as WideString and some thoughts are required on how to store them in the database correctly. With Delphi and C++ Builder starting from version 2009, the standard string is a Unicode string and difficulties come up when you store them as (ANSI) strings or memos.

When Storing Unicode Strings with Delphi/C++ Builder 2007 and Below

The TurboDB Engine supports Unicode in WideString columns and in WideMemos. Working with those data types in a VCL application however contains some pitfalls. The VCL almost everywhere uses AnsiStrings in its functions and components and converts Unicode strings automatically and without warning to this type. As a consequence the database controls also only receive and pass AnsiStrings. This means there is no way to enter and/or view Unicode data from your database in VCL data controls.

This is what you can do with the Unicode data in your TurboDB database within a VCL application:

Show and edit strings in normal controls, e.g. TEdit. Those controls work with Unicode correctly but you will have to write your own updating and posting logic, since normal controls have no DataSource link.
Work with the Unicode data internally. You can read and write WideStrings from and to a data set component.
When accessing Unicode data from a TdbDataSet be careful not to use the TField.AsString property but the Value or the AsWideString property.
Query parameters (i.e. items of the TTdbQuery.Params collection) can only be set via the TParam.Value property.
If you are working with Unicode memos you will notice quickly that VCL does not offer a data type for this kind of columns. Unicode memos are represented by the ftBlob field type and the custom field class TTdbBlobField, which offers a AsWideString property.
The Filter property in VCL components as well as the SQL property are both of type AnsiString. Therefore you cannot use them for searching for Unicode strings. The TurboDB database components therefore have additional FilterW and SQLW properties which are of type WideString.

When Storing ANSI Strings with Delphi/C++ Builder 2009 and Above

Many applications that need not be localized for other languages can still easily store its text data within string and memo columns, which have two advantages:

Better compatibility with older applications
Less storage requirements and therefore also better performance.

However storing (Unicode) strings in (ANSI) string and memo columns requires a conversion of the Delphi variable to an AnsiString. You must be sure that the content of the UnicodeString variable does not contain true Unicode characters or you will loose some of the information in the string when storing it to the database.