delphisqlite:Delphi中让sqlite显示Unicode

最近做了个小用到了SQLite后台用Python写分析将数据插入(更新)到SQLite数据库中Delphi周期显示数据库内容Delphi访问SQLite采用Aducom组件Python插入数据编码都是采用UTF-8而DelphiDBGrid、cxGridControl控件显示却是乱码主要是Delphi7不支持Unicode造成因此要想办法让他支持

尝试了多种思路方法包括使用据说支持UnicodeTMS Unicode Component、SUIPack等都不好使最后还是用了简单思路方法在数据集组件需要显示字段OnGetText事件在事件处理中对数据进行Unicode到GB转换

view plaincopy to clipboardpr?procedure unicode2gb(const unicodestr:; var GbStr:String); var SourceLength:eger; DoneLength:eger; AscNo:eger; Byte1,Byte2,Byte3:eger; begin GbStr:=''; Trim(unicodestr)='' then exit; SourceLength:=Length(UnicodeStr); DoneLength:=1; repeat AscNo:=ord(UnicodeStr[DoneLength]); (AscNo and $E0) of $E0:begin Byte1:=(AscNo and $0f) shl 12; Inc(DoneLength); DoneLength>SourceLength then ; AscNo:=ord(UnicodeStr[DoneLength]); Byte2:=(AscNo and $3f) shl 6; Inc(DoneLength); DoneLength>SourceLength then ; AscNo:=ord(UnicodeStr[DoneLength]); Byte3:=AscNo and $3f; end; $C0:begin Byte1:=(AscNo and $1f) shl 6; Inc(DoneLength); DoneLength>SourceLength then ; AscNo:=ord(UnicodeStr[DoneLength]); Byte2:=(AscNo and $3f); Byte3:=0; end; 0..$bf:begin Byte1:=AscNo; Byte2:=0; Byte3:=0; end; end;//; GbStr:=GBStr+widechar(Byte1+Byte2+Byte3); Inc(DoneLength); DoneLength>SourceLength then ; until DoneLength>=SourceLength; end; procedure unicode2gb(const unicodestr:; var GbStr:String);
var SourceLength:eger;
DoneLength:eger;
AscNo:eger;
Byte1,Byte2,Byte3:eger;
begin
GbStr:='';
Trim(unicodestr)='' then exit;

SourceLength:=Length(UnicodeStr);
DoneLength:=1;
repeat
AscNo:=ord(UnicodeStr[DoneLength]);
(AscNo and $E0) of
$E0:begin
Byte1:=(AscNo and $0f) shl 12;
Inc(DoneLength);
DoneLength>SourceLength then ;
AscNo:=ord(UnicodeStr[DoneLength]);
Byte2:=(AscNo and $3f) shl 6;
Inc(DoneLength);
DoneLength>SourceLength then ;
AscNo:=ord(UnicodeStr[DoneLength]);
Byte3:=AscNo and $3f;
end;
$C0:begin
Byte1:=(AscNo and $1f) shl 6;
Inc(DoneLength);
DoneLength>SourceLength then ;
AscNo:=ord(UnicodeStr[DoneLength]);
Byte2:=(AscNo and $3f);
Byte3:=0;
end;
0..$bf:begin
Byte1:=AscNo;
Byte2:=0;
Byte3:=0;
end;
end;//;
GbStr:=GBStr+widechar(Byte1+Byte2+Byte3);
Inc(DoneLength);
DoneLength>SourceLength then ;
until DoneLength>=SourceLength;
end; 另外在用cxGrid进行显示时候要根据字段值进行颜色设置这个可以在TableViewStylesOnGetContentStyle事件中进行处理如下所示:

view plaincopy to clipboardpr?procedure TFormMain.cxGrid1DBTableView1StylesGetContentStyle( Sender: TcxCustomGridTableView; ARecord: TcxCustomGridRecord; AItem: TcxCustomGridTableItem; out AStyle: TcxStyle); var attention : eger; begin attention := ARecord.Values[5]; attention > 0 then begin AStyle := styleAttention; exit; end; ARecord.Values[4] = 0 then begin AStyle := styleRed; end begin AStyle := styleDefault; end; end; procedure TFormMain.cxGrid1DBTableView1StylesGetContentStyle(
Sender: TcxCustomGridTableView; ARecord: TcxCustomGridRecord;
AItem: TcxCustomGridTableItem; out AStyle: TcxStyle);
var
attention : eger;
begin

attention := ARecord.Values[5];

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

ARecord.Values[4] = 0 then
begin
AStyle := styleRed;
end

begin
AStyle := styleDefault;
end;
end; 其中styleAttention、styleDefault等是放在cxStyleRepository1中设定好各种Style
Tags:  sqlite delphiunicode delphisqlite控件 delphisqlite

延伸阅读

最新评论

发表评论