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

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,end,Sender,Embedded,TForm1,TObject,SQLite3,Rec,procedure
来源: https://www.cnblogs.com/tulater/p/16655704.html