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

2022/9/5 2:22:58

本文主要是介绍02-mORMot框架样例学习-02 - Embedded SQLite3 ORM(SQLite3 数据库),对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

 
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-mORMot框架样例学习-02 - Embedded SQLite3 ORM(SQLite3 数据库)的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程