Page 1 of 1

dbf to array

Posted: Tue Dec 25, 2007 8:02 am
by Otto
What is the best way to fill an array from a dbf-file (a kind of recordset).

Thanks in advance
Otto

Posted: Tue Dec 25, 2007 9:59 am
by demont frank
I am using :

Code: Select all

# include "Common.ch"
# DEFINE MAAKBLCK(cvar)               &("{||"+cvar+"}")
# DEFINE MAAKINDBLCK(var,indexorde)   var := &("{||"+INDEXKEY(indexorde)+"}")
*********************************************************************************************************
PROC ReadDbf(Arr,ArrColBlock,cFilter, uFrom , uTo , cOrder  )
***************************************************************
// ArrColBlock may be nil (all fields are selected) , array from fieldnames or Codeblocks
LOCAL bFilt , IndBlck , HlpArr[0] , el
LOCAL j
LOCAL GehOrd
LOCAL Hlp
LOCAL aStruct := DbStruct() , aFields[0]
Aeval(aStruct , {|x|AADD(aFields,x[1])})
CursorWait()
Arr := {}
IF IsCharacter(cFilter)
	bFilt := MAAKBLCK(cFilter)
ELSEIF IsBlock(cFilter)
	bFilt := cFilter
END
IF IsNil(ArrColBlock)  .OR. ! IsArray(ArrColBlock)
	ArrColBlock := {}
	FOR j := 1 TO FCOUNT()
		AADD(ArrColBlock,FIELDBLOCK(FIELD(j)))
	NEXT
END
FOR EACH el IN ArrColBlock
	IF IsCharacter(el) .AND. ASCAN(aFields, UPPER(el)) > 0
		el := FIELDBLOCK(el)
	END
	IF ! IsBlock(el)
		Msginfo("Wrong column " + LTRIM(STR(Hb_EnumIndex()))
		RETURN
	END
NEXT
IF (IsCharacter(cOrder) .OR. IsNumber(cOrder)) .AND. OrdSetFocus() <> cOrder
	GehOrd := OrdSetFocus(cOrder)
END
IF IndexOrd() > 0
	MAAKINDBLCK(IndBlck,indexord())
	IF ! IsNil(uFrom)
		IF IsBlock(uFrom)
			uFrom := EVAL(uFrom)
		END
		DBSEEK(uFrom,.T.)
	ELSE
		GO TOP
	END
	IF ! IsNil(uTo)
		IF IsBlock(uTo)
			uTo := EVAL(uTo)
		END
	END
ELSE
	IF ! IsNil(uFrom) .AND. IsNumber(uFrom)
		IndBlck := {||RECNO()}
		IF !( !IsNil(uTo) .AND. IsNumber(uTo) )
			uTo := LASTREC()
		END
		DBGOTO(uFrom)
	ELSE
		GO TOP
	END
END
DO WHIL ! EOF() .AND. IIF(! IsNil(uTo) .AND. IsBlock(IndBlck), EVAL(indBlck) <= uTo , .T. )
	IF ! IsBlock(bFilt) .OR. EVAL(bFilt)
		ASIZE(hlparr,0)
		FOR EACH el IN ArrColBlock
			hlp := EVAL(el)
			AADD(hlparr , Hlp) //EVAL(el:bData))
		NEXT
		AADD(arr,ACLONE(hlparr))
	END
	SKIP
ENDD
IF ! IsNil(GehOrd)
	OrdSetFocus(GehOrd)
END
CursorArrow()
RETURN


Re: dbf to array

Posted: Tue Dec 25, 2007 10:12 am
by Enrico Maria Giordano

Code: Select all

FUNCTION MAIN()

    LOCAL aArray := {}

    LOCAL i

    USE TEST

    WHILE !EOF()
        AADD( aArray, {} )

        FOR i = 1 TO FCOUNT()
            AADD( ATAIL( aArray ), FIELDGET( i ) )
        NEXT

        SKIP
    ENDDO

    CLOSE

    // here you have the filled array

    RETURN NIL
EMG

Re: dbf to array

Posted: Tue Dec 25, 2007 10:13 am
by Enrico Maria Giordano
But if you really want a sort of recordset then you should create a class.

EMG