Sqlite 本身沒有這個功能, FireDAC 通過 TFDSQLiteFunction 增加了該功能; 盡管通過某些 SQL 語句或通過視圖也可以達到類似效果, 但函數會更靈活些. 本例先建了一個成績表, 然後通過兩個 TFDSQLiteFunction 實現了 "總分" 與 "平均分" 的計算.
你可以復制下面文本框中的內容, 然後直接往窗體上貼, 以快速完成窗體設計:
代碼:
unit Unit1; interfaceuses 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);procedure FDSQLiteFunction2Calculate(AFunc: TSQLiteFunctionInstance; AInputs: TSQLiteInputs; AOutput: TSQLiteOutput; var AUserData: TObject); private{ Private declarations } public{ Public declarations } end; var Form1: TForm1; implementation{$R *.dfm}procedure TForm1.FormCreate(Sender: TObject); const strTable = 'CREATE TABLE MyTable(姓名 string(10), 語文 Integer, 數學 Integer, 英語 Integer)'; // 建一個學生成績表begin { 建立一個成績表, 並插入測試數據 } FDConnection1.Params.Add('DriverID=SQLite'); FDConnection1.ExecSQL(strTable); FDQuery1.ExecSQL('INSERT INTO MyTable(姓名, 語文, 數學, 英語) VALUES(:1, :2, :3, :4)', ['張三', 66, 77, 88]); FDQuery1.ExecSQL('INSERT INTO MyTable(姓名, 語文, 數學, 英語) VALUES(:1, :2, :3, :4)', ['李四', 77, 88, 99]); FDQuery1.Open('SELECT * FROM MyTable'); { 分別給兩個 TFDSQLiteFunction 設定參數 } FDSQLiteFunction1.DriverLink := FDPhysSQLiteDriverLink1; FDSQLiteFunction1.FunctionName := 'MyFun1'; // 函數名 FDSQLiteFunction1.ArgumentsCount := 3; // 函數的參數個數 // FDSQLiteFunction1.OnCalculate := FDSQLiteFunction1Calculate; //在設計時建立 OnCalculate 事件更方便 FDSQLiteFunction1.Active := True; FDSQLiteFunction2.DriverLink := FDPhysSQLiteDriverLink1; FDSQLiteFunction2.FunctionName := 'MyFun2'; FDSQLiteFunction2.ArgumentsCount := 3; // FDSQLiteFunction2.OnCalculate := FDSQLiteFunction2Calculate; //在設計時建立 OnCalculate 事件更方便 FDSQLiteFunction2.Active := True; end; { 調用 MyFun1 }procedure TForm1.Button1Click(Sender: TObject); begin FDQuery1.Open('SELECT 姓名, MyFun1(語文, 數學, 英語) AS 總分 FROM MyTable'); end; { 調用 MyFun2 }procedure TForm1.Button2Click(Sender: TObject); begin FDQuery1.Open('SELECT 姓名, MyFun2(語文, 數學, 英語) AS 平均分 FROM MyTable'); end; { 函數 MyFun1 的定義: 算總分 }procedure TForm1.FDSQLiteFunction1Calculate(AFunc: TSQLiteFunctionInstance; AInputs: TSQLiteInputs; AOutput: TSQLiteOutput; var AUserData: TObject); begin AOutput.AsInteger := AInputs[0].AsInteger + AInputs[1].AsInteger + AInputs[2].AsInteger; end; { 函數 MyFun2 的定義: 算平均分 }procedure TForm1.FDSQLiteFunction2Calculate(AFunc: TSQLiteFunctionInstance; AInputs: TSQLiteInputs; AOutput: TSQLiteOutput; var AUserData: TObject); begin AOutput.AsFloat := (AInputs[0].AsInteger + AInputs[1].AsInteger + AInputs[2].AsInteger) / 3; end; end.
效果圖:
Author:cnblogs 萬一