How to copy a row of a rowset

Post Reply
User avatar
vilian
Posts: 795
Joined: Wed Nov 09, 2005 2:17 am
Location: Brazil
Contact:

How to copy a row of a rowset

Post by vilian »

Hi Guys,

I have a rowset oRs that have a lot of rows.
I wanted to copy only one row/record of oRs to a new rowset(oRL). Is It possible ?
Sds,
Vilian F. Arraes
vilian@vfatec.com.br
Belém-Pa-Brazil
User avatar
Adolfo
Posts: 815
Joined: Tue Oct 11, 2005 11:57 am
Location: Chile
Contact:

Re: How to copy a row of a rowset

Post by Adolfo »

Simple, use MYSQL

INSERT INTO clientes SELECT * FROM personas WHERE id='42431-01'

Hope it helps
From Chile
Adolfo
;-) Ji,ji,ji... buena la cosa... "all you need is code"

http://www.xdata.cl - Desarrollo Inteligente
----------
Lenovo Legion Y520, 16GB Ram, 1 TB NVME M.2, 1 TB SSD, GTX 1050
User avatar
vilian
Posts: 795
Joined: Wed Nov 09, 2005 2:17 am
Location: Brazil
Contact:

Re: How to copy a row of a rowset

Post by vilian »

Thank you,

But I don't want insert a new record. I want create a new rowset from a line of previous rowset.
Sds,
Vilian F. Arraes
vilian@vfatec.com.br
Belém-Pa-Brazil
User avatar
leandro
Posts: 958
Joined: Wed Oct 26, 2005 2:49 pm
Location: Colombia
Contact:

Re: How to copy a row of a rowset

Post by leandro »

Una idea

Code: Select all

//RsToHash( oRs, [nRows], [nStart], [aFields] ) 
hPrueba := RsToHash( oRsFtr, 1, 1 ) 
xbrowse( hPrueba )
 
Saludos
LEANDRO ALFONSO
SISTEMAS LYMA - BASE
Bogotá (Colombia)
[ FWH 19.09 ] [ xHarbour 1.2.3 Intl. (SimpLex) (Build 20190613) ] [ Embarcadero C++ 7.30 for Win32 ]
User avatar
vilian
Posts: 795
Joined: Wed Nov 09, 2005 2:17 am
Location: Brazil
Contact:

Re: How to copy a row of a rowset

Post by vilian »

Thanks Leandro,
It's almost what I wanted. But in your code you are creating a hash and I wanted created a new rowset.
Sds,
Vilian F. Arraes
vilian@vfatec.com.br
Belém-Pa-Brazil
User avatar
leandro
Posts: 958
Joined: Wed Oct 26, 2005 2:49 pm
Location: Colombia
Contact:

Re: How to copy a row of a rowset

Post by leandro »

Otra idea :D

Code: Select all

RsGetRows( oRs, [nRows], [nStart], [aFields] )
 
Saludos
LEANDRO ALFONSO
SISTEMAS LYMA - BASE
Bogotá (Colombia)
[ FWH 19.09 ] [ xHarbour 1.2.3 Intl. (SimpLex) (Build 20190613) ] [ Embarcadero C++ 7.30 for Win32 ]
User avatar
vilian
Posts: 795
Joined: Wed Nov 09, 2005 2:17 am
Location: Brazil
Contact:

Re: How to copy a row of a rowset

Post by vilian »

It's also returning an array and I wanted a object
Sds,
Vilian F. Arraes
vilian@vfatec.com.br
Belém-Pa-Brazil
User avatar
Marc Venken
Posts: 727
Joined: Tue Jun 14, 2016 7:51 am

Re: How to copy a row of a rowset

Post by Marc Venken »

nTotal := oCn:QueryResult( "SELECT SUM(SALARY) FROM CUSTOMER" )



Read field values of a row into an array
CODE: SELECT ALL EXPAND VIEW

cList := "ID,FIRST,LAST,CITY,SALARY"
aRow := oCn:QueryResult( "SELECT " + cList + " FROM CUSTOMER WHERE ID = 100" )


Copy the same values to another row at ID = 150:
CODE: SELECT ALL EXPAND VIEW

// modify/edit any values of the aRow
aRow[ 1 ] := 150 // do not change for saving to same row
oCn:Insert( "customer", cList, aRow, .t. ) // .T. indicates update if primary key exists


Copy present values of a row and append as new record
CODE: SELECT ALL EXPAND VIEW

aRow := oCn:QueryResult( "SELECT * FROM CUSTOMER WHERE ID = 100" )
aRow[ 1 ] := 0
oCn:Insert( "customer", nil, aRow ) // fwh 16.11. aFields can be nil

Maybe this can help : Examples from this link :

