database oDbf

Post Reply
Loren
Posts: 479
Joined: Fri Feb 16, 2007 10:29 am
Location: Cadiz - España

database oDbf

Post by Loren »

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: Select all | Expand


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:
User avatar
nageswaragunupudi
Posts: 10729
Joined: Sun Nov 19, 2006 5:22 am
Location: India
Been thanked: 10 times
Contact:

Re: database oDbf

Post by nageswaragunupudi »

Code: Select all | Expand


SELECT 1
DATABASE oDbf1
aCopy := oDbf1:Copy()

SELECT 2
DATABASE oDbf2
oDbf2:Append( aCopy )
 
Regards

G. N. Rao.
Hyderabad, India
User avatar
nageswaragunupudi
Posts: 10729
Joined: Sun Nov 19, 2006 5:22 am
Location: India
Been thanked: 10 times
Contact:

Re: database oDbf

Post by nageswaragunupudi »

Another method, without the overhead of creating 2 TDatabase objects.

Code: Select all | Expand


SELECT 1
aData := FW_DBFToArray( nil, nil, nil, 1 )

SELECT 2
FW_ArrayToDbf( aData )
 
Regards

G. N. Rao.
Hyderabad, India
User avatar
nageswaragunupudi
Posts: 10729
Joined: Sun Nov 19, 2006 5:22 am
Location: India
Been thanked: 10 times
Contact:

Re: database oDbf

Post by nageswaragunupudi »

Simplest way

Code: Select all | Expand


SELECT 1
hRec := FW_RecToHash()

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

G. N. Rao.
Hyderabad, India
User avatar
Marc Venken
Posts: 1485
Joined: Tue Jun 14, 2016 7:51 am
Location: Belgium

Re: database oDbf

Post by Marc Venken »

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
User avatar
Marc Venken
Posts: 1485
Joined: Tue Jun 14, 2016 7:51 am
Location: Belgium

Re: database oDbf

Post by Marc Venken »

The Gatter() and scatter() functions are anno 2021 not preferred anymore right ?
Marc Venken
Using: FWH 23.08 with Harbour
User avatar
nageswaragunupudi
Posts: 10729
Joined: Sun Nov 19, 2006 5:22 am
Location: India
Been thanked: 10 times
Contact:

Re: database oDbf

Post by nageswaragunupudi »

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
User avatar
nageswaragunupudi
Posts: 10729
Joined: Sun Nov 19, 2006 5:22 am
Location: India
Been thanked: 10 times
Contact:

Re: database oDbf

Post by nageswaragunupudi »

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
User avatar
Marc Venken
Posts: 1485
Joined: Tue Jun 14, 2016 7:51 am
Location: Belgium

Re: database oDbf

Post by Marc Venken »

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: Select all | Expand


   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
User avatar
nageswaragunupudi
Posts: 10729
Joined: Sun Nov 19, 2006 5:22 am
Location: India
Been thanked: 10 times
Contact:

Re: database oDbf

Post by nageswaragunupudi »

Code: Select all | Expand

hRec := data->( FW_RectoHash() )
? hRec[ "datum" ]
hRec[ "datum" ] := date() + 20
(otheralias)->( FW_HashToRec( hRec ) )
 
Regards

G. N. Rao.
Hyderabad, India
Post Reply