无法保存或加载FireDAC TFDQuery中的外文字符。

huangapple go评论59阅读模式
英文:

Unable to Save or Load Foreign Characters with FireDAC TFDQuery

问题

I have an SQLite database that is configured as:

SQLiteFDConnection->Params->Values["StringFormat"] = _D("Unicode");
SQLiteFDConnection->Params->Values["OpenMode"] = _D("CreateUTF8");

It has two tables accessed through a TFDQuery:

A) Table1 (TFDQuery) has persistent fields and uses a TDataSource to show items in a TDBGrid.

B) Table2 (TFDQuery) also has persistent fields.

Table1 works, it shows foreign characters correctly, and the persistent fields are set up to use TWideStringField.

Table2's field is also a TWideStringField and directly uses Table2 to ExecSQL() INSERT INTO and UPDATE statements. For reading back, it uses TFDQuery::Open() then FieldByName(). However, the foreign characters are just question marks.

My question is, since I set up the StringFormat to be Unicode, why is Table2 not properly using Unicode?

Updating uses:

SQLiteDataModule->Table2->ExecSQL("UPDATE Table2 SET Name=:N where UniqueId=:U",
OPENARRAY(Variant, (UnicodeStringVarName, UniqueId)));

Reading it back uses Select * from Table2 in SQLiteDataModule->Table2->Open():

String Name = SQLiteDataModule->Table2->FieldByName("Name")->AsString;

One problem is I can't output debug information to the Event window and show the correct characters. I wanted to check it just before the Update query, but it always shows ?? ?? (there are 4 chars). The Name is used in a TTreeNode and I can see foreign characters if I use a normal string, so somewhere between storing and loading it back results with ?? ??.

String is a UnicodeString.

Any ideas?

英文:

I have an SQLite database that is configured as:

SQLiteFDConnection->Params->Values["StringFormat"]=_D("Unicode");
SQLiteFDConnection->Params->Values["OpenMode"]=_D("CreateUTF8");

It has two tables accessed through a TFDQuery:

A) Table1 (TFDQuery) has persistent fields and uses a TDataSource to show items in a TDBGrid.

B) Table2 (TFDQuery) also has persistent fields.

Table1 works, it shows foreign characters correctly, and the persistent fields are setup to use TWideStringField.

Table2's field is also a TWideStringField and directly uses the Table2 to ExecSQL() INSERT INTO and UPDATE statements. For reading back, it uses TFDQuery::Open() then FieldByName(). However, the foreign characters are just question marks.

My question is, since I setup the StringFormat to be Unicode, why is Table2 not properly using Unicode?

Updating uses:

SQLiteDataModule->Table2->ExecSQL("UPDATE Table2 SET Name=:N where UniqueId=:U",
				 OPENARRAY(Variant, (UnicodeStringVarName, UniqueId)));

Reading it back uses Select * from Table2 in SQLiteDataModule->Table2->Open():

String Name=SQLiteDataModule->Table2->FieldByName("Name")->AsString;

One problem is I can't output debug information to the Event window and show the correct characters. I wanted to check it just before the Update query, but it always shows ?? ?? (there are 4 chars). The Name is used in a TTreeNode and I can see foreign characters if I use a normal string, so somewhere between storing and loading it back results with ?? ??.

String is a UnicodeString.

Any ideas?

答案1

得分: 1

需要手动提供字段类型:

SQLiteDataModule->Table2->ExecSQL("UPDATE Table2 SET Name=:N where UniqueId=:U",
                     OPENARRAY(Variant, (UnicodeStringVarName, UniqueId)),
                     OPENARRAY(TFieldType, (ftWideString)); // <<<< Field Types

请注意,这是代码示例中的一部分,只提供了翻译,不包括其他内容。

英文:

The answer is you need to provide the field types manually:

SQLiteDataModule-&gt;Table2-&gt;ExecSQL(&quot;UPDATE Table2 SET Name=:N where UniqueId=:U&quot;,
				 OPENARRAY(Variant, (UnicodeStringVarName, UniqueId)),
				 OPENARRAY(TFieldType, (ftWideString)); // &lt;&lt;&lt;&lt; Field Types

huangapple
  • 本文由 发表于 2023年5月11日 06:36:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/76223001.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定