Page 1 of 1

tdatabase arraytodbf

Posted: Sun Feb 03, 2019 9:09 am
by Silvio.Falconi
there is a command to rewrite dbf from array into tdatabase class ^?

Re: tdatabase arraytodbf

Posted: Sun Feb 03, 2019 3:53 pm
by ukoenig
tested : read from backup and write to original

1. read backup-data to array
2. zap original dbf
3. write backup-data to original

Code: Select all | Expand


// ---------------------- restore data from original DBF ( backup )----------

FUNCTION NET_RESTORE3(oBrw)
LOCAL  lReturn := .F., aData := {}

PACKZAP_1(2) // zap original dbf

// original data
USE CUST_ORG NEW ALIAS "CUSTNEW" SHARED VIA "DBFCDX"
oCustNew    := TDataBase():New( Select( "CUSTNEW" ) )

// Read required data into array
cList    := "*,RECNO()"
aData    := oCustNew:FW_DbfToArray( @cList )  
aNew     := Array( oCustNew:FCount() + 1 )
AEval( aNew, { |u,i| aNew[ i ] := uValBlank( oCustNew:FieldGet( i ) ) }, 1, oCustNew:FCount() )
nRecNoCol:= Len( aNew )
aNew[ nRecNoCol ] := 0
// Read done close backup
oCustNew:Close()

// write new data to customer.dbf from backup
WITH OBJECT oBrw:oDbf
    oCust:FW_ArrayToDbf( aData )
END

oCust:GoTop()
oBrw:SetoDbf(oCust)
oBrw:Refresh()

MSGINFO("Original data restored !")

RETURN (lReturn)

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

FUNCTION PACKZAP_1(nStyle)

IF oCust:lShared() = .T.
    oCust:Close()
    oCust := TCustomers():New(.F.) // exclusive
    oCust:use()
    // MSGINFO( oCust:lShared(),"Shared")  
ENDIF

IF oCust:Used()
    IF nStyle = 1
        oCust:Pack()
        MSGINFO("Done Pack")
    ELSE
        oCust:Zap()
        MSGINFO("Done Zap")
    ENDIF
ENDIF

oCust:Close()

oCust := TCustomers():New(.T.) // back to shared
oCust:use()

RETURN( NIL)
 


regards
Uwe :D

Re: tdatabase arraytodbf

Posted: Sun Feb 03, 2019 4:13 pm
by Silvio.Falconi
ukoenig wrote:tested : read from backup and write to original

1. read backup-data to array
2. zap original dbf
3. write backup-data to original

Code: Select all | Expand


// ---------------------- restore data from original DBF ( backup )----------

FUNCTION NET_RESTORE3(oBrw)
LOCAL  lReturn := .F., aData := {}

PACKZAP_1(2) // zap original dbf

// original data
USE CUST_ORG NEW ALIAS "CUSTNEW" SHARED VIA "DBFCDX"
oCustNew    := TDataBase():New( Select( "CUSTNEW" ) )

// Read required data into array
cList    := "*,RECNO()"
aData    := oCustNew:FW_DbfToArray( @cList )  
aNew     := Array( oCustNew:FCount() + 1 )
AEval( aNew, { |u,i| aNew[ i ] := uValBlank( oCustNew:FieldGet( i ) ) }, 1, oCustNew:FCount() )
nRecNoCol:= Len( aNew )
aNew[ nRecNoCol ] := 0
// Read done close backup
oCustNew:Close()

// write new data to customer.dbf from backup
WITH OBJECT oBrw:oDbf
    oCust:FW_ArrayToDbf( aData )
END

oCust:GoTop()
oBrw:SetoDbf(oCust)
oBrw:Refresh()

MSGINFO("Original data restored !")

RETURN (lReturn)

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

FUNCTION PACKZAP_1(nStyle)

IF oCust:lShared() = .T.
    oCust:Close()
    oCust := TCustomers():New(.F.) // exclusive
    oCust:use()
    // MSGINFO( oCust:lShared(),"Shared")  
ENDIF

IF oCust:Used()
    IF nStyle = 1
        oCust:Pack()
        MSGINFO("Done Pack")
    ELSE
        oCust:Zap()
        MSGINFO("Done Zap")
    ENDIF
ENDIF

oCust:Close()

oCust := TCustomers():New(.T.) // back to shared
oCust:use()

RETURN( NIL)
 


regards
Uwe :D


1. I must open a dbf with tdatabase on share mode.
2. The index have For !deleted()
3 I cannot make pack and zap
4. I can only delete all records.

My aData atray can be of 450,800, and over records.
It can be too hard to save.

Re: tdatabase arraytodbf

Posted: Sun Feb 03, 2019 4:24 pm
by ukoenig
Silvio,

what happens with deleted records :?:
after deleting records normally You need a pack
or do You want to clear the fields
or keep them marked as deleted adding new data from the array :?:

just delete PACKZAP_1(2) // zap original dbf
and the array-data will be added to the original dbf and keeps the original data

I deleted some records from the original
and added the data as array from the backup ( same like the original )
like You can see there are double data now.
red is deleted and new from the array-import ( added )
just see the recno - number

Code: Select all | Expand

3 I cannot make pack and zap
4. I can only delete all records.


Image

regards
Uwe