Board index » delphi » Best way to copy table record?

Best way to copy table record?

[Delphi 1]
I have a Paradox table with 110 fields. One of the operations to do
consists in creating (in the same table) a copy of one of the records,
which differs from the original record in only one field (the other
109 fields remain unchanged).

Saving and then reassigning the values of all those fields would be
rather tedious... Is there a simpler way to do it?

Thanks!

Thomas

 

Re:Best way to copy table record?


Try looping through the fields.  For this though, you would need to open two
tables, one to read from and one to insert a new record into.

var i : integer;

MyNewTable.Insert;

with MyTable do
begin
    { locate record to copy}

    { Then loop through fields. }
    for i := 0 to FieldCount-1 do
      begin
            if Fields[i].FieldName = 'MyField' then
                {set the different field }
            else
              MyNewTable.Fields[i].Assign(Fields[i]);
      end;
end;

MyNewTable.Post;

Quote
btm wrote in message <3794587f.15428...@news.spectraweb.ch>...
>[Delphi 1]
>I have a Paradox table with 110 fields. One of the operations to do
>consists in creating (in the same table) a copy of one of the records,
>which differs from the original record in only one field (the other
>109 fields remain unchanged).

>Saving and then reassigning the values of all those fields would be
>rather tedious... Is there a simpler way to do it?

>Thanks!

>Thomas

Re:Best way to copy table record?


Assuming that TTable1 is the source pointer and TTable2 is the target
pointer do this:

TTable2.Append; { create a blank record }
{ with TTable1 on the record you want to copy do the following: }

with TTable2 do
    for i := 0 to FieldCount-1 do
      begin

FindField(Fields[i].Name).Value:=TTable1.FieldByName(Fields[i].Name).Value;

      end;
TTable2.Post; { save the appended record }
{ NOTE that this requires the field names to be identical for the
match to work (Last Name and Last_Name will NOT match) but this will
insure that all fields in TTable2 are accounted for}

Hope this helps!

LONG LIVE DELPHI!

Bill

Quote
On Tue, 20 Jul 1999 11:07:47 GMT, b...@gmx.net (btm) wrote:
>[Delphi 1]
>I have a Paradox table with 110 fields. One of the operations to do
>consists in creating (in the same table) a copy of one of the records,
>which differs from the original record in only one field (the other
>109 fields remain unchanged).

>Saving and then reassigning the values of all those fields would be
>rather tedious... Is there a simpler way to do it?

>Thanks!

>Thomas

Re:Best way to copy table record?


That's tooooo slowly!!!

Try TTable.ActiveBuffer.
This is a  pointer to the internal RecordBuffer. Copy TTable.RecordSize
Bytes from Source to Destination Table. It's really fast!

Greetings

CHMILO

Quote
Bill Artemik wrote:

> Assuming that TTable1 is the source pointer and TTable2 is the target
> pointer do this:

> TTable2.Append; { create a blank record }
> { with TTable1 on the record you want to copy do the following: }

> with TTable2 do
>     for i := 0 to FieldCount-1 do
>       begin

> FindField(Fields[i].Name).Value:=TTable1.FieldByName(Fields[i].Name).Value;

>       end;
> TTable2.Post; { save the appended record }
> { NOTE that this requires the field names to be identical for the
> match to work (Last Name and Last_Name will NOT match) but this will
> insure that all fields in TTable2 are accounted for}

> Hope this helps!

> LONG LIVE DELPHI!

> Bill

> On Tue, 20 Jul 1999 11:07:47 GMT, b...@gmx.net (btm) wrote:

> >[Delphi 1]
> >I have a Paradox table with 110 fields. One of the operations to do
> >consists in creating (in the same table) a copy of one of the records,
> >which differs from the original record in only one field (the other
> >109 fields remain unchanged).

> >Saving and then reassigning the values of all those fields would be
> >rather tedious... Is there a simpler way to do it?

> >Thanks!

> >Thomas

Re:Best way to copy table record?


The TTable.ActiveBuffer is a neat trick! I will try this myself,
however, the user needs to copy FIELDS from one table to another as
their structures are different...

