How to copy a row of a rowset

How to copy a row of a rowset

Postby vilian » Wed Mar 25, 2020 8:54 pm

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
vilian
 
Posts: 957
Joined: Wed Nov 09, 2005 2:17 am
Location: Brazil

Re: How to copy a row of a rowset

Postby Adolfo » Thu Mar 26, 2020 2:39 pm

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
----------
Asus TUF F15, 32GB Ram, 2 * 1 TB NVME M.2, GTX 1650
User avatar
Adolfo
 
Posts: 852
Joined: Tue Oct 11, 2005 11:57 am
Location: Chile

Re: How to copy a row of a rowset

Postby vilian » Thu Mar 26, 2020 3:13 pm

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
vilian
 
Posts: 957
Joined: Wed Nov 09, 2005 2:17 am
Location: Brazil

Re: How to copy a row of a rowset

Postby leandro » Thu Mar 26, 2020 3:42 pm

Una idea

Code: Select all  Expand view

//RsToHash( oRs, [nRows], [nStart], [aFields] )
hPrueba := RsToHash( oRsFtr, 1, 1 )
xbrowse( hPrueba )
 
Saludos
LEANDRO AREVALO
Bogotá (Colombia)
https://hymlyma.com
https://hymplus.com/
leandroalfonso111@gmail.com
leandroalfonso111@hotmail.com

[ Embarcadero C++ 7.60 for Win32 ] [ FiveWin 23.07 ] [ xHarbour 1.3.0 Intl. (SimpLex) (Build 20230914) ]
User avatar
leandro
 
Posts: 1620
Joined: Wed Oct 26, 2005 2:49 pm
Location: Colombia

Re: How to copy a row of a rowset

Postby vilian » Thu Mar 26, 2020 4:41 pm

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
vilian
 
Posts: 957
Joined: Wed Nov 09, 2005 2:17 am
Location: Brazil

Re: How to copy a row of a rowset

Postby leandro » Thu Mar 26, 2020 6:24 pm

Otra idea :D
Code: Select all  Expand view

RsGetRows( oRs, [nRows], [nStart], [aFields] )
 
Saludos
LEANDRO AREVALO
Bogotá (Colombia)
https://hymlyma.com
https://hymplus.com/
leandroalfonso111@gmail.com
leandroalfonso111@hotmail.com

[ Embarcadero C++ 7.60 for Win32 ] [ FiveWin 23.07 ] [ xHarbour 1.3.0 Intl. (SimpLex) (Build 20230914) ]
User avatar
leandro
 
Posts: 1620
Joined: Wed Oct 26, 2005 2:49 pm
Location: Colombia

Re: How to copy a row of a rowset

Postby vilian » Thu Mar 26, 2020 7:32 pm

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
vilian
 
Posts: 957
Joined: Wed Nov 09, 2005 2:17 am
Location: Brazil

Re: How to copy a row of a rowset

Postby Marc Venken » Thu Mar 26, 2020 7:36 pm

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 :

viewtopic.php?f=3&t=32737
Marc Venken
Using: FWH 23.04 with Harbour
User avatar
Marc Venken
 
Posts: 1397
Joined: Tue Jun 14, 2016 7:51 am
Location: Belgium

Re: How to copy a row of a rowset

Postby vilian » Thu Mar 26, 2020 8:08 pm

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
vilian
 
Posts: 957
Joined: Wed Nov 09, 2005 2:17 am
Location: Brazil

Re: How to copy a row of a rowset

Postby leandro » Thu Mar 26, 2020 8:47 pm

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  Expand view

oRow := oRs:Fields
 
Saludos
LEANDRO AREVALO
Bogotá (Colombia)
https://hymlyma.com
https://hymplus.com/
leandroalfonso111@gmail.com
leandroalfonso111@hotmail.com

[ Embarcadero C++ 7.60 for Win32 ] [ FiveWin 23.07 ] [ xHarbour 1.3.0 Intl. (SimpLex) (Build 20230914) ]
User avatar
leandro
 
Posts: 1620
Joined: Wed Oct 26, 2005 2:49 pm
Location: Colombia

Re: How to copy a row of a rowset

Postby betoncu » Fri Mar 27, 2020 5:47 am

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
betoncu
 
Posts: 126
Joined: Sat Oct 08, 2005 9:38 pm
Location: Cyprus (North)

Re: How to copy a row of a rowset

Postby nageswaragunupudi » Fri Mar 27, 2020 9:29 pm

Code: Select all  Expand view

aRows := oRs:GetRows( 1, oRs:KeyNo )
 

This gives a two-dimensional array with a single row
Code: Select all  Expand view

{ { 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  Expand view

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

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

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

Postby vilian » Fri Mar 27, 2020 11:23 pm

Thank you !
Sds,
Vilian F. Arraes
vilian@vfatec.com.br
Belém-Pa-Brazil
User avatar
vilian
 
Posts: 957
Joined: Wed Nov 09, 2005 2:17 am
Location: Brazil

Re: How to copy a row of a rowset

Postby alvaro533 » Tue Mar 31, 2020 10:02 am

nageswaragunupudi wrote:
Code: Select all  Expand view

aRows := oRs:GetRows( 1, oRs:KeyNo )
 

This gives a two-dimensional array with a single row
Code: Select all  Expand view

{ { 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  Expand view

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  Expand view

   aRows := oRs:GetRows(  )
   aStru := aclone( oRs:aStructure )
   aeval( aStru, { | aAr , n | iif( aAr[2]=='+' , aAr[2]:='N',nil)  } )
   oData := TArrayData():New( aRows, aStru )
 


Regards
alvaro533
 
Posts: 206
Joined: Sat Apr 19, 2008 10:28 pm
Location: Madrid, España

Re: How to copy a row of a rowset

Postby nageswaragunupudi » Tue Mar 31, 2020 10:42 am

ok thanks
Regards

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


Return to FiveWin for Harbour/xHarbour

Who is online

Users browsing this forum: No registered users and 42 guests