代码之家  ›  专栏  ›  技术社区  ›  zac

如何限制TFDMemTable中的记录数?

  •  0
  • zac  · 技术社区  · 6 年前

    我有一个 TFDMemTable表格 有成千上万的记录。有没有办法限制前50个的成绩记录?

    我试过使用:

    FDMemTable.FetchOptions.RecsSkip := 0;
    FDMemTable.FetchOptions.RecsMax := 50;
    FDMemTable.Open;
    

    但它没有起作用,数据保持不变。

    1 回复  |  直到 6 年前
        1
  •  2
  •   MartynA    6 年前

    我希望@Victoria能给你展示一个更好更一般的 但至少有两种方法:

    • 一个FDQuery,然后将它们复制回FDMemTable。

    • 对FDMemTable应用筛选器要筛选出其他记录,请使用FDBatchMove 将X记录复制到第二个FDMemTable中,然后将它们复制回原始FDMemTable FDMemTable表格。

    要实现其中的第一个,请将以下组件添加到窗体/数据模块中:

    FDLocalSQL1: TFDLocalSQL;
    FDConnection1: TFDConnection;
    FDQuery1: TFDQuery;
    FDPhysSQLiteDriverLink1: TFDPhysSQLiteDriverLink;
    

    然后执行如下代码:

    procedure TForm3.CopyData1;
    begin
      FDConnection1.DriverName := 'SQLite';
      FDConnection1.Connected := True;
    
      FDLocalSQL1.Connection := FDConnection1;
      FDLocalSQL1.DataSets.Add(FDMemTable1);  // this is the source dataset
    
      FDLocalSQL1.Active := True;
    
      FDQuery1.SQL.Text := 'select * from FDMemTable1 order by ID limit 5';  //  ID being an Integer field of the FDMemTable
      FDQuery1.Active := True;
    
      FDMemTable1.Close;
      FDMemTable1.Data := FDQuery1.Data;  // Re-opens FDMemTable 1, which now contains only the first X records
    end;
    

    “Select Top X…”是它的限制子句。

    支持order by,您可以通过它来确定保留哪些(top)X记录。

    batchmove方法需要更少的代码,但需要有一种识别方法

    procedure TForm3.CopyData2;
    begin
      FDMemTable1.Filter := 'ID <=50';
      FDMemTable1.Filtered := True;
      FDBatchMove1.Execute;  //  move data from FDMemTable1 to FDMemTable2;
      FDMemTable1.Close;
      FDMemTable1.Data := FDMemTable2.Data;  // Re-opens FDMemTable 1, which now contains only the first X records
    end;
    

    顺便说一句,你说

    我有一个TFDMemTable,里面有成千上万条记录。我