Sqlite under FireDAC [8] - the custom function.

Sqlite does not have this feature, FireDAC by TFDSQLiteFunction adds this feature; although by some statement or through SQL views can also achieve a similar effect, but function more flexible.

This first example built a results table, and then by two TFDSQLiteFunction to achieve a "score" calculated "average" of.



You can copy the contents of the text box below, and then to a form posted to quickly direct the Form Designer:



Code:


 

unit Unit1;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics, Vcl.Controls, Vcl.Forms,
  Vcl.Dialogs, FireDAC.Stan.Intf, FireDAC.Stan.Option, FireDAC.Stan.Error, FireDAC.UI.Intf, FireDAC.Phys.Intf, FireDAC.Stan.Def,
  FireDAC.Stan.Pool, FireDAC.Stan.Async, FireDAC.Phys, FireDAC.Stan.ExprFuncs, FireDAC.VCLUI.Wait, FireDAC.Stan.Param, FireDAC.DatS,
  FireDAC.DApt.Intf, FireDAC.DApt, Vcl.Grids, Vcl.DBGrids, Data.DB, FireDAC.Comp.DataSet, FireDAC.Comp.Client, FireDAC.Comp.UI,
  FireDAC.Phys.SQLite, Vcl.StdCtrls, FireDAC.Phys.SQLiteWrapper;

type
  TForm1 = class(TForm)
    FDConnection1: TFDConnection;
    FDPhysSQLiteDriverLink1: TFDPhysSQLiteDriverLink;
    FDGUIxWaitCursor1: TFDGUIxWaitCursor;
    FDQuery1: TFDQuery;
    DataSource1: TDataSource;
    DBGrid1: TDBGrid;
    Button1: TButton;
    Button2: TButton;
    FDSQLiteFunction1: TFDSQLiteFunction;
    FDSQLiteFunction2: TFDSQLiteFunction;
    procedure FormCreate(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure FDSQLiteFunction1Calculate(AFunc: TSQLiteFunctionInstance; AInputs: TSQLiteInputs; AOutput: TSQLiteOutput; var AUserData: TObject);
    procedureFDSQLiteFunction2Calculate (Afunc: TSQLiteFunctionInstance; AInputs: TSQLiteInputs; AOutput: TSQLiteOutput; var AUserData: TObject);
   Private
     {Private Declarations} 
  public
     {Public Declarations} 
  End ; var 
  the Form1: TForm1; Implementation {$ R & lt *}. Dfm Procedure TForm1.FormCreate ( SENDER: TObject);
 const 
  strTable = 'the CREATE tABLE MyTable (name string (10), language Integer, mathematics Integer, English Integer)' ; // build a table of student achievement the begin {establish a results table, and insert the test data} 
  FDConnection1 .Params.Add ( 'the SQLite DriverID =' );









   
  FDConnection1.ExecSQL (strTable);
  FDQuery1.ExecSQL ( 'the INSERT the INTO the MyTable (name, language, mathematics, English) the VALUES (:. 1,: 2,:. 3,:. 4)' , [ 'John Doe' , 66 , 77 , 88 ]); 
  FDQuery1.ExecSQL ( 'the INSERT the INTO the MyTable (name, language, mathematics, English) the VALUES (:. 1,: 2,:. 3,:. 4)' , [ 'John Doe' , 77 , 88 , 99 ]); 
  FDQuery1.Open ( 'the SELECT the MyTable the FROM * ' ); {TFDSQLiteFunction are set to two parameters} 
  FDSQLiteFunction1.DriverLink: = FDPhysSQLiteDriverLink1; 
  FDSQLiteFunction1.FunctionName: = ' MyFun1 ' ; // function name 
  FDSQLiteFunction1.ArgumentsCount:= 3

   ; The number of parameters // function 
  // FDSQLiteFunction1.OnCalculate: = FDSQLiteFunction1Calculate; // establish OnCalculate event designed more convenient 
  FDSQLiteFunction1.Active: = True; 

  FDSQLiteFunction2.DriverLink: = FDPhysSQLiteDriverLink1; 
  FDSQLiteFunction2.FunctionName: = 'MyFun2' ; 
  FDSQLiteFunction2.ArgumentsCount: = 3 ;
   // FDSQLiteFunction2.OnCalculate: = FDSQLiteFunction2Calculate; // establish OnCalculate event designed more convenient 
  FDSQLiteFunction2.Active: = True; End ; {} call MyFun1 Procedure TForm1.Button1Click (Sender: TObject);
 the begin 
  FDQuery1.Open ( 'the SELECT name, MyFun1 (language, mathematics, English) AS score MyTable the FROM' );
 End



 ;

{} Call MyFun2 
Procedure TForm1.Button2Click (Sender: TObject);
 the begin 
  FDQuery1.Open ( 'the SELECT name, MyFun2 (language, mathematics, English) AS average of the MyTable the FROM' );
 End ; defined function {the MyFun1: Total count } Procedure TForm1.FDSQLiteFunction1Calculate (Afunc: TSQLiteFunctionInstance; AInputs: TSQLiteInputs; AOutput: TSQLiteOutput; var AUserData: TObject);
 the begin 
  AOutput.AsInteger: AInputs = [ 0 ] + .AsInteger AInputs [ . 1 ] + .AsInteger AInputs [ 2 ] .AsInteger ;
 End ; {MyFun2 defined function: the average count} Procedure





TForm1.FDSQLiteFunction2Calculate (afunc: TSQLiteFunctionInstance; AInputs: TSQLiteInputs; AOutput: TSQLiteOutput; var Auser Data: TObject);
begin 
  AOutput.AsFloat: = (AInputs [ 0 ] + .AsInteger AInputs [ 1 ] + .AsInteger AInputs [ 2 ] .AsInteger) / 3 ;
end ; end .




Renderings:

Guess you like

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