Page 1 of 1

How to copy a row of a rowset

Posted: Wed Mar 25, 2020 8:54 pm
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 ?

Re: How to copy a row of a rowset

Posted: Thu Mar 26, 2020 2:39 pm
by Adolfo
Simple, use MYSQL

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

Hope it helps
From Chile
Adolfo

Re: How to copy a row of a rowset

Posted: Thu Mar 26, 2020 3:13 pm
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.

Re: How to copy a row of a rowset

Posted: Thu Mar 26, 2020 3:42 pm
by leandro
Una idea

Code: Select all

//RsToHash( oRs, [nRows], [nStart], [aFields] ) 
hPrueba := RsToHash( oRsFtr, 1, 1 ) 
xbrowse( hPrueba )
 

Re: How to copy a row of a rowset

Posted: Thu Mar 26, 2020 4:41 pm
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.

Re: How to copy a row of a rowset

Posted: Thu Mar 26, 2020 6:24 pm
by leandro
Otra idea :D

Code: Select all

RsGetRows( oRs, [nRows], [nStart], [aFields] )
 

Re: How to copy a row of a rowset

Posted: Thu Mar 26, 2020 7:32 pm
by vilian
It's also returning an array and I wanted a object

Re: How to copy a row of a rowset

Posted: Thu Mar 26, 2020 7:36 pm
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

Re: How to copy a row of a rowset

Posted: Thu Mar 26, 2020 8:08 pm
by vilian
Thanks,
I already read this topic and did not find what i need.

Re: How to copy a row of a rowset

Posted: Thu Mar 26, 2020 8:47 pm
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
 

Re: How to copy a row of a rowset

Posted: Fri Mar 27, 2020 5:47 am
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

Re: How to copy a row of a rowset

Posted: Fri Mar 27, 2020 9:29 pm
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 )
 

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

Posted: Fri Mar 27, 2020 11:23 pm
by vilian
Thank you !

Re: How to copy a row of a rowset

Posted: Tue Mar 31, 2020 10:02 am
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

Re: How to copy a row of a rowset

Posted: Tue Mar 31, 2020 10:42 am
by nageswaragunupudi
ok thanks