打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
Delphi中让sqlite显示Unicode
最近做了一个小程序,用到了SQLite,后台用Python写的分析程序,将数据插入(更新)到SQLite数据库中,Delphi的程序周期显示数据库的内容。Delphi访问SQLite采用的Aducom组件。Python插入的数据编码都是采用的UTF-8,而Delphi的DBGrid、cxGrid控件显示的却是乱码,主要是因为Delphi7不支持Unicode造成的,因此要想办法让他支持。
尝试了多种方法,包括使用据说支持Unicode的TMS Unicode Component、SUIPack等,都不好使。最后还是用了简单的方法,在数据集组件的需要显示的字段的OnGetText事件,在事件处理中,对数据进行Unicode到GB的转换。

procedure unicode2gb(const unicodestr:string; var GbStr:String);
var SourceLength:integer;
    DoneLength:integer;
    AscNo:integer;
    Byte1,Byte2,Byte3:integer;
begin
GbStr:='';
if Trim(unicodestr)='' then exit;

SourceLength:=Length(UnicodeStr);
DoneLength:=1;
repeat
    AscNo:=ord(UnicodeStr[DoneLength]);
    case (AscNo and $E0) of
    $E0:begin
         Byte1:=(AscNo and $0f) shl 12;
         Inc(DoneLength);
         if DoneLength>SourceLength then break;
         AscNo:=ord(UnicodeStr[DoneLength]);
         Byte2:=(AscNo and $3f) shl 6;
         Inc(DoneLength);
             if DoneLength>SourceLength then break;
         AscNo:=ord(UnicodeStr[DoneLength]);
         Byte3:=AscNo and $3f;
        end;
    $C0:begin
         Byte1:=(AscNo and $1f) shl 6;
         Inc(DoneLength);
         if DoneLength>SourceLength then break;
         AscNo:=ord(UnicodeStr[DoneLength]);
         Byte2:=(AscNo and $3f);
         Byte3:=0;
        end;
    0..$bf:begin
         Byte1:=AscNo;
         Byte2:=0;
         Byte3:=0;
        end;
    end;//case;
     GbStr:=GBStr+widechar(Byte1+Byte2+Byte3);
     Inc(DoneLength);
     if DoneLength>SourceLength then break;
until DoneLength>=SourceLength;
end;
另外,在用cxGrid进行显示的时候,要根据字段的值进行颜色的设置,这个可以在TableView的Styles的OnGetContentStyle事件中进行处理,如下所示:
procedure TFormMain.cxGrid1DBTableView1StylesGetContentStyle(
    Sender: TcxCustomGridTableView; ARecord: TcxCustomGridRecord;
    AItem: TcxCustomGridTableItem; out AStyle: TcxStyle);
var
    attention : integer;
begin

    attention := ARecord.Values[5];

    if attention > 0 then
    begin
        AStyle := styleAttention;
        exit;
    end;

    if ARecord.Values[4] = 0 then
    begin
        AStyle := styleRed;
    end
    else
    begin
        AStyle := styleDefault;
    end;
end;
其中styleAttention、styleDefault等是放在cxStyleRepository1中的设定好的各种Style。


本文出自 “景元的技术专栏” 博客,请务必保留此出处http://kernel.blog.51cto.com/920538/197288

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
用JavaScript实现汉字与Unicode的相互转换
Qt QString 中文 char* UTF-8 QByteArray QTextCodec unicode gb2312 GBK 乱码与转码问题
delphi中的unicode转换
c中实现utf8和gbk的互转
unicode编码转换gb2312编码
java中的char类型
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服