Recent

Author Topic: MemTables Memdataset check fieldsdefintion before open  (Read 1329 times)

eldonfsr

  • Hero Member
  • *****
  • Posts: 528
MemTables Memdataset check fieldsdefintion before open
« on: November 14, 2024, 03:17:51 pm »
Hello well i using memdataset for create a memory tables and handle information but never has this fact..

1.- How i can update fieldsdefinition and don't lose information , if i update on design time when i run the program rise on error
     how i can validate current table changes before open and transfer information the other way big problem

2.- I need to keep old fielddefinitions and with that information handle and  migrate to new table structure...


Any Idea are welcome

eldonfsr

  • Hero Member
  • *****
  • Posts: 528
Re: MemTables Memdataset check fieldsdefintion before open
« Reply #1 on: November 14, 2024, 06:39:16 pm »
After reading i think get solve this problem,  what i did is open file creating dataset and assign the file name and open it, this way fields are created with file structure, after that compare fieldcount and fielddef.count see if both are same ..
Code: Pascal  [Select][+][-]
  1. on create form
  2.   TmpBuff:= TBufDataset.Create(nil);
  3.   TmpDs:= TDataSource.Create(nil);
  4.   TmpDs.DataSet := TmpBuff;
  5.  
  6. //--------------------------------------
  7.  
  8.  if(FileExists(GetCurrentDir+'\mmacros.db')) then begin
  9.     TmpBuff.FileName:=GetCurrentDir+'\mmacros.db';
  10.     TmpBuff.Open;
  11.     if(TmpBuff.FieldCount<>MDBtns.FieldDefs.Count) then begin
  12.       MigrateTable(TmpBuff, MDBtns);
  13.     end;
  14.     TmpBuff.close;
  15.   end;
  16.   TmpBuff.Clear;
  17.  
  18.  
Code: Pascal  [Select][+][-]
  1. Procedure TForMMain.MigrateTable(OldTbl:TBufDataSet; NewTbl:TBufDataSet);
  2. Var Fldname:String;
  3.     cf:Integer;
  4. begin
  5.  if( NewTbl.Active = false) then begin
  6.     NewTbl.Open;
  7.  end;
  8.  oldtbl.First;
  9.  while not oldtbl.EOF do begin
  10.    newtbl.Append;
  11.    for cf:=0 to oldtbl.FieldCount-1 do begin
  12.       fldname:= oldtbl.FieldDefs[cf].DisplayName;
  13.       newtbl.FieldByName(fldname).Value:= oldtbl.FieldByName(fldname).Value;
  14.    end;
  15.    newtbl.Post;
  16.    oldtbl.Next;
  17.  end;
  18.  NewTbl.SaveToFile(oldtbl.FileName);
  19. end;        
  20.  

Could be another way to do that but now for me is working..

Richard Marriott

  • Newbie
  • Posts: 6
Re: MemTables Memdataset check fieldsdefintion before open
« Reply #2 on: December 16, 2024, 07:08:45 am »
As far as I know, none of the TDataset descendants that ship with Lazarus allow a change to Field Definitions or Indexes after the table is Created and records added.
The only solution is the one that you have described, where a new table is defined and the old table is copied across.

egsuh

  • Hero Member
  • *****
  • Posts: 1525
Re: MemTables Memdataset check fieldsdefintion before open
« Reply #3 on: December 16, 2024, 07:18:25 am »
If you want to save table structure and data, and need to change the structrure sometimes, I recomment you to use SQLite etc.

 

TinyPortal © 2005-2018