Copying and Pasting Records

Copying and Pasting Records

Postby nageswaragunupudi » Sat Feb 17, 2018 10:59 pm

There can be occassions when we need to copy a record, either partly or fully, from a table and paste the values in some other record of the same table or another table or append the values as a new record. It is also possible that source and destination databases can be different, eg. copy some values from a DBF and paste (or insert) into a table in MySql database. The situations can vary and different situations require coding specific to those requirements.

FWH makes any such job very easy by providing these functions:

FW_CopyRecord( [uSource = Alias()], [cFieldlist = All] ) --> hRec (hash)
FW_PasteRecrod( [uDest = Alias()], [hRec], [lAppend = .f.] ) --> lSuccess
FW_EditHash( hHash )

uSource and uDest can be Alias(), RecordSet, RowSet, Qry of any Database or even an active XBrowse. If omitted, defauts to current Alias().

FW_CopyRecord() returns a Hash with fieldnames and value. FWH also retains a copy of the hash in its internal memory. If the parameter hRec is omitted in the next call to FW_PasteRecord(), the hash in the memory is used.

When pasting the values are pasted to the corresponding fields in the destination. So, it is not necessary that the fields should be in the same order in the source and destination.

AutoIncrement and DateStamp fields: Paste operation provides safety by not over-writing auto-increment and datestamp fields.

However it is the reponsibility of the programmer to ensure that the source and destination fields have the same data type and widths and also values pasted respect the data constraints of the destination table (eg: Unique values should not be repeated). For this purpose, the programmer can change the values of some fields or even the field names after copying by modifying the Hash.

If required, programmer can use the handy utility FW_EditHash( hHash ) to let the user edit the values before pasting.

Some Examples:
Code: Select all  Expand view

// Same DBF
FW_CopyRecord()
DBGOTO( 100 )
FW_PasteRecord()
// or
FW_PasteRecord( nil, nil, .T. )
//-------------------
hRec := FW_CopyRecord( oBrw, "ID,NAME,AGE" )
hRec[ "ID" ] := "0234"
FW_PasteRecord( oRecordSet, hRec, .T. )
 


We hope these three functions cover every possible requirement for copying and pasting records from one table to the same or another table of any database.
Regards

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

Re: Copying and Pasting Records

Postby dutch » Sun Feb 18, 2018 7:06 am

Thanks, Mr.Rao.
Great and make use more easier.
Regards,
Dutch
Regards,
Dutch

FWH 19.01 / xHarbour Simplex 1.2.3 / BCC73 / Pelles C / UEStudio
FWPPC 10.02 / Harbour for PPC (FTDN)
ADS V.9 / MySql / MariaDB
R&R 12 Infinity / Crystal Report XI R2
(Thailand)
User avatar
dutch
 
Posts: 1535
Joined: Fri Oct 07, 2005 5:56 pm
Location: Thailand

Re: Copying and Pasting Records

Postby Marc Venken » Sun Feb 18, 2018 11:20 am

Thanks.

If we want to use a condition like ex. (copy all records from source to target) where

State = "US"

Is there a small sample for dbf and xBrowse (Xbrowse also if there is a selection done)

I would do something like this, but often you guys do it in 1 or 2 lines ))

use cust NEW
copy structure to temp
use temp new

do while !cust->(eof())
if cust->state = "US"
hRec := FW_CopyRecord( cust , "ID,NAME,AGE" )
hRec[ "ID" ] := "0234"
FW_PasteRecord( temp, hRec, .T. )
endif
cust->(dbskip())
enddo
close all
Marc Venken
Using: FWH 23.04 with Harbour
User avatar
Marc Venken
 
Posts: 1338
Joined: Tue Jun 14, 2016 7:51 am
Location: Belgium

Re: Copying and Pasting Records

Postby nageswaragunupudi » Sun Feb 18, 2018 11:50 am

Mr Marc

That is a separate topic and FWH provides powerful functions to do what you want.

Please see documentation on
FW_AdoImportFromDBF()
and
FWMARIADB
oCn:ImportFromDBF()

These functions provide for all possible options and requirements
Regards

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


Return to FiveWin for Harbour/xHarbour

Who is online

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