首页 > 数据库 >02-mORMot框架样例学习-02 - Embedded SQLite3 ORM(SQLite3 数据库)

02-mORMot框架样例学习-02 - Embedded SQLite3 ORM(SQLite3 数据库)

时间:2022-09-04 18:59:00浏览次数:122  
标签:02 end Sender Embedded TForm1 TObject SQLite3 Rec procedure

 

program Project01;
uses
  {$I SynDprUses.inc} // use FastMM4 on older Delphi, or set FPC threads
  Forms,   SysUtils,       SynCommons,     mORMot,
  Unit1 in 'Unit1.pas' {Form1}
  ,mORMotSQLite3, SynSQLite3Static
    ;
{$R *.res}
begin
  Application.Initialize;
  Application.CreateForm(TForm1, Form1);
  Form1.Caption := ' Sample 01 - In Memory ORM';
  Form1.aSQLRest := TSQLRestServerDB.Create(Form1.aSQLModel,
    ChangeFileExt(ExeVersion.ProgramFileName,'.db3'));//系统会 在起始时加载Projet01.db3
  TSQLRestServerDB(Form1.aSQLRest).CreateMissingTables;
  Application.Run;
end.

 

unit Unit1;

interface

uses
  {$ifdef MSWINDOWS}
  Windows,
  Messages,
  Graphics,
  {$endif}
  Classes, SysUtils, Forms, Controls, Dialogs, StdCtrls,
  SynCommons,
  SynTable,
  mORMot;

type

  TSQLSampleRecord = class(TSQLRecord)
  private
    //系统默认有ID列
    fName: RawUTF8;
    fInfo: RawUTF8;
  published

    property Name: RawUTF8 read fName write fName; //Name列
    property Info: RawUTF8 read fInfo write fInfo; //Info列
  end;

  { TForm1 }
  TForm1 = class(TForm)
    AddButton: TButton;
    Label1: TLabel;
    Label2: TLabel;
    QuitButton: TButton;
    FindButton: TButton;
    QuestionMemo: TMemo;
    NameEdit: TEdit;
    procedure AddButtonClick(Sender: TObject);
    procedure FindButtonClick(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
    procedure QuitButtonClick(Sender: TObject);
  private
    { private declarations }
  public
    { public declarations }
    aSQLRest: TSQLRest;
    aSQLModel: TSQLModel;
  end;
var
  Form1: TForm1;
implementation
{$ifdef FPC} {$R *.lfm}  {$else}   {$R *.dfm}   {$endif}
procedure TForm1.FormCreate(Sender: TObject);
begin
aSQLModel :=TSQLModel.Create([TSQLSampleRecord]);
end;

procedure TForm1.AddButtonClick(Sender: TObject);
var Rec: TSQLSampleRecord;
begin
  Rec := TSQLSampleRecord.Create;
  try
    Rec.Name := StringToUTF8(NameEdit.Text);
    Rec.Info := StringToUTF8(QuestionMemo.Text);
    if aSQLRest.Add(Rec,true)=0 then
      ShowMessage('Error adding the data') else begin
      NameEdit.Text := '';
      QuestionMemo.Text := '';
      NameEdit.SetFocus;
    end;
  finally
    Rec.Free;
  end;
end;

procedure TForm1.FindButtonClick(Sender: TObject);
var Rec: TSQLSampleRecord;
begin
  Rec := TSQLSampleRecord.Create(aSQLRest,'Name=?',[StringToUTF8(NameEdit.Text)]);
  try
    if Rec.ID=0 then
      QuestionMemo.Text := 'Not found' else      QuestionMemo.Text :=stringreplace(UTF8ToString(Rec.Info), 'Z~!~', #13#10, [rfReplaceAll]) ;
  finally
    Rec.Free;
  end;
end;

procedure TForm1.FormDestroy(Sender: TObject);
begin
  aSQLRest.Free;   aSQLModel.Free;
end;

procedure TForm1.QuitButtonClick(Sender: TObject);
begin
  Close;
end;

end.

 

标签:02,end,Sender,Embedded,TForm1,TObject,SQLite3,Rec,procedure
From: https://www.cnblogs.com/tulater/p/16655704.html

相关文章

  • 【2022-08-30】哺乳
    20:00习惯的枷锁,开始的时候轻得难以察觉,到后来却重得无法摆脱。                                ......
  • VS2022编译太慢
    一个C++的helloworld,在vs里硬是10秒才能编译启动。不知道大家有没有遇到。禁用符号加载还是很慢。甚至换成code::block来编译运行也很慢。解决方法是把编译出的exe程序或......
  • WEB自动化-02-Cypress 安装
    2Cypress安装2.1安装要求2.1.1系统要求  在操作系统满足以下要求时,才能进行安装,具体要求如下所示:MacOS10.9+(仅支持64位版本)LinuxUbuntu12.04+、Fedora21......
  • 《Unix/Linux系统编程》第1,2章学习笔记 20201209戴骏
    一.知识点归纳第一章引言1.本书目标编程背景知识详细介绍了程序开发步骤,包括汇报器、编译器、链接器等。动态数据结构的应用字符串标记化、搜索树节点、插入和......
  • 2022java-web一条龙工具安装
    @目录java安装java-jdk安装java环境变量配置java-eclipse工具安装Java-idea工具安装MySQL安装navicat安装tomcat安装maven安装配置本地仓库配置镜像配置jdk备忘~安装常见......
  • "蔚来杯"2022牛客暑期多校训练营7
    CConstructiveProblemsNeverDie题意:给你一个数组A,你需要构造一个排列P,使得P[i]≠A[i]分析:考虑构造不出来的情况如果所有A[i]都相同一定不成立先构造P[i]=i......
  • 2022java-web一条龙工具安装
    @目录java安装java-jdk安装java环境变量配置java-eclipse工具安装Java-idea工具安装MySQL安装navicat安装tomcat安装maven安装配置本地仓库配置镜像配置jdk备忘~安装常见......
  • 【2022-09-02】Django框架(四)
    Django框架(四)Django框架之伪静态概念静态文件:数据是写死,永远不会修改伪静态:将一个动态页面伪装成静态页面#为什么要伪装?伪装的目的在于增大本网站的seo查询力......
  • docker安装 jenkins 20220904
    ###https://www.cnblogs.com/fuzongle/p/12834080.html1、#####下载Jenkins镜像dockerpulljenkins/jenkins2、宿主机=》创建jenkins的目录####/var/jenkins_mount......
  • 2022.9.4 模拟赛
    ForNOIP.谜之阶乘题意:问\(n\)能够被多少组\((a,b)\)表示,满足\(n=\dfrac{a!}{b!}\)。\(n\leq10^{18}\),多组询问。首先猜测\(a-b\)并不大,然后就枚举这个差值......