FiveTech Support Forums

FiveWin / Harbour / xBase community
Board index FiveWin for Harbour/xHarbour MARAIDB ROWSET ONE RECORD TO COVERT INTO COLUMNS
Posts: 76
Joined: Fri Aug 28, 2009 05:25 AM
MARAIDB ROWSET ONE RECORD TO COVERT INTO COLUMNS
Posted: Wed May 24, 2023 10:40 AM
Dear Rao Sir ,

As we convert SQL result set from rows to columns. Is there any way to convert MariaDB Rowset into columns. for the given below examples:
Code (fw): Select all Collapse
Original  RowSet Result :
 col_name     col_value 
 ------------     ---------------
 name            Suresh
 age               22
 dob               01/01/2021 

AFTER CONVERTING 
name         age             dob
========  ========     ==========
Suresh      22                01/01/2021
Thanks
Shridhar
Posts: 10733
Joined: Sun Nov 19, 2006 05:22 AM
Re: MARAIDB ROWSET ONE RECORD TO COVERT INTO COLUMNS
Posted: Wed May 24, 2023 03:08 PM
We need to start with this table.
Code (fw): Select all Collapse
Original  RowSet Result :
 col_name     col_value
 ------------     ---------------
 name            Suresh
 age               22
 dob               01/01/2021
Let is first create this table and keep it ready for actual processing.
We will use FW's cloud server which is available for all of us.
Code (fw): Select all Collapse
#include "fivewin.ch"

function Main()

   local oCn

   oCn   := maria_Connect( "208.91.198.197:3306","fwhdemo", "gnraofwh", "Bharat@1950" )

   oCn:Droptable( "tbl_struct" )
   oCn:CreateTable( "tbl_struct", { { "col_name", "C", 20, 0 }, { "col_value", "C", 20, 0 } }, .f. )
   oCn:Insert( "tbl_struct", nil, {{ "name","suresh"}, {"age",22},{"dob","01/01/2001"}} )

   XBROWSER oCn:tbl_struct TITLE "tbl_struct"
   oCn:Close()
   
return nil


Now let us create the final table from the data in the first table "tbl_struct"
Code (fw): Select all Collapse
#include "fivewin.ch"

function Main()

   local oCn
   local aData
   local aStruct := {}
   local aRowData := {}

   oCn   := maria_Connect( "208.91.198.197:3306","fwhdemo", "gnraofwh", "Bharat@1950" )

   aData    := oCn:Execute( "select * from tbl_struct" )
   AEval( aData, { |a,i| AAdd( aStruct, { a[ 1 ], "C", 20, 0 } ), ;
                         AAdd( aRowData, a[ 2 ] ) } )

   oCn:DropTable( "tbl_data" )
   oCn:CreateTable( "tbl_data", aStruct, .f. )
   oCn:Insert( "tbl_data", nil, aRowData )

   XBROWSER oCn:tbl_data TITLE "tbl_data"

   oCn:Close()

return nil
Regards



G. N. Rao.

Hyderabad, India
Posts: 76
Joined: Fri Aug 28, 2009 05:25 AM
Re: MARAIDB ROWSET ONE RECORD TO COVERT INTO COLUMNS
Posted: Tue May 30, 2023 03:28 AM

Dear Rao Sir ,

Thanks a lot...!

Thanks

Shridhar

Continue the discussion