Page 1 of 1

use fw_arraytodbf with conditions

Posted: Sat Sep 17, 2022 10:35 am
by Silvio.Falconi
I have an array previously built from a txt file, in this array as the first field there is a date field, I have to convert this array into a dbf file all records if the date field (dUltimaData) is greater than the last passed date

oTemp:= TDatabase():Open( , cPath+"LOTTO", "DBFCDX", .T. )
oTemp:gobottom()
dUltimaData:=oTemp:data


olotto2:= TDatabase():Open( , cPath+"STORICO", "DBFCDX", .T. )
SET DELETED ON
olotto2:setorder(0)
oLotto2:gotop()

//FW_ArrayToDBF( aData, cFieldList, bProgress, lOverWrite, lRecallDeleted, bTrigger )


oLotto2:fw_ArrayToDBF( aData,,bProgress) // how insert conditions ?

I need to create it because when I update the archive the old procedure deletes all the archive and re-insert all the records while it should only add the new records which then if the extraction is not updated is only one the records are only 11 and therefore the end user does not have to wait a long time to update the archive

I tried to create a new array with only the records need to me

oTemp:= TDatabase():Open( , cPath+"LOTTO", "DBFCDX", .T. )
oTemp:gobottom()
dUltimaData:=oTemp:data

For n= 1 to len(aData)
If ctod(aData[1][n]) > dUltimaData
AaDd(aNewData,{aData[1][n],;
aData[2][n],;
aData[3][n],;
aData[4][n],;
aData[5][n],;
aData[6][n],;
aData[7][n]})
Endif
Next

but make me error

Code: Select all | Expand

Error occurred at: 17-09-2022, 12:56:24
   Error description: Error BASE/1132  Bound error: array access
   Args:
     [   1] = A   {"07/01/1939","BA","58","22","47","49","69"} length: 7
     [   2] = N   8

 


wich is 8 ?

Re: use fw_arraytodbf with conditions

Posted: Sun Sep 18, 2022 1:17 pm
by nageswaragunupudi

Code: Select all | Expand

For n= 1 to len(aData)
If ctod(aData[1][n]) > dUltimaData
AaDd(aNewData,{aData[1][n],;
aData[2][n],;
aData[3][n],;
aData[4][n],;
aData[5][n],;
aData[6][n],;
aData[7][n]})
Endif
Next
 

It should be aData[ n, nCol ] or aData[ n ][ nCol ] but not aData[ nCol ][ n ]
When in the for loop "n" exceeds 7, you get this error.

Re: use fw_arraytodbf with conditions

Posted: Sun Sep 18, 2022 6:01 pm
by nageswaragunupudi
Inserting new records:

For testing, I have used a small portion of the "storico.txt"

We already have some data in STORICO.DBF and now we will try to insert new records.

Image

Code: Select all | Expand

#include "fivewin.ch"

REQUEST DBFCDX

function Main()

   local cText, aData, aNew, dMaxDate

   cText    := HB_MEMOREAD( "storico.txt" )
   cText    := FW_ALLTRIM( cText )
   cText    := StrTran( cText, CRLF, Chr( 10 ) )

   aData    := HB_ATokens( cText, Chr( 10 ) )

   // for purpose of testing we take a small portion of the Data
   ASIZE( aData, 24)
   //

   AEval( aData, { |c,i| aData[ i ] := HB_ATokens( c, Chr( 9 ) ) } )
   AEval( aData, { |a,i| a[ 1 ] := uCharToVal( a[ 1 ], "D" ) } )

   USE STORICO NEW VIA "DBFCDX" EXCLUSIVE
   XBROWSER ALIAS() SHOW RECID

   DBGOBOTTOM()
   dMaxDate := FIELD->DATA

   aNew     := {}
   AEval( aData, { |a| If( a[ 1 ] > dMaxDate, AAdd( aNew, a ), ) } )
   FW_ArrayToDbf( anew )

   XBROWSER ALIAS() SHOW RECID
   CLOSE DATA

return nil
 


Image

Re: use fw_arraytodbf with conditions

Posted: Mon Sep 19, 2022 7:10 am
by Silvio.Falconi
THANKS RAO

Re: use fw_arraytodbf with conditions - RESOLVED

