在加载现有数据时向FDMemTable添加新字段

时间:2017-07-02 19:54:37

标签: delphi firedac fdmemtable

我使用TFDMemTable,我创建了一个数据集并用数据填充了我的表格,然后使用FDMemTable.saveToFile来保存我的数据。
现在问题是,我如何向已保存的数据添加新Field并使用默认值填充所有记录?
我尝试将新的Field添加到FDMemTable,然后加载信息,希望它会使用它FieldName自动填充每个字段,并使用空格填充新字段,但是我得到一个错误说: `---------------------------

调试器异常通知

Project Project1.exe引发了异常类EDatabaseError,其中包含消息' Field' 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.

2 个答案:

答案 0 :(得分:2)

下面的代码在Delphi Seattle中对我来说很好。它紧密地基于你的,但有一些细微的变化(见评论)和一个重要的补充。

问题是,如果你仔细观察FDMemTable1.loadFromFile,它实际上会清除FieldDefs / Fields,所以首先要重新设置它们,所以添加了Episode字段只是从加载的数据集中省略。

为了避免这种情况,我做的补充是使用临时TFDMemTable,将XML文件加载到其中,然后使用CopyDataSet将其内容复制到FDMemTable1。 FDMemTable1在此过程中保留添加的剧集字段,您当然可以在调用CopyDataSet后添加代码以设置剧集字段数据。

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;

答案 1 :(得分:0)

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

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