unit Umemtable;
interface
uses
System.SysUtils, System.Types, System.UITypes, System.Classes, System.Variants, FMX.Types, FMX.Controls, FMX.Forms, FMX.Graphics, FMX.Dialogs, System.Rtti, FMX.Layouts, FMX.Grid, FireDAC.Stan.Intf, FireDAC.Stan.Option, FireDAC.Stan.Param, FireDAC.Stan.Error, FireDAC.DatS, FireDAC.Phys.Intf, FireDAC.DApt.Intf, Data.DB, FireDAC.Comp.DataSet, FireDAC.Comp.Client, FMX.Controls.Presentation, FMX.StdCtrls, Data.Bind.EngExt, Fmx.Bind.DBEngExt, Fmx.Bind.Grid, System.Bindings.Outputs, Fmx.Bind.Editors, Data.Bind.Components, Data.Bind.Grid, Data.Bind.DBScope, FMX.Edit;type
TForm1 = class(TForm) Button1: TButton; FDMemTable1: TFDMemTable; DataSource1: TDataSource; Grid1: TGrid; bndsrcdb1: TBindSourceDB; bndngslst1: TBindingsList; lnkgrdtdtsrcBindSourceDB: TLinkGridToDataSource; Button2: TButton; Button3: TButton; Button4: TButton; Edit1: TEdit; Edit2: TEdit; Edit3: TEdit; lbl1: TLabel; Label1: TLabel; Label2: TLabel; Button5: TButton; Button6: TButton; Button7: TButton; Button8: TButton; Button9: TButton; Button10: TButton; Button11: TButton; Button12: TButton; Button13: TButton; Button14: TButton; Button15: TButton; Button16: TButton; Button17: TButton; procedure Button1Click(Sender: TObject); procedure Button2Click(Sender: TObject); procedure Button5Click(Sender: TObject); procedure Button6Click(Sender: TObject); procedure Button8Click(Sender: TObject); procedure Button7Click(Sender: TObject); procedure Button9Click(Sender: TObject); procedure Button10Click(Sender: TObject); procedure Button11Click(Sender: TObject); procedure Button12Click(Sender: TObject); procedure Button13Click(Sender: TObject); procedure Button14Click(Sender: TObject); procedure Button15Click(Sender: TObject); procedure Button16Click(Sender: TObject); procedure Button17Click(Sender: TObject); private{ Private declarations }
public { Public declarations } end;var
Form1: TForm1; Id: Integer;implementation
{$R *.fmx}
procedure TForm1.Button10Click(Sender: TObject);
begin //清空 FDMemTable1.EmptyView;end;procedure TForm1.Button11Click(Sender: TObject);
var FRow, FRow2: TFDDatSRow;begin FRow := FDMemTable1.Table.Rows.Add([1, 'Hello', '2015-09-15']); FRow2 := FDMemTable1.Table.NewRow(); FRow2.SetValues([2, 'Hello2', '2015-09-15']); FDMemTable1.Table.Rows.Add(FRow2); //以下方法2条记示增加成功 FDMemTable1.CachedUpdates; FDMemTable1.CommitUpdates;// FDMemTable1.ApplyUpdates;// FDMemTable1.CommitUpdates;end;procedure TForm1.Button12Click(Sender: TObject);
begin FDMemTable1.Filter := 'Value = ' + QuotedStr('Hello'); FDMemTable1.Filtered := True;end;procedure TForm1.Button13Click(Sender: TObject);
begin //FDMemTable1.Filter := 'Value = ''Hello'''; FDMemTable1.Filter := 'Value = ''Hel*'''; //foCaseInsensitive时,“Filter”属性中的条件字符串文字与字段值进行比较时,不区分大小写。 //foNoPartialCompare强制Delphi将星号(*)解释为原义字符而非通配符。默认情况下, //FilterOptions设置为空集 FDMemTable1.FilterOptions := [foCaseInsensitive]; FDMemTable1.Filtered := True;end;
procedure TForm1.Button14Click(Sender: TObject);
begin FDMemTable1.Filter := 'Value Like ''Hel%'''; FDMemTable1.Filtered := True;end;
procedure TForm1.Button15Click(Sender: TObject);
begin FDMemTable1.Filter := 'Id<10 and Value Like ''He%'''; FDMemTable1.Filtered := True;end;
procedure TForm1.Button16Click(Sender: TObject);
var FConnIntf: IFDPhysConnection; FCommIntf: IFDPhysCommand; oMechFilter: TFDDatSMechFilter; FDataView: TFDDatSView;beginFDataView := FDMemTable1.Table.DefaultView;
FDataView.Sort := 'Id ASC'; //FDataView.Mechanisms.AddSort('Id DESC;Value ASC'); //FDMemTable1.View.Mechanisms.AddSort('Id'); FDataView.Mechanisms.Clear; // FDMemTable1.View.Sort:=' Id DESC;' //FDMemTable1.View.SortingMechanism; // 4) filtering// oMechFilter := TFDDatSMechFilter.Create('id > 3');// FDMemTable1.View.Mechanisms.Add(oMechFilter);// FDefaultView.Mechanisms.Clear;
// // 5) too // FDMemTable1.View.Mechanisms.AddFilter('Id DESC');// FDefaultView.Mechanisms.Clear;// FDefaultView.Mechanisms.Clear;// // 5) too// FDefaultView.Mechanisms.AddFilter('id > 5');// FDefaultView.Mechanisms.Clear;end;
procedure TForm1.Button17Click(Sender: TObject);
var oMech: TFDDatSMechFilter; i: Integer; FDataView: TFDDatSView;begin FDataView := FDMemTable1.Table.DefaultView; // Locate row using locating mechanism oMech := TFDDatSMechFilter.Create('Id=5'); oMech.Locator := True; FDataView.Mechanisms.Add(oMech); FDataView.Locate(i, True, True); if i <> -1 then begin ShowMessage('找到 删除'); //FDataView.de; end else begin ShowMessage('NO'); end; FDataView.Mechanisms.Clear;end; procedure TForm1.Button1Click(Sender: TObject);var i: Integer;begin if FDMemTable1.Active then FDMemTable1.Close; with FDMemTable1.FieldDefs do begin Clear; Add('Id', ftInteger, 0, False); Add('Value', FtString, 20, False); Add('Time', ftDateTime, 0, False); end; with FDMemTable1.IndexDefs do begin Clear; // Add('Index1', 'Id', []); //定义索引 正序 Add('Index1', 'Id', [ixDescending]); //定义索引 倒序 end; FDMemTable1.CreateDataSet; for i := 1 to 15 do begin FDMemTable1.AppendRecord([i, 'ID' + IntTOSTR(i), Now()]); end;end;
procedure TForm1.Button2Click(Sender: TObject);
begin FDMemTable1.AppendRecord([StrToInt(Edit1.Text), Edit2.Text, StrToDate(Edit3.Text)]);end;procedure TForm1.Button5Click(Sender: TObject);
var fld_Id: TIntegerField; fld_Value: TStringField; fld_Time: TDateTimeField; i: Integer;begin //插入 with FDMemTable1 do begin DisableControls; //切断数据感知控件 try Open; //打开 //定义Field fld_Id := TIntegerField(FieldByName('Id')); fld_Value := TStringField(FieldByName('Value')); fld_Time := TDateTimeField(FieldByName('Time')); for i := 0 to 9 do begin Inc(Id); Append; //附加数据 //赋值 fld_Id.AsInteger := Id; fld_Value.AsString := 'Hello PFeng!' + inttostr(Id); fld_Time.AsDateTime := Now; Post; //确定 end; //UpdateIndexes;//更新索引 finally EnableControls; //连接数据感知控件 end; end;end;procedure TForm1.Button6Click(Sender: TObject);
var fld_Id: TIntegerField; fld_Value: TStringField; fld_Time: TDateTimeField;begin //更新 with FDMemTable1 do begin Open; fld_Id := TIntegerField(FieldByName('Id')); fld_Value := TStringField(FieldByName('Value')); fld_Time := TDateTimeField(FieldByName('Time')); if Locate('Id', 5, []) then begin Edit; fld_Value.AsString := '内容被修改'; fld_Time.AsDateTime := Now; Post; end; end;end;procedure TForm1.Button7Click(Sender: TObject);
var fld_Id: TIntegerField; fld_Value: TStringField; fld_Time: TDateTimeField;begin //删除 with FDMemTable1 do begin Open; fld_Id := TIntegerField(FieldByName('Id')); fld_Value := TStringField(FieldByName('Value')); fld_Time := TDateTimeField(FieldByName('Time')); if Locate('Id', 5, []) then begin Delete; //UpdateIndexes; end; end;end;procedure TForm1.Button8Click(Sender: TObject);
var fld_Id: TIntegerField; fld_Value: TStringField; fld_Time: TDateTimeField;begin //查询 with FDMemTable1 do begin Open; fld_Id := TIntegerField(FieldByName('Id')); fld_Value := TStringField(FieldByName('Value')); fld_Time := TDateTimeField(FieldByName('Time')); if Locate('Id', 5, []) then ShowMessage(fld_Value.AsString); //还可以用FindKey实现,结合索引速度更快 // kbmMemTable1.IndexFieldNames:='Id'; // if kbmMemtable1.FindKey([5]) then ... end;end;procedure TForm1.Button9Click(Sender: TObject);
var fld_Id: TIntegerField; fld_Value: TStringField; fld_Time: TDateTimeField; id: Integer;begin //循环比较最小值 with FDMemTable1 do begin Open; fld_Id := TIntegerField(FieldByName('Id')); fld_Value := TStringField(FieldByName('Value')); fld_Time := TDateTimeField(FieldByName('Time')); DisableControls; try First; id := fld_Id.AsInteger; while not Eof do begin if id > fld_Id.AsInteger then id := fld_Id.AsInteger; Next; end; Locate('Id', id, []); finally EnableControls; end; end;end;end.