0

我正在使用TFDMemTable,我创建了一个数据集并用数据填充了我的表,然后用于FDMemTable.saveToFile保存我的数据。
现在问题来了,我怎样才能Field在这个已经保存的数据中添加一个新的并用默认值填充所有记录?
我尝试将新的添加FieldFDMemTable然后加载信息,希望它会使用它自动填充每个字段,FieldName并且新字段带有空白,但我收到错误消息:`------------ ---------------

调试器异常通知

Project Project1.exe 引发异常类 EDatabaseError,并带有消息“未找到字段 'Episode'”。

中断继续帮助

`
我该如何解决这个问题?是否可以解决向现有数据添加具有默认值的新字段的方法?
这是一个测试用例:

// here is a simple example, i have a few fields
FDMemTable1.FieldDefs.Add('ID', ftInteger, 0, false);
FDMemTable1.FieldDefs.Add('name', ftString, 30, false);
FDMemTable1.FieldDefs.Add('QualityID', ftInteger, 0, false);

FDMemTable1.CreateDataSet;

// i fill the the table with some value
FDMemTable1.Open;
FDMemTable1.AppendRecord([1, 'Movie1', 1]);
FDMemTable1.AppendRecord([2, 'Movie2', 2]);
FDMemTable1.AppendRecord([3, 'Movie3', 1]);

// after seeing the value on the grid, i push a button and save the table as XML
FDMemTable1.saveToFile('Data.xml');

// now after closing the program and running it again, i want to load the data with a new FieldDef Called Episode with a default value 0
// the table is connected to cxGrid, and the moment i try to load, i get the error i mentioned.

FDMemTable1.FieldDefs.Add('ID', ftInteger, 0, false);
FDMemTable1.FieldDefs.Add('name', ftString, 30, false);
// this line is NEW
FDMemTable1.FieldDefs.Add('Episode', ftInteger, 0, false);
FDMemTable1.FieldDefs.Add('QualityID', ftInteger, 0, false);

FDMemTable1.CreateDataSet;

// i try to load but i get an error
FDMemTable1.loadFromFile('Data.xml');

更新 1(基于维多利亚的回答):

//All the codes below are executed after the code at the top, after the loadFromFile.
  FDMemTable1.ResourceOptions.StoreItems := FDMemTable1.ResourceOptions.StoreItems + [siMeta];
  FDMemTable1.Close;

  //i tried putting FDMemTable1 as the owner but when i try the line "FDMemTable1.Fields.add" i get an Duplicate error!
  // and when i try putting nil, i get access violation ! so i tried putting the owner someother random table and it fixed the problem
  fieldDefs := TFieldDefs.Create(someRandomFDMemTable);
  fieldDefs.Add('Episodes', ftString, 30, false);

  FDMemTable1.Fields.Add(fieldDefs.Items[0].CreateField(FDMemTable1));
  FDMemTable1.Open;

如您所见,我有两个问题,
1-添加新字段时遇到问题!在我得到一个错误的地方,我首先尝试使用TFieldDef而不是TFieldDefs但我无法让它工作。
2- 是所有列都是空的并且网格上没有数据的事实。
当我尝试强行解决问题 1 时,会出现问题 2。

4

2 回答 2

5

下面的代码在西雅图德尔福对我来说很好用。它与您的密切相关,但有一些小的变化(见评论)和一个重要的补充。

问题是,如果您仔细观察FDMemTable1.loadFromFile,它实际上会清除 FieldDefs/Fields,因此一开始就没有太多必要再次设置它们,因此您添加的Episode字段只会从加载的数据集中被忽略。

为避免这种情况,我所做的添加是使用临时 TFDMemTable,将 XML 文件加载到其中,然后使用CopyDataSet. FDMemTable1在过程中保留了添加的Episode字段,当然你也可以CopyDataSet在调用后添加代码设置Episode字段数据。

procedure TForm1.AddFieldTest;
var
  DataFN : String;
  TempMemTable : TFDMemTable;
begin
  // Added, to avoid relative path for data file name
  DataFN := ExtractFilePath(Application.ExeName) + 'Data.Xml';

  FDMemTable1.FieldDefs.Add('ID', ftInteger, 0, false);
  FDMemTable1.FieldDefs.Add('name', ftString, 30, false);
  FDMemTable1.FieldDefs.Add('QualityID', ftInteger, 0, false);

  FDMemTable1.CreateDataSet;

  // i fill the the table with some value
  FDMemTable1.Open;
  FDMemTable1.AppendRecord([1, 'Movie1', 1]);
  FDMemTable1.AppendRecord([2, 'Movie2', 2]);
  FDMemTable1.AppendRecord([3, 'Movie3', 1]);

  // after seeing the value on the grid, i push a button and save the table as XML
  FDMemTable1.saveToFile(DataFN);

  // Added, close the dataset and clear the FieldDefs
  // Without the FieldDefs.Clear call, your code produces a "Duplicate field ID" error 
  FDMemTable1.Close;
  FDMemTable1.FieldDefs.Clear;

  // now after closing the program and running it again, i want to load the data with a new FieldDef Called Episode with a default value 0
  // the table is connected to cxGrid, and the moment i try to load, i get the error i mentioned.

  FDMemTable1.FieldDefs.Add('ID', ftInteger, 0, false);
  FDMemTable1.FieldDefs.Add('name', ftString, 30, false);
  // this line is NEW
  FDMemTable1.FieldDefs.Add('Episode', ftInteger, 0, false);
  FDMemTable1.FieldDefs.Add('QualityID', ftInteger, 0, false);

  FDMemTable1.CreateDataSet;

  // check the FieldCount and existence of the Episode field
  Caption := IntToStr(FDMemTable1.FieldCount);
  Assert(FDMemTable1.FindField('Episode') <> Nil);

  //  Create a temporary TFDMemTable
  TempMemTable := TFDMemTable.Create(Nil);
  try
    //  load the data into the temporary TFDMemTable
    TempMemTable.loadFromFile(DataFN);
    //  copy the data from the temporary TFDMemTable into FDMemTable1
    FDMemTable1.CopyDataSet(TempMemTable, [coAppend]);

  // check the FieldCount and existence of the Episode field
    Caption := IntToStr(FDMemTable1.FieldCount);
    Assert(FDMemTable1.FindField('Episode') <> Nil);
  finally
    TempMemTable.Free;
  end;
end;
于 2017-07-08T09:24:33.227 回答
1

从文件加载数据集后添加该字段(不是字段定义)(假设siMeta包含在TFDMemTable的ResourceOptionsStoreItems属性中)。请注意,在将字段添加到字段集合时必须关闭数据集。

因此,从文件加载后立即关闭数据集,添加字段并再次打开它。

于 2017-07-03T22:44:06.907 回答