On Wed, 21 Jul 1999 10:17:38 +0200, Steffen Chmil <chm...@infosys.de>
wrote:

Quote
>That's tooooo slowly!!!

>Try TTable.ActiveBuffer.
>This is a  pointer to the internal RecordBuffer. Copy TTable.RecordSize
>Bytes from Source to Destination Table. It's really fast!

>Greetings

>CHMILO

>Bill Artemik wrote:

>> Assuming that TTable1 is the source pointer and TTable2 is the target
>> pointer do this:

>> TTable2.Append; { create a blank record }
>> { with TTable1 on the record you want to copy do the following: }

>> with TTable2 do
>>     for i := 0 to FieldCount-1 do
>>       begin

>> FindField(Fields[i].Name).Value:=TTable1.FieldByName(Fields[i].Name).Value;

>>       end;
>> TTable2.Post; { save the appended record }
>> { NOTE that this requires the field names to be identical for the
>> match to work (Last Name and Last_Name will NOT match) but this will
>> insure that all fields in TTable2 are accounted for}

>> Hope this helps!

>> LONG LIVE DELPHI!

>> Bill

>> On Tue, 20 Jul 1999 11:07:47 GMT, b...@gmx.net (btm) wrote:

>> >[Delphi 1]
>> >I have a Paradox table with 110 fields. One of the operations to do
>> >consists in creating (in the same table) a copy of one of the records,
>> >which differs from the original record in only one field (the other
>> >109 fields remain unchanged).

>> >Saving and then reassigning the values of all those fields would be
>> >rather tedious... Is there a simpler way to do it?

>> >Thanks!

>> >Thomas

Re:Best way to copy table record?


On Tue, 20 Jul 1999 08:49:10 -0400, "BlueGI"

Quote
<giwachowNOS...@bcw-usaNOSPAM.com> wrote:
>Try looping through the fields.  For this though, you would need to open two
>tables, one to read from and one to insert a new record into.

>var i : integer;

>MyNewTable.Insert;

>with MyTable do
>begin
>    { locate record to copy}

>    { Then loop through fields. }
>    for i := 0 to FieldCount-1 do
>      begin
>            if Fields[i].FieldName = 'MyField' then
>                {set the different field }
>            else
>              MyNewTable.Fields[i].Assign(Fields[i]);
>      end;
>end;

>MyNewTable.Post;

<snip>

Damn, that's even more elegant than my solution! ;-)

Cheers...

Re:Best way to copy table record?


Quote
On Tue, 20 Jul 1999 11:07:47 GMT, b...@gmx.net (btm) wrote:
>[Delphi 1]
>I have a Paradox table with 110 fields. One of the operations to do
>consists in creating (in the same table) a copy of one of the records,
>which differs from the original record in only one field (the other
>109 fields remain unchanged).

>Saving and then reassigning the values of all those fields would be
>rather tedious... Is there a simpler way to do it?

>Thanks!

>Thomas

I quite often do something like this:

tblDest.Insert;
with tblSource do
begin
   for i := 0 to FieldCount - 1 do
   begin
      case Fields[i].DataType of
         ftString : tblDest.FieldByName(Fields[i].FieldName).AsString
:= Fields[i].AsString;
         ftInteger :
tblDest.FieldByName(Fields[i].FieldName).AsInteger :=
Fields[i].AsInteger;
      end;
   end;
end;

Unfortunately, the indentation is probably stuffed!  This may be slow,
but it works for exporting data to text files and keeps the lines of
code down!

Cheers...

Re:Best way to copy table record?


