Delphi FireDAC 下的 Sqlite(九) 關於排序

來源:互聯網
上載者:User

SQLite 內部是按二進位排序, 可以支援 ANSI; FrieDAC 通過 TFDSQLiteCollation 支援了 Unicode 排序, 並可通過其 OnCompare 事件自訂排序. 下面的例子, 測試了這兩種排序的不同.

可把下面代碼直接貼在空白表單上, 以快速完成表單設計: object DBGrid1: TDBGrid Left = 0 Top = 0 Width = 297 Height = 199 Align = alLeft DataSource = DataSource1 TabOrder = 0 TitleFont.Charset = DEFAULT_CHARSET TitleFont.Color = clWindowText TitleFont.Height = -11 TitleFont.Name = 'Tahoma' TitleFont.Style = [] end object Button1: TButton Left = 303 Top = 24 Width = 110 Height = 25 Caption = 'SQLite '#20869#32622#25490#24207 TabOrder = 1 OnClick = Button1Click end object Button2: TButton Left = 303 Top = 65 Width = 110 Height = 25 Caption = 'FireDAC '#40664#35748#25490#24207 TabOrder = 2 OnClick = Button2Click end object FDConnection1: TFDConnection Left = 34 Top = 24 end object FDPhysSQLiteDriverLink1: TFDPhysSQLiteDriverLink Left = 143 Top = 24 end object FDGUIxWaitCursor1: TFDGUIxWaitCursor Provider = 'Forms' Left = 260 Top = 24 end object FDQuery1: TFDQuery Connection = FDConnection1 Left = 32 Top = 88 end object DataSource1: TDataSource DataSet = FDQuery1 Left = 132 Top = 88 end object FDSQLiteCollation1: TFDSQLiteCollation DriverLink = FDPhysSQLiteDriverLink1 CollationName = 'MyCollation' LocaleName = 'zh-CN' Left = 240 Top = 120 end

更多精彩內容:http://www.bianceng.cnhttp://www.bianceng.cn/Programming/Delphi/

代碼:

procedure TForm1.FormCreate(Sender: TObject);var  i: Integer;  LCode: Integer;begin  {給 FDSQLiteCollation1 設定參數}  FDSQLiteCollation1.DriverLink := FDPhysSQLiteDriverLink1;//  FDSQLiteCollation1.CollationKind := scCompareString; //這是預設值(Unicode 不區分大小寫, 在 Win 下是調用 WinAPI.CompareString); 使用其他選項需要自訂定序  FDSQLiteCollation1.LocaleName := 'zh-CN';  FDSQLiteCollation1.Flags := [sfIgnoreCase];  FDSQLiteCollation1.CollationName := 'MyCollation';     //下面所有的調用全要依賴這個名稱  FDSQLiteCollation1.Active := True;  FDConnection1.Params.Add('DriverID=SQLite');//  FDConnection1.Params.Add('OpenMode=CreateUTF8'); //這是預設值, 可選 CreateUTF16(Unicode)  {建立測試表, 三個欄位 str(漢字), code(漢字對應的 Unicode 值), id(添加順序)}  FDConnection1.ExecSQL('CREATE TABLE MyTable(str string(10), code integer, id integer)');//  FDConnection1.ExecSQL('CREATE TABLE MyTable(str string(10) COLLATE MyCollation, code integer, id integer)'); //用在表設計時  {添加測試資料資料}  for i := 0to99do  beginLCode := Random($9FA5-$4E00);    FDConnection1.ExecSQL('INSERT INTO MyTable(str, code, id) VALUES(:1, :2, :3)', [WideChar($4E00 + LCode), LCode, i+1]);  end;  FDQuery1.Open('SELECT * FROM MyTable'); //無排序end;procedure TForm1.Button1Click(Sender: TObject);begin  FDQuery1.Open('SELECT * FROM MyTable ORDER BY str'); //SQLite 內建排序end;procedure TForm1.Button2Click(Sender: TObject);begin  FDQuery1.Open('SELECT * FROM MyTable ORDER BY str COLLATE MyCollation'); //FireDAC 預設排序end;

測試效果圖:

Author:cnblogs 萬一

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.