Posted: Mon Sep 19, 2022 11:04 am
by Silvio.Falconi
Rao,
on my test I have the dMaxDate "10/09/2022"
and on storico.txt I arrive until 2022/09/17
when I execute

aNew := {}
AEval( aData, { |a| If( a[ 1 ] > dMaxDate, AAdd( aNew, a ), ) } )
oStorico:FW_ArrayToDbf( anew )

XBROWSER oStorico SHOW RECID

I not see any records

why ?

Re: use fw_arraytodbf with conditions

Posted: Tue Sep 20, 2022 8:17 am
by Silvio.Falconi
nageswaragunupudi wrote:Inserting new records:

For testing, I have used a small portion of the "storico.txt"

We already have some data in STORICO.DBF and now we will try to insert new records.

Image

Code: Select all | Expand

#include "fivewin.ch"

REQUEST DBFCDX

function Main()

   local cText, aData, aNew, dMaxDate

   cText    := HB_MEMOREAD( "storico.txt" )
   cText    := FW_ALLTRIM( cText )
   cText    := StrTran( cText, CRLF, Chr( 10 ) )

   aData    := HB_ATokens( cText, Chr( 10 ) )

   // for purpose of testing we take a small portion of the Data
   ASIZE( aData, 24)
   //

   AEval( aData, { |c,i| aData[ i ] := HB_ATokens( c, Chr( 9 ) ) } )
   AEval( aData, { |a,i| a[ 1 ] := uCharToVal( a[ 1 ], "D" ) } )

   USE STORICO NEW VIA "DBFCDX" EXCLUSIVE
   XBROWSER ALIAS() SHOW RECID

   DBGOBOTTOM()
   dMaxDate := FIELD->DATA

   aNew     := {}
   AEval( aData, { |a| If( a[ 1 ] > dMaxDate, AAdd( aNew, a ), ) } )
   FW_ArrayToDbf( anew )

   XBROWSER ALIAS() SHOW RECID
   CLOSE DATA

return nil
 


Image





Rao I have problem

give me a xbrowse empty

Please try this test

Code: Select all | Expand



#include "fivewin.ch"

REQUEST DBFCDX


FUNCTION Main()

   // PUBLIC oApp

   // HB_LangSelect("IT")
   * HB_SetCodePage("ITWIN")

   SetHandleCount( 100 )

   SET DATE FORMAT "dd-mm-yyyy"
   SET DELETED     ON
   SET CENTURY     ON
   SET EPOCH TO    year( date() ) - 20
   SET MULTIPLE    OFF


    Update()

   return nil

//-----------------------------------------------------//

function Update()

   local cText, aData,  aNew, dMaxDate, oLotto
   local aFields :=   { { "DATA" , "D", 8, 0 },;
                        { "RUOTA", "C", 2, 0 },;
                        { "N1"   , "N", 2, 0 },;
                        { "N2"   , "N", 2, 0 },;
                        { "N3"   , "N", 2, 0 },;
                        { "N4"   , "N", 2, 0 },;
                        { "N5"   , "N", 2, 0 }}

   cText    := HB_MEMOREAD( "storico.txt" )
   cText    := FW_ALLTRIM( cText )
   cText    := StrTran( cText, CRLF, Chr( 10 ) )

   aData    := HB_ATokens( cText, Chr( 10 ) )


   AEval( aData, { |c,i| aData[ i ] := HB_ATokens( c, Chr( 9 ) ) } )
   AEval( aData, { |a,i| a[ 1 ] := uCharToVal( a[ 1 ], "D" ) } )

   XBROWSER aData

    oLotto:= TDatabase():Open( , "LOTTO", "DBFCDX", .T. )
    oLotto:Gobottom()
    dMaxDate := oLotto:Data
    ?dMaxDate //TAKE LAST RECORD DATE

    //CREATE TEMP
   DBCREATE( "TEMP.DBF", aFields, "DBFCDX", .T., "Storico" )


   aNew     := {}
   AEval( aData, { |a| If( a[ 1 ] > dMaxDate, AAdd( aNew, a ), ) } )
   FW_ArrayToDbf( anew )


   GO TOP
   XBROWSER ALIAS() SHOW RECID
   CLOSE DATA

return nil