Sqlite 本身没有这个功能, FireDAC 通过 TFDSQLiteFunction 增加了该功能; 尽管通过某些 SQL 语句或通过视图也可以达到类似效果, 但函数会更灵活些.
本例先建了一个成绩表, 然后通过两个 TFDSQLiteFunction 实现了 "总分" 与 "平均分" 的计算.
你可以复制下面文本框中的内容, 然后直接往窗体上贴, 以快速完成窗体设计:
代码:
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); 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.
效果图:

分类:
数据库编程
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具
2011-05-22 给 "水桶" 同学解释一下 "流" 类中的 Seek() 方法
2009-05-22 时间与相关类型(3): TFileTime、TSystemTime 及 DOS 时间
2009-05-22 时间与相关类型(2) - TDate、TTime、TTimeStamp
2009-05-22 时间与相关类型(1) - TDateTime 与 Double
2008-05-22 WinAPI: InvertRect - 翻转矩形中像素的颜色
2008-05-22 WinAPI: OffsetRect - 移动矩形
2008-05-22 WinAPI: IntersectRect、SubtractRect、UnionRect - 用两个矩形合成一个新的矩形