dbf to array

dbf to array

Postby Otto » Tue Dec 25, 2007 8:02 am

What is the best way to fill an array from a dbf-file (a kind of recordset).

Thanks in advance
Otto
User avatar
Otto
 
Posts: 6327
Joined: Fri Oct 07, 2005 7:07 pm

Postby demont frank » Tue Dec 25, 2007 9:59 am

I am using :

Code: Select all  Expand view
# 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

demont frank
 
Posts: 167
Joined: Thu Mar 22, 2007 11:24 am

Re: dbf to array

Postby Enrico Maria Giordano » Tue Dec 25, 2007 10:12 am

Code: Select all  Expand view
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
User avatar
Enrico Maria Giordano
 
Posts: 8710
Joined: Thu Oct 06, 2005 8:17 pm
Location: Roma - Italia

Re: dbf to array

Postby Enrico Maria Giordano » Tue Dec 25, 2007 10:13 am

But if you really want a sort of recordset then you should create a class.

EMG
User avatar
Enrico Maria Giordano
 
Posts: 8710
Joined: Thu Oct 06, 2005 8:17 pm
Location: Roma - Italia


Return to FiveWin for Harbour/xHarbour

Who is online

Users browsing this forum: No registered users and 62 guests