Sqlite 本身没有这个功能, FireDAC 通过 TFDSQLiteFunction 增加了该功能; 尽管通过某些 SQL 语句或通过视图也可以达到类似效果, 但函数会更灵活些.

本例先建了一个成绩表, 然后通过两个 TFDSQLiteFunction 实现了 "总分" 与 "平均分" 的计算.


你可以复制下面文本框中的内容, 然后直接往窗体上贴, 以快速完成窗体设计:

object DBGrid1: TDBGrid
Left = 8
Top = 88
Width = 321
Height = 89
DataSource = DataSource1
TabOrder = 0
TitleFont.Charset = DEFAULT_CHARSET
TitleFont.Color = clWindowText
TitleFont.Height = -11
TitleFont.Name = 'Tahoma'
TitleFont.Style = []
end
object Button1: TButton
Left = 382
Top = 88
Width = 75
Height = 25
Caption = 'Button1'
TabOrder = 1
OnClick = Button1Click
end
object Button2: TButton
Left = 382
Top = 129
Width = 75
Height = 25
Caption = 'Button2'
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 = 344
Top = 24
end
object DataSource1: TDataSource
DataSet = FDQuery1
Left = 420
Top = 24
end
object FDSQLiteFunction1: TFDSQLiteFunction
DriverLink = FDPhysSQLiteDriverLink1
Active = True
FunctionName = 'MyFun1'
ArgumentsCount = 3
OnCalculate = FDSQLiteFunction1Calculate
Left = 48
Top = 200
end
object FDSQLiteFunction2: TFDSQLiteFunction
DriverLink = FDPhysSQLiteDriverLink1
Active = True
FunctionName = 'MyFun2'
ArgumentsCount = 3
OnCalculate = FDSQLiteFunction2Calculate
Left = 152
Top = 200
end


代码:


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.

效果图:


FireDAC 下的 Sqlite [8] - 自定义函数的更多相关文章

  1. Android SQLite 加入自定义函数

    SQLite Database 自定义函数实现: //Here's how to create a function that finds the first character of a strin ...

  2. FireDAC 下的 Sqlite [3] - 获取数据库的基本信息

    在空白窗体上添加: TFDConnection, TFDPhysSQLiteDriverLink, TFDGUIxWaitCursor, TMemo procedure TForm1.FormCrea ...

  3. Mybatis下配置调用Oracle自定义函数返回的游标结果集

    在ibatis和Mybatis对存储过程和函数函数的调用的配置Xml是不一样的,以下是针对Mybatis 3.2的环境进行操作的. 第一步配置Mapper的xml内容 <mapper names ...

  4. FireDAC 下的 Sqlite [6] - 加密

    主要就是设置 TFDConnection 的两个链接参数: Password, NewPassword, 非常简单. const dbPath = 'C:\Temp\SQLiteTest.sdb'; ...

  5. FireDAC 下的 Sqlite [9] - 关于排序

    SQLite 内部是按二进制排序, 可以支持 ANSI; FrieDAC 通过 TFDSQLiteCollation 支持了 Unicode 排序, 并可通过其 OnCompare 事件自定义排序. ...

  6. FireDAC 下的 Sqlite [4] - 创建数据库

    建立数据库的代码: {建立内存数据库的一般代码:} begin FDConnection1.DriverName := 'SQLite'; //同 FDConnection1.Params.Add(' ...

  7. FireDAC 下的 Sqlite [10] - 使用 R-Tree 搜索

    R-Tree 主要用于三维空间的搜索, 据说这种搜索算法非常之快, 哪怕百万条记录也是眨眼间的事! SQLite 支持 1-5 维, FireDAC 也提供了 TFDSQLiteRTree 控件以方便 ...

  8. FireDAC 下的 Sqlite [2] - 第一个例子

    为了方便测试, 我把官方提供的 C:\Users\Public\Documents\Embarcadero\Studio\14.0\Samples\data\FDDemo.sdb 复制了一份到 C:\ ...

  9. FireDAC 下的 Sqlite [1] - 前言

    很长时间没静下心来写博客了, 现在回来, 是 Delphi 不断地进步让我感动.振奋. Delphi XE5 并入了 FireDAC, 第一印象非常好, 恐怕 dbExpress 等等都要靠边站了. ...

随机推荐

  1. JAVA多线程之线程的挂起与恢复(suspend方法与resume方法)

    一,介绍 本文讨论JAVA多线程中,使用 thread.suspend()方法暂停线程,使用 thread.resume()恢复暂停的线程 的特点. 先介绍二个关于线程的基本知识: ①线程的执行体是r ...

  2. CSS那些事!这个篇幅是我特意开的,不是因为帮助小菜之类的,而是在多人的团队配合中各种命名冲突的规范让人蛋疼

    CSS那些事!这个篇幅是我特意开的,不是因为帮助小菜之类的,而是在多人的团队配合中各种命名冲突的规范让人蛋疼. css这个东西只要不是新的离谱都会写,但是每个人的命名风格,方法,都不同 有人喜欢驼峰, ...

  3. C# 常用控件属性及方法介绍

      C#常用控件属性及方法介绍                                               目录 1.窗体(Form) 2.Label (标签)控件 3.TextBox ...

  4. camera驱动框架分析(上)

    前言 camera驱动框架涉及到的知识点比较多,特别是camera本身的接口就有很多,有些是直接连接到soc的camif口上的,有些是通过usb接口导出的,如usb camera.我这里主要讨论前者, ...

  5. mysql5.7 主从复制的正常切换【转】

    目前环境如下: master server IP:172.17.61.131 slave server IP:172.17.61.132 mysql version: mysql-5.7.21-lin ...

  6. 可视化并理解CNN

    参考:https://zhuanlan.zhihu.com/p/24833574 学习论文[1311.2901] Visualizing and Understanding Convolutional ...

  7. C++ Primer读书笔记(3)

    vector: 本科时学C++的时候没学过vector,正好补一下. 第一个要注意的地方是要正确区分列表初始值还是元素数量. 第二点是不能使用范围for向vector对象添加元素,范围for语句体内不 ...

  8. LeetCode(21):合并两个有序链表

    Easy! 题目描述: 将两个有序链表合并为一个新的有序链表并返回.新链表是通过拼接给定的两个链表的所有节点组成的. 示例: 输入:1->2->4, 1->3->4 输出:1- ...

  9. 【AtCoder】ARC101题解

    C - Candles 题解 点燃的一定是连续的一段,枚举左端点即可 代码 #include <bits/stdc++.h> #define enter putchar('\n') #de ...

  10. SQL数据库数据类型详解

    数据类型 类型 描 述 bit 整型 bit 数据类型是整型,其值只能是0.1或空值.这种数据类型用于存储只有两种可能值的数据,如Yes 或No.True 或Fa lse .On 或Off int 整 ...