Get record in an array 
Author Message
 Get record in an array

I think that few days ago someone asked about way to put whole record
from database in array. I can't find original posting :), but here is
the answer.

This can be done in two steps - first one is to get informations about
database structure using Clipper's function dbStructure() (introduced in
CA-Clipper 5.2, this function is prefered replacement for good old
AFields()). Second step is to retrive data from database for each field
by using macro operator (this is one of the last cases where code blocks
still can't replace macros) to evaluate field contents.

Here is the code for function called GetRecord() which returns array
with record contents (from the curent record in selected database):

#include "dbStruct.ch"

FUNCTION GetRecord  ()

    Local aRecord, aStruct
    Local i, cFldName, uFldValue

    aRecord := {}
    aStruct := dbStruct()

    FOR i := 1 TO Len(aStruct)
        cFldName  := aStruct[i, DBS_NAME]
        uFldValue := &cFldName
        AAdd (aRecord, { cFldName, uFldValue })
    NEXT

RETURN aRecord

-= Jovan Bulajic =-
Belgrade,Yugoslavia



Mon, 21 Dec 1998 03:00:00 GMT  
 Get record in an array

Quote:

> I think that few days ago someone asked about way to put whole record
> from database in array. I can't find original posting :), but here is
> the answer.

> This can be done in two steps - first one is to get informations about
> database structure using Clipper's function dbStructure() (introduced in
> CA-Clipper 5.2, this function is prefered replacement for good old
> AFields()). Second step is to retrive data from database for each field
> by using macro operator (this is one of the last cases where code blocks
> still can't replace macros) to evaluate field contents.

> Here is the code for function called GetRecord() which returns array
> with record contents (from the curent record in selected database):

> #include "dbStruct.ch"

> FUNCTION GetRecord  ()

>     Local aRecord, aStruct
>     Local i, cFldName, uFldValue

>     aRecord := {}
>     aStruct := dbStruct()

>     FOR i := 1 TO Len(aStruct)
>         cFldName  := aStruct[i, DBS_NAME]
>         uFldValue := &cFldName
>         AAdd (aRecord, { cFldName, uFldValue })
>     NEXT

> RETURN aRecord

> -= Jovan Bulajic =-
> Belgrade,Yugoslavia


Jovan,

About the "last" macro. What about this code? I think it works the same.

FOR i := 1 TO Len(aStruct)
    AAdd (aRecord, { aStruct[i, DBS_NAME], FieldGet(i) })
NEXT

:-)

Holger



Tue, 22 Dec 1998 03:00:00 GMT  
 Get record in an array


Quote:

>I think that few days ago someone asked about way to put whole record
>from database in array. I can't find original posting :), but here is
>the answer.

>This can be done in two steps - first one is to get informations about
>database structure using Clipper's function dbStructure() (introduced in
>CA-Clipper 5.2, this function is prefered replacement for good old
>AFields()). Second step is to retrive data from database for each field
>by using macro operator (this is one of the last cases where code blocks
>still can't replace macros) to evaluate field contents.

>Here is the code for function called GetRecord() which returns array
>with record contents (from the curent record in selected database):

Macro's are slow, and 5.x gave us a better, faster way!

FUNCTION GetRecord  ()

Local aRecord
Local i
local nNumRecs := fcount()

aRecord := array( nNumRecs )

FOR i := 1 TO nNumRecs
   aRecord[ i ] := { fieldname(i), fieldget(i) }
NEXT

RETURN aRecord

Better yet, forget putting in the field name.

--

                       WWW  http://www.iag.net/~philb/
                  FTP Site  ftp://ftp.iag.net/pub/clipper
                      FIDO  1:363/299



Wed, 23 Dec 1998 03:00:00 GMT  
 Get record in an array


Quote:
> I think that few days ago someone asked about way to put whole record
> from database in array.

In reading this posting, I was surprised by the responses, and the lack of
the aEval function to solve the problem. I created a Scatter and Gather
function which allows me to throw all the fields in a database to an
array, and then later ( after user modifies GETs to the array elements )
gather the array back to the database.

FUNCTION Scatter( cFileName, aName, lBlankRec )
   //
   // Checking For cFilename Open
   //
   Local nOldRec              
   Default lBlankRec To .F.

   If Select( cFileName ) == 0
      Use ( cFileName ) New
   Else
      Select ( cFileName )
   Endif

   aName := Array( FCount() )
   //
   //  Save the old record no in case we have to get a blank record
   //
   nOldRec := RecNo()

   If lBlankRec
      dbGoBottom()                              // Move to last record
      dbSkip(1)                                 // Then move to phantom record to get blank
   Endif

   aEval( aName, {|xExpression, nField| aName[nField] := FieldGet( nField
) })

   dbGoTo( nOldRec )                            // Reset the record pointer

RETURN( .T. )

Where cFileName = Name of Database to scatter
           aName     = Declared empty array
           lBlankRec = Whether or not to scatter an empty record (
defaults )

This function is called like this, ( assumes "Customer" is the database
name, and aCustomer is the array passed in ( blank ) and we want a blank
record used to stuff defaults:

    Scatter( "Customer", aCustomer, .T. )

aCustomer must exist so that the scatter actually does something, and the
Customer database must exist and be openned either prior to the call, or
if the scatter function contains code to open it if not found available.

The gather function works the same, except you can add a blank record to
the database before gathering the array with the third parameters like
this:

FUNCTION Gather( cFileName, aName, lNewRec )
   //
   // Checking For Filename Open
   //
   If Select( cFileName ) == 0
      Use ( cFileName ) New
   Else
      Select ( cFileName )
   Endif

   If lNewRec

      If ( !lFirst .and. lNewRec )
         Append Blank
      Endif

      If NetErr()
         //
         //   Repeat this until it works or user aborts
         //
         Do While .T.
            nWarn := Alert( "Database " + AllTrim( cFileName ) + "
Currently Locked", { "~Retry", "~Abort" })
              If nWarn == 1
                 Append Blank
                 If NetErr()
                    Loop
                 Else
                    Exit
                 Endif
              Else
                 Return( .F. )
              Endif
         EndDo
      Endif
   Endif

   If oNetUse                   // Our variable which means this is on a multiuser system
      If RLock()
         aEval( aName, {|xExpression, nField| FIELDPUT(nField,
xExpression)})
      Else
         Alert( "Cannot Write File;Lock Required;Try Your Request Later" )
      Endif
   Else
      If RLock()
         aEval( aName, {|xExpression, nField| FIELDPUT(nField,
xExpression)})
      Else
         Alert( "Cannot Write File;Lock Required;Try Your Request Later" )
      Endif
   Endif
   UnLock
RETURN( .T. )



Fri, 25 Dec 1998 03:00:00 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. An array of records within an array of records (Still a problem)

2. an array of records within an array of records

3. Getting strange records with more than 100.000 records with NTX

4. getting the previous record

5. getting selected records

6. Loop Question - Only getting half the records

7. Getting selected record in relational tree

8. Getting latest record from file!

9. Error getting record in browse

10. getting data from nonrelated one-record file in report

11. Getting all record info for Combo box selection

12. getting: invalid record declaration error

 

 
Powered by phpBB® Forum Software