FireDAC 下的 Sqlite [8] - 自定义函数

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] - 自定义函数

时间: 2024-08-03 04:50:33

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

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

R-Tree 主要用于三维空间的搜索, 据说这种搜索算法非常之快, 哪怕百万条记录也是眨眼间的事! SQLite 支持 1-5 维, FireDAC 也提供了 TFDSQLiteRTree 控件以方便定义回调函数. 为了简单, 我用二维表进行了成功的测试. 建立 R-Tree 表(索引)时需要使用特定语法, 譬如: FDConnection1.ExecSQL('CREATE VIRTUAL TABLE MyRTreeTable USING rtree(Id, minX, maxX, minY,

FireDAC 下的 Sqlite [1] - 前言

很长时间没静下心来写博客了, 现在回来, 是 Delphi 不断地进步让我感动.振奋.Delphi XE5 并入了 FireDAC, 第一印象非常好, 恐怕 dbExpress 等等都要靠边站了.让我最高兴地是 FireDAC 对 Sqlite 的支持!优秀的 Sqlite 早就有很多 Delphi 的包装(http://www.sqlite.org/cvstrac/wiki?p=SqliteWrappers, 从 https://code.google.com/ 等还能搜到更多).有静态引用

FireDAC 下的 Sqlite [11] - 关于批量提交 SQL 命令的测试

可把下面代码直接贴在空白窗体上, 以快速完成窗体设计:object DBGrid1: TDBGrid Left = 0 Top = 0 Width = 265 Height = 338 Align = alLeft DataSource = DataSource1 TabOrder = 0 TitleFont.Charset = DEFAULT_CHARSET TitleFont.Color = clWindowText TitleFont.Height = -11 TitleFont.Name

FireDAC 下的 Sqlite [5] - 数据的插入、更新、删除

先在空白窗体上添加: TFDConnection.TFDPhysSQLiteDriverLink.TFDGUIxWaitCursor.TFDQuery.TDataSource.TDBGrid(并在设计时关联好).你也可以复制下面文本框中的内容, 然后直接往窗体上贴, 以快速完成以上的添加过程:object DBGrid1: TDBGrid Left = 16 Top = 88 Width = 361 Height = 329 DataSource = DataSource1 TabOrder =

Android SQLite 加入自定义函数

SQLite Database 自定义函数实现: //Here's how to create a function that finds the first character of a string. static void firstchar(sqlite3_context *context, int argc, sqlite3_value **argv) { if (argc == 1) { char *text = sqlite3_value_text(argv[0]); if (te

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

今天有幸被召回母校给即将毕业的学弟学妹们讲我这两年的工作史,看了下母校没啥特别的变化,就是寝室都安了空调,学妹们都非常漂亮而已..好了不扯蛋了,说下今天的主题吧.这些天我在深度定制语法高亮功能的同时发现了博客园提供的一些有意思的函数,甚至有几个博客园都没用到,我也不知道怎么才能触发那些功能..打开这个js就可以看到很多好用的东西了,虽然写的不怎么样,但是至少有这些功能. ps: 推荐安装一个代码格式化的插件,否则一坨看着蛋疼.比如第一个就是 log,方便调试. http://www.qidian

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

position:static(静态定位) 当position属性定义为static时,可以将元素定义为静态位置,所谓静态位置就是各个元素在HTML文档流中应有的位置 podisition定位问题.所以当没有定义position属性时,并不说明该元素没有自己的位置,它会遵循默认显示为静态位置,在静态定位状态下无法通过坐标值(top,left,right,bottom)来改变它的位置. position:absolute(绝对定位) 当position属性定义为absolute时,元素会脱离文档流

FireDAC 下的 Sqlite [7] - 备份、优化、事务(Transaction)/

用 TFDSQLiteBackup 控件, 两三行代码即可完成 Sqlite 数据库的备份. procedure TForm1.Button1Click(Sender: TObject); begin   {先初始化目标}   FDConnection1.DriverName := 'SQLite';   FDConnection1.Params.Add('Database=C:\Temp\FDDemo_Back.sdb'); //如果不指定这个路径, 就是备份到内存   FDConnectio

FireDAC 下的 Sqlite [12] - 备忘录(草草结束这个话题了).

该话题的继续延伸主要就是 SQL 的语法了, 草草收场的原因是现在的脑筋已经进入了 IntraWeb 的世界. 相关备忘会随时补充在下面: //连接多个数据库的参考代码: FDConnection1.ExecSQL('ATTACH ''c:\hr.sdb'' AS hr'); FDConnection1.ExecSQL('ATTACH ''c:\cust.sdb'' AS cust'); FDQuery1.Open('select * from "Orders" o ' +   'le