About tdatabase

About tdatabase

Postby Wanderson » Sat Jun 01, 2013 12:29 pm

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.
Wanderson
 
Posts: 332
Joined: Thu Nov 17, 2005 9:11 pm

Re: About tdatabase

Postby James Bott » Sat Jun 01, 2013 6:43 pm

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 view
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
James Bott
 
Posts: 4840
Joined: Fri Nov 18, 2005 4:52 pm
Location: San Diego, California, USA

Re: About tdatabase

Postby nageswaragunupudi » Sun Jun 02, 2013 9:01 am

You can also use this code:
Code: Select all  Expand view
 
   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
nageswaragunupudi
 
Posts: 10632
Joined: Sun Nov 19, 2006 5:22 am
Location: India

Re: About tdatabase

Postby James Bott » Mon Jun 03, 2013 1:11 pm

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 view
  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
James Bott
 
Posts: 4840
Joined: Fri Nov 18, 2005 4:52 pm
Location: San Diego, California, USA

Re: About tdatabase

Postby nageswaragunupudi » Mon Jun 03, 2013 1:37 pm

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

G. N. Rao.
Hyderabad, India
User avatar
nageswaragunupudi
 
Posts: 10632
Joined: Sun Nov 19, 2006 5:22 am
Location: India

Re: About tdatabase

Postby James Bott » Mon Jun 03, 2013 1:57 pm

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
User avatar
James Bott
 
Posts: 4840
Joined: Fri Nov 18, 2005 4:52 pm
Location: San Diego, California, USA


Return to FiveWin for Harbour/xHarbour

Who is online

Users browsing this forum: Google [Bot] and 108 guests