Delphi XE,Firebird和UTF8 [英] Delphi XE, Firebird and UTF8

查看:308
本文介绍了Delphi XE,Firebird和UTF8的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将D7程序升级到XE,在Delphi 7下,我有这样的代码...

I'm upgrading a D7 program to XE, and under Delphi 7 I had code like this...

ParamByName('Somefield').AsString:= someutf8rawbytestring;

ParamByName ('Somefield').AsString:=someutf8rawbytestring;

在XE下,如果someutf8rawbytestring包含Unicode字符(例如西里尔字母),则它们显示为????.在数据库中.

Under XE if someutf8rawbytestring contains unicode characters such as Cyrillic script, then they appear as ???? in the DB.

我看到someutf8rawbytestring的长度为8个字符,对于我的4个字符串,这是正确的.但是在数据库中只有四个字符.

I see that someutf8rawbytestring is 8 characters long, for my 4 character string, which is correct. But in the DB there are just four characters.

我正在通过带有XE的TIBQuery使用Firebird 2,并更新字符类型为'NONE'的Varchar字段.

I'm using Firebird 2 through TIBQuery with XE and updating a Varchar field with character type 'NONE'.

所以看起来是因为检测到utf8并以某种方式将其转换回unicode数据点,然后才导致DB的字符串转换失败.我尝试将varchar字段设置为UTF8编码,但结果相同.

So what it looks like is that the utf8 is being detected and converted somehow back to unicode data points, and then that is failing a string conversion for the DB. I've tried setting the varchar field to UTF8 encoding but with the same result.

那应该如何处理?

我可以使用数据库工具并编辑我的DB字段以包含一些非ASCII数据,当我读回它时,它是一个utf8编码的字符串,可以在其上使用UTF8decode,这是正确的.但是,如果没有很多帮助,将数据写回到该字段似乎是不可能的.在数据库中.我已经尝试过ParamByName('Somefield').AsString:= somewidestring;和ParamByName('Somefield').AsWideString:= somewidestring;而我只是在数据库中得到了垃圾...

I can use a database tool and edit my DB field to have some non-ASCII data and when I read it back it comes as a utf8 encoded string that I can use UTF8decode on and it's correct. But writing data back to this field seems impossible without getting a bunch of ???? in the DB. I've tried ParamByName ('Somefield').AsString:=somewidestring; and ParamByName ('Somefield').AsWideString:=somewidestring; and I just get rubbish in the DB...

这是代码(一次迭代)...

Here's the code (in one iteration) ...



procedure TFormnameEdit.savename(id : integer);
begin
    With DataModule.UpdateNameQuery do begin
        ParamByName ('Name').AsString:=UTF8Encode(NameEdit.Text);
        ParamByName ('ID').AsInteger:=id;
        ExecSQL;
        Transaction.Commit;
    end;
end;

推荐答案

按照@Lightbulb的建议,将lc_ctype = UTF8添加到TIBDatabase参数中即可解决此问题.

As @Lightbulb recommended, adding lc_ctype=UTF8 to the TIBDatabase params solved the problem.

这篇关于Delphi XE,Firebird和UTF8的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