Page 1 of 1

zMemTable - remove fields and unwanted records

Posted: 10.11.2021, 02:02
by kjteng
Hi,
I wanted to remove some fields and filtered records from a zMemTable at runtime (but not to alter the file that I originally loaded the data from).
Current I do it by copying the fields/records that I want to temporary memtable and than clone it to the original memtable (see code below). Long cut ... because I dont know how to deleted fields.
Any advice
would be much appreciated.

Code: Select all

procedure AltTable(mt: TZMemTable; ls: array of integer); // ls contains the fields (number) that I want to keep
var tmp: TZMemTable; ii: integer;
begin
  tmp := TZMemTable.Create(nil);
  try
    for ii := 0 to high(ls) do
      with mt.Fields[ls[ii]] do
        tmp.FieldDefs.Add(FieldName, DataType, Size, Required);
    tmp.Open;
    mt.First;
    while not mt.eof do
      with tmp do
      begin
        Append;
        for ii := 0 to FieldCount -1 do
          Fields[ii].Value := mt.Fields[ls[ii]].Value;
        Post;
        mt.Next;
      end;
    mt.Close;
    mt.CloneDataFrom(tmp); 
 {  tmp.SaveTofile('tmp');
    mt.LoadFromFile('tmp'); }
  finally
    tmp.free
  end;
end;     

Re: zMemTable - remove fields and unwanted records

Posted: 10.11.2021, 08:06
by aehimself
A bit hacky but it might work... try Memtable.Fieldbyname('somefield').Free ?
For records, simply use MemTable.Delete;

Re: zMemTable - remove fields and unwanted records

Posted: 14.11.2021, 04:34
by kjteng
aehimself:
No luck.
Memtable.Fieldbyname('somefield').Free results in 'Access violation' error.
MemTable.Delete can delete a record that is visible (i.e. not filtered out). My question is how to delete all records that are filtered out (not visible). I use CloneDataFrom because it would copy records that are filtered out.

Re: zMemTable - remove fields and unwanted records

Posted: 14.11.2021, 09:15
by marsupilami
Hello kjteng,

I think that TZMemTable is not made for removing fields and the like. Your best bet will be to have some kind of data pump that copies the data.

Best regards,

Jan

Re: zMemTable - remove fields and unwanted records

Posted: 14.11.2021, 10:03
by miab3
Hi kjteng, all,

You can operate between many different ZMemTable.

It would be more interesting and relatively simple to write/read parts (from-to) of the ZMemTable to/from disk, and merge data.
Though it might actually be useful to copy the definitions of the specified fields and data to the ZMemTable.

Michał

Re: zMemTable - remove fields and unwanted records

Posted: 17.11.2021, 04:21
by kjteng
miab3 wrote: 14.11.2021, 10:03 Hi kjteng, all,

You can operate between many different ZMemTable.

It would be more interesting and relatively simple to write/read parts (from-to) of the ZMemTable to/from disk, and merge data.
Though it might actually be useful to copy the definitions of the specified fields and data to the ZMemTable.

Michał
Thanks for your reply. Your suggestion is the similar to what I was trying to do with the code above. Right?

Re: zMemTable - remove fields and unwanted records

Posted: 17.11.2021, 10:04
by miab3
Hi kjteng,

Note that you can copy the structure without some fields.
As well as adding new fields.
And copy the data in a traditional way, indicating the correct ones.
In fact, a library of such routines, the ZMemTableUtils, would come in handy.

Michał

Re: zMemTable - remove fields and unwanted records

Posted: 17.11.2021, 11:56
by kjteng
miab3 wrote: 17.11.2021, 10:04 Hi kjteng,
...
As well as adding new fields.
And copy the data in a traditional way, indicating the correct ones.
In fact, a library of such routines, the ZMemTableUtils, would come in handy.

Michał
OK I see your point. Thank you for the valuable advice.
Where can I find ZMemTableUtils.pas ?

Re: zMemTable - remove fields and unwanted records

Posted: 17.11.2021, 12:44
by miab3
kjteng wrote: 17.11.2021, 11:56 Where can I find ZMemTableUtils.pas ?
I thought you would write.

Michał

Re: zMemTable - remove fields and unwanted records

Posted: 18.11.2021, 00:05
by kjteng
miab3 wrote: 17.11.2021, 12:44
kjteng wrote: 17.11.2021, 11:56 Where can I find ZMemTableUtils.pas ?
I thought you would write.

Michał
Haha. Okok. I will include it in my helper file.

Re: zMemTable - remove fields and unwanted records

Posted: 18.11.2021, 07:02
by miab3
Hi kjteng,

I was hoping that since you use this, you would write some useful procedures and share with us.

Michał

Re: zMemTable - remove fields and unwanted records

Posted: 20.11.2021, 16:56
by kjteng
miab3 wrote: 18.11.2021, 07:02 Hi kjteng,

I was hoping that since you use this, you would write some useful procedures and share with us.

Michał
Hi Michal,
I have uploaded to github. see viewtopic.php?f=50&t=148850.

Re: zMemTable - remove fields and unwanted records

Posted: 20.11.2021, 18:18
by miab3
Hi kjteng,

Thanks.
I see that you solved the problem of copying some fields and data.
If in the future you have something interesting with ZMemTable, publish it.

Michał