FiveTech Support Forums

FiveWin / Harbour / xBase community
Board index FiveWin para Harbour/xHarbour database oDbf
Posts: 479
Joined: Fri Feb 16, 2007 10:29 AM
database oDbf
Posted: Tue May 04, 2021 06:21 PM
Compañeros, buenas tardes:

Tengo una DBF con muchos campos. Necesito copiar un registro y pegarlo en otra DBF idéntica (hacer un duplicado de ese registro). Para evitar tantos "replace..." por cada campo, quiero utilizar la clase tdatabase

Code (fw): Select all Collapse
select 1
database oDbf 
oDbf:load()    && copia
select  2 
append blank 
oDbf:save() && pega


Sin embargo, solo me crea el registro en blanco y nada más.
¿ Qué hago mal?

Gracias. Saludos.
LORENZO:
Posts: 10733
Joined: Sun Nov 19, 2006 05:22 AM
Re: database oDbf
Posted: Wed May 05, 2021 08:59 AM
Code (fw): Select all Collapse
SELECT 1
DATABASE oDbf1
aCopy := oDbf1:Copy()

SELECT 2
DATABASE oDbf2
oDbf2:Append( aCopy )
Regards



G. N. Rao.

Hyderabad, India
Posts: 10733
Joined: Sun Nov 19, 2006 05:22 AM
Re: database oDbf
Posted: Wed May 05, 2021 09:31 AM
Another method, without the overhead of creating 2 TDatabase objects.

Code (fw): Select all Collapse
SELECT 1
aData := FW_DBFToArray( nil, nil, nil, 1 )

SELECT 2
FW_ArrayToDbf( aData )
Regards



G. N. Rao.

Hyderabad, India
Posts: 10733
Joined: Sun Nov 19, 2006 05:22 AM
Re: database oDbf
Posted: Wed May 05, 2021 12:54 PM
Simplest way

Code (fw): Select all Collapse
SELECT 1
hRec := FW_RecToHash()

SELECT 2
APPEND BLANK
FW_HashToRec( hRec )
DBUNLOCK()
Regards



G. N. Rao.

Hyderabad, India
Posts: 1487
Joined: Tue Jun 14, 2016 07:51 AM
Re: database oDbf
Posted: Sun May 16, 2021 01:23 PM

Witch off these techniques are the best when the source dbf will always have less fields than the target, but the fields always match in name and structure.

There must and will be one index field for matching and updating.

There will also be some replacement done (ex. a standard value that is needed) just before the update in the source dbf field.

Marc Venken

Using: FWH 23.08 with Harbour
Posts: 1487
Joined: Tue Jun 14, 2016 07:51 AM
Re: database oDbf
Posted: Sun May 16, 2021 01:32 PM

The Gatter() and scatter() functions are anno 2021 not preferred anymore right ?

Marc Venken

Using: FWH 23.08 with Harbour
Posts: 10733
Joined: Sun Nov 19, 2006 05:22 AM
Re: database oDbf
Posted: Sun May 16, 2021 02:51 PM

Witch off these techniques are the best when the source dbf will always have less fields than the target, but the fields always match in name and structure.


We recommend FW_RecToHash() and FW_HashToRec().
This writes only common fields. The order of the fields is not important. It is the responsibility of the programmer to ensure compatibility of field types and sizes to avoid errors while writing.


There must and will be one index field for matching and updating.

The programmer has to locate the record containing the matching index and then lock the record before calling FW_HashToRec() for updating.


There will also be some replacement done (ex. a standard value that is needed) just before the update in the source dbf field.

After reading the data from the source dbf into the hash, the programmer can modify and field value in the hash before updating the destination dbf.
Regards



G. N. Rao.

Hyderabad, India
Posts: 10733
Joined: Sun Nov 19, 2006 05:22 AM
Re: database oDbf
Posted: Sun May 16, 2021 02:55 PM
Marc Venken wrote:The Gatter() and scatter() functions are anno 2021 not preferred anymore right ?

The functions FW_DbfToArray() and FW_ArrayToDBF() work as Gather() and Scatter().
Hash functions are more flexible.

Gather() and Scatter() were originally used to editing records. TDataRow is better than these functions.
Regards



G. N. Rao.

Hyderabad, India
Posts: 1487
Joined: Tue Jun 14, 2016 07:51 AM
Re: database oDbf
Posted: Mon May 17, 2021 08:24 AM

After reading the data from the source dbf into the hash, the programmer can modify and field value in the hash before updating the destination dbf.


I looked at Hashes, and the look like arrays

Code (fw): Select all Collapse
   local aData:= {;
      { 'codigo' => 'uno',    'nombre' => 'Juan' },;
      { 'codigo' => 'dos',    'nombre' => 'Maria' },;
      { 'codigo' => 'tres',   'nombre' => 'Jose' },;
      { 'codigo' => 'cuatro', 'nombre' => 'Sonia' },;
      { 'nombre' => 'Pedro',  'codigo' => 'cinco' },;
      { 'edad'   => 18,       'codigo' => 'seis' };
   }

   XBROWSER aData FASTEDIT TITLE "ARRAY OF HASHES"


How do you edit 1 hash then ? I have them in a loop and let say I want to set the date as a value insite 1 element

The dbf field = datum (D)

hRec := data->( FW_RectoHash() )

For DBF we know..
data->datum = date()

So how to edit the hash element from that record and the field datum ?
Marc Venken

Using: FWH 23.08 with Harbour
Posts: 10733
Joined: Sun Nov 19, 2006 05:22 AM
Re: database oDbf
Posted: Mon May 17, 2021 12:28 PM
Code (fw): Select all Collapse
hRec := data->( FW_RectoHash() )
? hRec[ "datum" ]
hRec[ "datum" ] := date() + 20
(otheralias)->( FW_HashToRec( hRec ) )
Regards



G. N. Rao.

Hyderabad, India

Continue the discussion