This one works only when you have fields addet on a table :-(...

procedure AppendirajJednog(BazaIzvor, BazaCilj:TDataset);
var
  Proba: Array[0..255] of Variant;
  i      : Integer;
begin
  // Uitaj podatke u niz//Read the data in Array
  i:=0;
  for i := 0 to (BazaIzvor.Fieldcount-1) do
  begin
       Proba[i]:=BazaIzvor.Fields[i].AsVariant;
  end;
  BazaCilj.Append ;
 i:=0;
// Ispi?i podatke iz niza u bazu// Write the data in the table
  for i := 0 to (BazaCilj.Fieldcount-1) do
  begin
// if BazaCilj.Fields[i].FieldName = 'Field to changed' then//You could add
this line
       else  BazaCilj.fields[i].Value := Proba[i] ;
  end;
  BazaCilj.Post;
end;

btm <b...@gmx.net> wrote in article
<3794587f.15428...@news.spectraweb.ch>...

Quote
> [Delphi 1]
> I have a Paradox table with 110 fields. One of the operations to do
> consists in creating (in the same table) a copy of one of the records,
> which differs from the original record in only one field (the other
> 109 fields remain unchanged).

> Saving and then reassigning the values of all those fields would be
> rather tedious... Is there a simpler way to do it?

> Thanks!

> Thomas

Re:Best way to copy table record?


I was searching someting in bdeapi clls and found this:

function DbiPutField (hCursor: hDBICur; iField: Word; pRecBuff: Pointer;
pSrc: Pointer): DBIResult stdcall;

Description

DbiPutField writes the field value to the correct location in the supplied
record buffer.

Parameters

hCursor         Type: hDBICur   (Input)
Specifies the cursor handle.
iField  Type: UINT16    (Input)
Specifies the ordinal number of the field to be updated.
pRecBuf         Type: pBYTE     (Input)
Pointer to the record buffer, which is updated upon success.
pSrc    Type: pBYTE     (Input)
Pointer to the new field value.

Usage

This function is used to update a record one field at a time. If a NULL
pointer is supplied, the field is set to NULL or blank.

If the xltMODE for the cursor is xltFIELD, pSrc is assumed to contain field
data in BDE logical format. This data is translated to the driver's
physical type by this function. If xltMODE is xltNONE, pSrc is assumed to
contain field data in physical format.

DbiPutField is not supported with BLOB fields.

Did someone try to use it??????
                                Robert

Re:Best way to copy table record?


Quote
Bill Artemik wrote:

> The TTable.ActiveBuffer is a neat trick! I will try this myself,
> however, the user needs to copy FIELDS from one table to another as
> their structures are different...

bad but slooooow luck for this user...

CHMILO

p.s. I work with databases around 2GB, if you have small files, the way
to copy records doesn't matter ;-)

Re:Best way to copy table record?


Hello,
why not use the BatchMove component, filter the table down to the record to
be copied, move it to a helptable and back after reassigning the value?
That's pretty fast.

:)
Kai Inge

btm skrev i meldingen <3794587f.15428...@news.spectraweb.ch>...

Quote
>[Delphi 1]
>I have a Paradox table with 110 fields. One of the operations to do
>consists in creating (in the same table) a copy of one of the records,
>which differs from the original record in only one field (the other
>109 fields remain unchanged).

>Saving and then reassigning the values of all those fields would be
>rather tedious... Is there a simpler way to do it?

>Thanks!

>Thomas

Re:Best way to copy table record?


Hello, I do it by looping through the fields in the table, saving
their values into a Variant Array.  Then Append, and assign
the values in another loop:

note: vals is a variant array of however many fields you have in your
table.

       for I := 0 to Table1.FieldCount-1 do
       begin
            vals[I] := Table1.Fields[I].Value;  
       end;

       Table1.Append;

       for I := 0 to Table1.FieldCount-1 do
       begin
       // skip the  field that is different
            if Table1.Fields[I].FieldName = 'fieldname' then continue;
            Table1.Fields[I].Value := vals[I];
       end;

Clare
Clar...@usa.net

On Wed, 25 Aug 1999 17:21:18 GMT, "Lars Ole Oksnes"

Quote
<lars.ole.oks...@c2i.net> wrote:
>>I have a Paradox table with 110 fields. One of the operations to do
>>consists in creating (in the same table) a copy of one of the records,
>>which differs from the original record in only one field (the other
>>109 fields remain unchanged).

>>Saving and then reassigning the values of all those fields would be
>>rather tedious... Is there a simpler way to do it?

>>Thanks!

>>Thomas

Other Threads