http://forums.fivetechsupport.com/viewt ... =3&t=32737
Marc Venken
Using: FWH 20.08 with Harbour
User avatar
vilian
Posts: 795
Joined: Wed Nov 09, 2005 2:17 am
Location: Brazil
Contact:

Re: How to copy a row of a rowset

Post by vilian »

Thanks,
I already read this topic and did not find what i need.
Sds,
Vilian F. Arraes
vilian@vfatec.com.br
Belém-Pa-Brazil
User avatar
leandro
Posts: 958
Joined: Wed Oct 26, 2005 2:49 pm
Location: Colombia
Contact:

Re: How to copy a row of a rowset

Post by leandro »

En ese orden de ideas, ya tienes el objeto, lo único que hay que hacer es ubicarse sobre el registro a leer y llamar la linea completa, así:

Code: Select all

oRow := oRs:Fields
 
Saludos
LEANDRO ALFONSO
SISTEMAS LYMA - BASE
Bogotá (Colombia)
[ FWH 19.09 ] [ xHarbour 1.2.3 Intl. (SimpLex) (Build 20190613) ] [ Embarcadero C++ 7.30 for Win32 ]
User avatar
betoncu
Posts: 120
Joined: Sat Oct 08, 2005 9:38 pm
Location: Cyprus (North)

Re: How to copy a row of a rowset

Post by betoncu »

oCurrentRs := oCn:Query( "SELECT * FROM CUSTOMER" )

oNewRow := oCn:Query( "SELECT * FROM CUSTOMER WHERE ID < 0" ) //Get an empty row (There is no customer has a negative id)

FOR nI=1 TO oCurrentRs:FCount()
oNewRow:FieldPut(nI, oCurrentRs:FieldGet(nI))
NEXT
Birol Betoncu
birol.betoncu@gmail.com
Using Harbour, FWH 19.05, BCC7
User avatar
nageswaragunupudi
Posts: 8017
Joined: Sun Nov 19, 2006 5:22 am
Location: India
Contact:

Re: How to copy a row of a rowset

Post by nageswaragunupudi »

Code: Select all

aRows := oRs:GetRows( 1, oRs:KeyNo )
 
This gives a two-dimensional array with a single row

Code: Select all

{ { col1value, col2value, ... colNvalue } }
 
If you want to insert the data into another existing RowSet with identical structure, you can do it using oRs2:AddNew( aFields, aRows[ 1 ] ) }
But you can not create a new rowset in memory with this data.
A RowSet can be created ONLY by reading data from the MySql database via an sql query.

If you want to create an object, which works similary, holding this data, you can create TArrayData object

Code: Select all

oData := TArrayData():New( aRows, oRs:aStructure )
 
Regards

G. N. Rao.
Hyderabad, India
User avatar
vilian
Posts: 795
Joined: Wed Nov 09, 2005 2:17 am
Location: Brazil
Contact:

Re: How to copy a row of a rowset(solved)

Post by vilian »

Thank you !
Sds,
Vilian F. Arraes
vilian@vfatec.com.br
Belém-Pa-Brazil
alvaro533
Posts: 179
Joined: Sat Apr 19, 2008 10:28 pm
Location: Madrid, España

Re: How to copy a row of a rowset

Post by alvaro533 »

nageswaragunupudi wrote:

Code: Select all

aRows := oRs:GetRows( 1, oRs:KeyNo )
 
This gives a two-dimensional array with a single row

Code: Select all

{ { col1value, col2value, ... colNvalue } }
 
If you want to insert the data into another existing RowSet with identical structure, you can do it using oRs2:AddNew( aFields, aRows[ 1 ] ) }
But you can not create a new rowset in memory with this data.
A RowSet can be created ONLY by reading data from the MySql database via an sql query.

If you want to create an object, which works similary, holding this data, you can create TArrayData object

Code: Select all

oData := TArrayData():New( aRows, oRs:aStructure )
 
Good morning,

In oRs:aStructure the field type for the “primary key” is “+” instead of “N”, so the “primary key” value is not copied properly in TdataArray

The solution is either modify TdataArray and add ‘+” as a field type, like “N”, “D”, “C”, etc. or put this in your code.

Code: Select all

   aRows := oRs:GetRows(  )
   aStru := aclone( oRs:aStructure )
   aeval( aStru, { | aAr , n | iif( aAr[2]=='+' , aAr[2]:='N',nil)  } )
   oData := TArrayData():New( aRows, aStru )
 
Regards
User avatar
nageswaragunupudi
Posts: 8017
Joined: Sun Nov 19, 2006 5:22 am
Location: India
Contact:

Re: How to copy a row of a rowset

Post by nageswaragunupudi »

ok thanks
Regards

G. N. Rao.
Hyderabad, India
Post Reply