插入UTF8数据库时格式错误的字符串

时间:2013-05-30 10:53:46

标签: delphi utf-8 delphi-2010 firebird firebird2.5

我使用Delphi 2010,Firebird 2.5.2,IBExpress组件。

数据库字符集是UTF8。在db-connection UTF8中。

数据库:

var 
  Database: TIBDatabase;
begin
  ...
  Database.params.Clear;
  Database.params.Add('user ''SYSDBA'' password ''masterkey'' ');
  Database.params.Add('page_size 16384');
  Database.params.Add('default character set UTF8');

表:

CREATE TABLE NEW_TABLE (
    NEW_FIELD  VARCHAR(255)
);

连接代码:

  Database.params.Clear;
  Database.params.Add('user_name=SYSDBA');
  Database.params.Add('password=masterke');
  Database.params.Add('sql_role_name=UTF8');
  Database.Open;

插入代码:

var
  IBSQL: TIBSQL;
begin
  IBSQL := TIBSQL.Create(nil);
  try
    IBSQL.Database := db;
    IBSQL.Transaction := tr

    IBSQL.SQL.Text := 'insert into NEW_TABLE (NEW_FIELD) values (:param)';

    IBSQL.params[0].Value := 'Ãabc©'; // unsupported symbols :(

    if not IBSQL.Transaction.Active then
      IBSQL.Transaction.StartTransaction;

    IBSQL.ExecQuery; // "Malformed string" exception here

    if IBSQL.Transaction.Active then
      IBSQL.Transaction.Commit;
  finally
    FreeAndNil(IBSQL);
  end;
end;

我收到“格式错误的字符串”异常。如何插入此字符串?

1 个答案:

答案 0 :(得分:0)

确保您字段的字符集是UTF-8。

创建新字段时,您有两个选择:

  1. 使用默认字符集创建域并进行整理,并使用域
  2. 设置字段
  3. 创建不带域的字段,设置字符集并手动整理。
  4. 在下图中,显示使用IBExpert使用域创建的字段。

    IBExpert with domain and charset

相关问题