Delphi learn to understand FireDac [UpdateStatus of cache update and record status]

RAD Studio 10.3 test √

After successfully connecting to the database [Connection Reference] Delphi FireDAC connects to the MySQL database
1. Put a cxGrid control on the form , set the properties
Insert picture description here
and find that there is no data in the table, then click Customize in the cxGrid, and then find Columns on the right, which can be added manually Click Add on the right, or directly search the fields in the table and click Retrieve Fields on the right, then the fields will be available. Keep going

2. Set the FDQuery property, and set the cache update to True.
Insert picture description here
3. The data has been modified and needs to be submitted before it can be saved in the database. At this time, add a Button and double-click to write these two lines of code

procedure TForm1.Button1Click(Sender: TObject);
begin
  FDQuery1.ApplyUpdates();        // 应用更新(确认更新)
  FDQuery1.CommitUpdates;         // 提交更新
end;

4. The previous has already been achieved, continue to add a little content. Add a status bar [StatusBar], double-click the status bar, and then add

5. Set the event of DataSource [OnDataChange]

procedure TForm1.DataSource1DataChange(Sender: TObject; Field: TField);
begin
  // 属性
  // cachedUpdates          是否缓存更新数据
  // ChangeCount            数据变动的数量

  // 判断 FDQuery1 内容是否有改动
  if FDQuery1.ChangeCount > 0 then
    StatusBar1.Panels[0].Text := '有改动--' + FDQuery1.ChangeCount.ToString
  else
    StatusBar1.Panels[0].Text := '无改动--' + FDQuery1.ChangeCount.ToString;
end;

6. Judge whether the data is submitted before exiting, and then prompt.
Find [OnCloseQuery] in the event of Form1, double-click to open it

procedure TForm1.FormCloseQuery(Sender: TObject; var CanClose: Boolean);
begin
  // UpdatesPending         是否有更新挂起【是否有数据变动】

  if FDQuery1.UpdatesPending then
    case Application.MessageBox('数据已修改,确认不保存退出吗?', '提示',
      MB_OKCANCEL + MB_DEFBUTTON2 + MB_TOPMOST) of
      IDOK:
        begin
          CanClose := True;
        end;
      IDCANCEL:
        begin
          CanClose := false;
        end;
    end;
end;

7. UpdateStatus of the status of the record
first create a new field: click FDQuery first to close the connection , right-click FDQuery -->field editor ->new field [then set properties],
then right-click the title bar of the table, add a column, and click the added column , Set the column properties: DataBinding --> State
Insert picture description here
Insert picture description here
are all set, find [OnCalcFields] in the FDQuery event, double-click to open it

procedure TForm1.FDQuery1CalcFields(DataSet: TDataSet);
begin
  // 计算字段
  case DataSet.UpdateStatus of
    usUnmodified:
      DataSet['State'] := '未修改';
    usModified:
      DataSet['State'] := '已修改';
    usInserted:
      DataSet['State'] := '已插入数据';
    usDeleted:
      DataSet['State'] := '已删除';
  end;
end;

After setting up these things, you will find that the deleted ones are not displayed. The default ones are not displayed. Let's add two Buttons to show them and restore the defaults.

procedure TForm1.Button2Click(Sender: TObject);
begin                          // FilterChages 设为显示全部
  FDQuery1.FilterChanges := [rtModified, rtInserted, rtDeleted, rtUnmodified, rtHasErrors];
end;

procedure TForm1.Button3Click(Sender: TObject);
begin                          // FilterChages 设为默认显示
  FDQuery1.FilterChanges := [rtModified, rtInserted, rtUnmodified];
end;

After finishing work, let's see the effect for yourself, it slipped away.


Note source code


A little bit of notes recorded during study, so that you can read it later.

Guess you like

Origin blog.csdn.net/qq_44111597/article/details/108321085