Board index » delphi » in-memory tables, key-group locked error, copying records

in-memory tables, key-group locked error, copying records

I've got three questions:

(1) What is the best way to copy a single record from one TTable to another? TBatchMove
doesn't seem flexible enough for copying specific ranges of records, so copying them
one by one seems like the only option. Other than using Fields and SetFields, I can't
see an efficient way to do it (I'm used to the copying functions in ObjectPAL).

(2) I found a unit for in-memory tables.. I'm assuming that you need to set the
FielDefs for the in-memory TTable in order for it to work, but I'm having trouble with
the TFieldDefs.add() method -- Delphi tells me I need another paramater (other than the
three which the documentation shows); I'm thinking perhaps the help is flawed. Does
anyone know the exact procedure definition?

(3) What exactly does the "key group locked" error mean? It appears to happen everytime
I change the key field (there's only one) and try posting the record. I'm assuming
changing the key and posting the record just makes it "fly" to the proper position, as
it does in Paradox.

Any help would be greatly appreciated...

~~~~~~~~~~~~~~~~~~~~~ "the revolution is now. why settle?" ~~~~~~~~~~~~~~~~~~~~~~~
Kito D. Mann                                            Virtua Communications Corp
km...@virtua.com                                  http://www.virtua.com
(410)662-7280                                           3100 Falls Cliff Rd. #200
(410)467-0127 (FAX)                                     Baltimore, MD 21211

 

Re:in-memory tables, key-group locked error, copying records


Kito D. Mann (km...@virtua.com) wrote:
: I've got three questions:

: (1) What is the best way to copy a single record from one TTable to another? TBatchMove
: doesn't seem flexible enough for copying specific ranges of records, so copying them
: one by one seems like the only option. Other than using Fields and SetFields, I can't
: see an efficient way to do it (I'm used to the copying functions in ObjectPAL).

I imagine (though I haven't tried it) that you could simply loop through the
list of fields and assign from one to the other.  This assumes the structures
of the two tables are compatible.

  for nI := 0 to Table1.FieldCount - 1 do
    Table2.Fields[nI].AsString := Table1.Fields[nI].AsString;

: (2) I found a unit for in-memory tables.. I'm assuming that you need to set the
: FielDefs for the in-memory TTable in order for it to work, but I'm having trouble with
: the TFieldDefs.add() method -- Delphi tells me I need another paramater (other than the
: three which the documentation shows); I'm thinking perhaps the help is flawed. Does
: anyone know the exact procedure definition?

: (3) What exactly does the "key group locked" error mean? It appears to happen everytime
: I change the key field (there's only one) and try posting the record. I'm assuming
: changing the key and posting the record just makes it "fly" to the proper position, as
: it does in Paradox.

: Any help would be greatly appreciated...

: ~~~~~~~~~~~~~~~~~~~~~ "the revolution is now. why settle?" ~~~~~~~~~~~~~~~~~~~~~~~
: Kito D. Mann                                          Virtua Communications Corp
: km...@virtua.com                                        http://www.virtua.com
: (410)662-7280                                                 3100 Falls Cliff Rd. #200
: (410)467-0127 (FAX)                                   Baltimore, MD 21211

Re:in-memory tables, key-group locked error, copying records


Glen W. Mann (gm...@bugs.aditech.com) wrote:
: Kito D. Mann (km...@virtua.com) wrote:
: : I've got three questions:

: : (1) What is the best way to copy a single record from one TTable to another? TBatchMove
: : doesn't seem flexible enough for copying specific ranges of records, so copying them
: : one by one seems like the only option. Other than using Fields and SetFields, I can't
: : see an efficient way to do it (I'm used to the copying functions in ObjectPAL).

: I imagine (though I haven't tried it) that you could simply loop through the
: list of fields and assign from one to the other.  This assumes the structures
: of the two tables are compatible.

:   for nI := 0 to Table1.FieldCount - 1 do
:     Table2.Fields[nI].AsString := Table1.Fields[nI].AsString;

After trying this (I apologize for not doing so beforehand), the loop
above does not work.  Anyone know why?  Seems like it should.  I tried
.Value whch also does not work.  This does work:

  for nI := 0 to Table1.FieldCount - 1 do
    Table1.FieldByName(Table1.Fields[nI].FieldName).AsString :=
           Table2.FieldByName(Table1.Fields[nI].FieldName).AsString;

HTH
-Glen

Re:in-memory tables, key-group locked error, copying records


Quote
Glen W. Mann wrote:

> Kito D. Mann (km...@virtua.com) wrote:
> : I've got three questions:

> : (1) What is the best way to copy a single record from one TTable to another? TBatchMove
> : doesn't seem flexible enough for copying specific ranges of records, so copying them
> : one by one seems like the only option. Other than using Fields and SetFields, I can't
> : see an efficient way to do it (I'm used to the copying functions in ObjectPAL).

> I imagine (though I haven't tried it) that you could simply loop through the
> list of fields and assign from one to the other.  This assumes the structures
> of the two tables are compatible.

>   for nI := 0 to Table1.FieldCount - 1 do
>     Table2.Fields[nI].AsString := Table1.Fields[nI].AsString;

Thanks for the advice. Funny how the Delphi help says that Fields is "run-time and read-only."
I think that's the third error I've found in the help so far.

Does anyone know where I can get an updated help file? (I'm using one of the first pressings
of Delphi 1.0.)

~~~~~~~~~~~~~~~~~~ "the revolution is now. why settle?" ~~~~~~~~~~~~~~~~~~~~~~~~~~
Kito D. Mann                                            Virtua Communications Corp
km...@virtua.com                                  http://www.virtua.com
(410)662-7280                                           3100 Falls Cliff Rd. #200
(410)467-0127 (FAX)                                     Baltimore, MD 21211

Re:in-memory tables, key-group locked error, copying records


On 29 May 1996 14:03:12 GMT, gm...@bugs.aditech.com (Glen W. Mann)
wrote:

Quote
>Glen W. Mann (gm...@bugs.aditech.com) wrote:
>: Kito D. Mann (km...@virtua.com) wrote:
>: : I've got three questions:

>: : (1) What is the best way to copy a single record from one TTable to another? TBatchMove
>: : doesn't seem flexible enough for copying specific ranges of records, so copying them
>: : one by one seems like the only option. Other than using Fields and SetFields, I can't
>: : see an efficient way to do it (I'm used to the copying functions in ObjectPAL).

>: I imagine (though I haven't tried it) that you could simply loop through the
>: list of fields and assign from one to the other.  This assumes the structures
>: of the two tables are compatible.

>:   for nI := 0 to Table1.FieldCount - 1 do
>:     Table2.Fields[nI].AsString := Table1.Fields[nI].AsString;

>After trying this (I apologize for not doing so beforehand), the loop
>above does not work.  Anyone know why?  Seems like it should.  I tried
>..Value whch also does not work.  This does work:

>  for nI := 0 to Table1.FieldCount - 1 do
>    Table1.FieldByName(Table1.Fields[nI].FieldName).AsString :=
>           Table2.FieldByName(Table1.Fields[nI].FieldName).AsString;

Her's a general function for copying between two tables. It could be
made a bit slicker by looking up the field types etc and then using
FindField rather than comparing two string lists, but it works and
will also copy between tables without exactly the same structures.

both the src and tgt tables have to be open. Setting RecCreate to True
creates a new record in the target table while setting RecPost to
False leaves the target record unposted for you to do further work on.

function DbsCopyRecord(srcTbl, tgtTbl : TTable; RecCreate, RecPost :
Boolean) : Integer;
var
    i, c, added : integer;
    SFldNames, TFldNames: TStringList;
 begin

   Result := 0;

   try
     SFldNames := TStringList.Create;      {open the list for field
names}
     srcTbl.GetFieldNames(SFldNames);      {get the fieldnames into
the list}

     TFldNames := TStringList.Create;      {open the list for field
names}
     tgtTbl.GetFieldNames(TFldNames);      {get the fieldnames into
the list}

     tgtTbl.Edit;                           {set target into edit
mode}

     if RecCreate then tgtTbl.Append;      {add a new record if
required}

     {now copy the values from Source to Target.}

     for i := 0 to (SFldNames.Count - 1) do begin

        if TFldNames.IndexOf(SFldNames[i)) then begin

tgtTbl.FieldByName(SFldNames[i]).Assign(srcTbl.FieldByName(SFldNames[i]));
           inc(Result);
        end;

     end;

   finally
     if RecPost then
        tgtTbl.Post;            
     SFldNames.Free;      
     TFldNames.Free;
   end;

   end;

Russell Weetch
and UK Index is at http://www.ukindex.co.uk

Other Threads