Sqlite under FireDAC [9] - on sorting,

Internal SQLite is based on a binary sort, supports ANSI; FrieDAC by TFDSQLiteCollation support the Unicode sorting, and through its OnCompare event custom sort.

The following example, tested two different sort.



The code below can be directly attached to the blank form, in order to quickly form design:



Code:


procedure TForm1.FormCreate (Sender: TObject);
was
  i: Integer;
  LCode: Integer;
the begin
   {} to set the parameters FDSQLiteCollation1
  FDSQLiteCollation1.DriverLink := FDPhysSQLiteDriverLink1;
// FDSQLiteCollation1.CollationKind: = scCompareString; // this is the default (Unicode case-insensitive, at Win calling WinAPI.CompareString); other options require custom collation 
  FDSQLiteCollation1.LocaleName: = 'the CN-ZH' ;
  FDSQLiteCollation1.Flags := [sfIgnoreCase];
  FDSQLiteCollation1.CollationName: = 'MyCollation' ;      all calls // This will all be dependent on the name
  FDSQLiteCollation1.Active := True;

  FDConnection1.Params.Add ( 'the SQLite DriverID =' );
 // FDConnection1.Params.Add ( 'The OpenMode CreateUTF8 ='); // this is the default, optionally CreateUTF16 (Unicode)

  {Create a test table, three fields str (Chinese characters), code (Unicode values corresponding to the characters), ID (order of addition)} 
  FDConnection1.ExecSQL ( 'the MyTable the CREATE TABLE (STR String (10), code Integer, Integer ID)' );
 // when used in a design table; // FDConnection1.ExecSQL ( 'CREATE tABLE MyTable (str string (10) COLLATE MyCollation, code integer, id integer)')

  Adding test data {} 
  for I: = 0  to  99  do 
  the begin 
    LCODE: the Random = ( $ 9FA5 - $ 4E00 );
    FDConnection1.ExecSQL('INSERT INTO MyTable(str, code, id) VALUES(:1, :2, :3)', [WideChar($4E00 + LCode), LCode, i+1]);
  end;

  FDQuery1.Open ( 'the FROM the MyTable the SELECT *' ); // no sorting 
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;


Test renderings:

Guess you like

Origin www.cnblogs.com/yjhb/p/11804243.html