Copy record to same table?

The stable tester's forum for ZeosLib 7.0.x series

Report problems concerning our Delphi 2009+ version and new Zeoslib 7.0 features here.
Post Reply
IbeDBob
Junior Boarder
Junior Boarder
Posts: 47
Joined: 27.05.2010, 21:04

Copy record to same table?

Post by IbeDBob »

Hi All,

Is there an easy way to copy a record o the same table?

I have an SQLite database that the user may often want to have almost the same stuff in another record, and just change one or two fields.

Is there something like BatchMove that I can copy all but the AutoInc field? There are no no unique, cannot be blank etc conflicts.

I know I can write a

...fieldbyname.AsString = ...fieldbyname..

for something like 120 fields but would rather just use something like CopyRecord(tblFrom, tblTo

Thanks
Thanks

Dyslexic Bob
marsupilami
Platinum Boarder
Platinum Boarder
Posts: 1962
Joined: 17.01.2011, 14:17

Post by marsupilami »

hmmm - something like that:

Code: Select all

procedure copyfields(Src, Target: TDataSet; Blacklist: String);
var
  x: Integer;
begin
  Blacklist := ';' + Blacklist + ';';
  for x := 0 to Src.Fields.Count do begin // iterate over all source fields
    if Pos(';' + Src.Fields[x].FieldName + ';', Blacklist) <> 0 then begin //check if the Field is in the blacklist
      Dst.FieldByName(Src.Fields[x].FieldName).Value := Src.Fields[x].Value;
    end;
  end;
end;
The Blacklist is a list of the fields you don't want to copy. The Fields are separated by Semicolons...
IbeDBob
Junior Boarder
Junior Boarder
Posts: 47
Joined: 27.05.2010, 21:04

Post by IbeDBob »

Thanks for going to that trouble, but I had already tried something like that and it would not copy the data to the new record. It created and empty record. Here's what I tried...

Code: Select all

  for i:=1 to tblFrom.FieldCount-1 do  // Start from 1 to skip the AutoInc
  begin
    if tblTo.FindField(tblFrom.Fields[i].FieldName) <> nil then
    begin
      tblTo.FieldByName(tblFrom.Fields[i].FieldName).Value:=tblFrom.Fields[i].Value;
    end;
  end;
I finally got around it by creating a table at run time, Copying a record to it then back to the originating DataSet. That worked well.
Thanks

Dyslexic Bob
Post Reply