About tdatabase

Post Reply
Wanderson
Posts: 332
Joined: Thu Nov 17, 2005 9:11 pm

About tdatabase

Post by Wanderson »

Hi,

I work with native tdatabase fivewin, i want to put all value fields from on record in another like this:

oArqCli:GoTop()
Do While ! oArqCli:Eof()
oArqCli:Load()
oCliCli:Blank()
oCliCli:(All fields) := oArqCli:(All fields) <------------------- how?
oCliCli:Append()
oCliCli:Save()
oCliCli:Commit()
oArqCli:Skip(+1)
loop
Enddo

Thanks.
User avatar
James Bott
Posts: 4840
Joined: Fri Nov 18, 2005 4:52 pm
Location: San Diego, California, USA
Contact:

Re: About tdatabase

Post by James Bott »

Wanderson,

Note that all the field data is stored in a buffer array in database objects. Also, of course, both files must have exactly the same structure. Below is an example of how to copy all the field data.

It depends on the situation, but you may want to only do one commit after the ENDDO instead of one after each record save. This would be much faster, but maybe not as safe in a multi-user environment. However, if you are only copying a few records this would only take a few seconds so there isn't much risk.

James

Code: Select all | Expand

oArqCli:GoTop()
Do While ! oArqCli:Eof()
   oArqCli:Load()
   oCliCli:Blank()
   //oCliCli:(All fields) := oArqCli:(All fields) <------------------- how?

   copyAll(oArqCli, oCliCli)

   oCliCli:Append()
   oCliCli:Save()
   oCliCli:Commit()
   oArqCli:Skip(+1)
   loop
Enddo


function copyAll( oDB1, oDB2 )
   Local i:=1
   for i 1 to len(oDB1:aBuffer)
      oDB2:aBuffer[i]:= oDB1:aBuffer[i]
   next
return nil
User avatar
nageswaragunupudi
Posts: 10721
Joined: Sun Nov 19, 2006 5:22 am
Location: India
Been thanked: 8 times
Contact:

Re: About tdatabase

Post by nageswaragunupudi »

You can also use this code:

Code: Select all | Expand

 
   oArqCli:lBuffer := .t.  // This is default
   oArqCli:GoTop()
   do while ! oArqCli:Eof()
      oCliCli:Append()
      ACOPY( oArqCli:aBuffer, oCliCli:aBuffer )
      oCliCli:Save()
      oArqCli:Skip()
   enddo
   oCliCli:Close()

 
Regards

G. N. Rao.
Hyderabad, India
User avatar
James Bott
Posts: 4840
Joined: Fri Nov 18, 2005 4:52 pm
Location: San Diego, California, USA
Contact:

Re: About tdatabase

Post by James Bott »

Yes, acopy() would be faster. If you set oArqCli:lBuffer I also suggest saving and restoring its state so as to prevent any possible problems elsewhere.

James

Code: Select all | Expand

  Local lBuffer
   ...
   lBuffer:= oArqCli:lBuffer
   oArqCli:lBuffer := .t.  // this is default
   oArqCli:GoTop()
   do while ! oArqCli:Eof()
      oCliCli:Append()
      ACOPY( oArqCli:aBuffer, oCliCli:aBuffer )
      oCliCli:Save()
      oArqCli:Skip()
   enddo
   oCliCli:Close()
   oArqCli:lBuffer:= lBuffer 
User avatar
nageswaragunupudi
Posts: 10721
Joined: Sun Nov 19, 2006 5:22 am
Location: India
Been thanked: 8 times
Contact:

Re: About tdatabase

Post by nageswaragunupudi »

For TDatabase, lBuffer is .t. by default.
Regards

G. N. Rao.
Hyderabad, India
User avatar
James Bott
Posts: 4840
Joined: Fri Nov 18, 2005 4:52 pm
Location: San Diego, California, USA
Contact:

Re: About tdatabase

Post by James Bott »

Rao,

For TDatabase, lBuffer is .t. by default.


Yes, I understand that. But if you change something like that it is wise to save and restore it's state. It is wise to do as you suggested, set it to true at the start of the routine because you can't depend on it being true already.

As I expect you already know, encapsulation is one of the key principles of OOP. So whenever, you change the state of something you should also restore it. I have found this to be a very useful principle to follow. I can't imagine how many problems I have prevented by using this simple technique.

Regards,
James
Post Reply