Show Unicode for SQLite in Delphi

Source: Internet
Author: User

A small program was developed recently. SQLite was used, and the analysis program written in Python in the background was used to insert (update) data into the SQLite database. The Delphi program cycle showed the database content. Delphi accesses the aducom component used by SQLite. Python inserted data encoding is the use of the UTF-8, And the Delphi DBGrid, cxgrid control display is garbled, mainly because Delphi7 does not support Unicode caused, so we want to make it support.

I tried a variety of methods, including the use of TMS Unicode component and suipack that are said to support Unicode. Finally, a simple method is used. In the ongettext event of the fields to be displayed in the dataset component, Unicode to GB conversion is performed on the data during event processing.

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;

In addition, when you use cxgrid for display, you need to set the color based on the field value. This can be handled in the ongetcontentstyle event of styles in tableview, as shown below:

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 and styledefault are various styles set in cxstylerepository1.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.