database oDbf

database oDbf

Postby Loren » Tue May 04, 2021 6: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: Select all  Expand view  RUN

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:
Loren
 
Posts: 479
Joined: Fri Feb 16, 2007 10:29 am
Location: Cadiz - España

Re: database oDbf

Postby nageswaragunupudi » Wed May 05, 2021 8:59 am

Code: Select all  Expand view  RUN

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

SELECT 2
DATABASE oDbf2
oDbf2:Append( aCopy )
 
Regards

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

Re: database oDbf

Postby nageswaragunupudi » Wed May 05, 2021 9:31 am

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

Code: Select all  Expand view  RUN

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

SELECT 2
FW_ArrayToDbf( aData )
 
Regards

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

Re: database oDbf

Postby nageswaragunupudi » Wed May 05, 2021 12:54 pm

Simplest way

Code: Select all  Expand view  RUN

SELECT 1
hRec := FW_RecToHash()

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

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

Re: database oDbf

Postby Marc Venken » Sun May 16, 2021 1: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.04 with Harbour
User avatar
Marc Venken
 
Posts: 1447
Joined: Tue Jun 14, 2016 7:51 am
Location: Belgium

Re: database oDbf

Postby Marc Venken » Sun May 16, 2021 1:32 pm

The Gatter() and scatter() functions are anno 2021 not preferred anymore right ?
Marc Venken
Using: FWH 23.04 with Harbour
User avatar
Marc Venken
 
Posts: 1447
Joined: Tue Jun 14, 2016 7:51 am
Location: Belgium

Re: database oDbf

Postby nageswaragunupudi » Sun May 16, 2021 2: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
User avatar
nageswaragunupudi
 
Posts: 10662
Joined: Sun Nov 19, 2006 5:22 am
Location: India

Re: database oDbf

Postby nageswaragunupudi » Sun May 16, 2021 2: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
User avatar
nageswaragunupudi
 
Posts: 10662
Joined: Sun Nov 19, 2006 5:22 am
Location: India

Re: database oDbf

Postby Marc Venken » Mon May 17, 2021 8: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: Select all  Expand view  RUN

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

Re: database oDbf

Postby nageswaragunupudi » Mon May 17, 2021 12:28 pm

Code: Select all  Expand view  RUN
hRec := data->( FW_RectoHash() )
? hRec[ "datum" ]
hRec[ "datum" ] := date() + 20
(otheralias)->( FW_HashToRec( hRec ) )
 
Regards

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


Return to FiveWin para Harbour/xHarbour

Who is online

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