Retro video games delivered to your door every month!
Click above to get retro games delivered to your door ever month!
X-Hacker.org- Force 4.0 Reference - dyncreate() create database without alias http://www.X-Hacker.org [<<Previous Entry] [^^Up^^] [Next Entry>>] [Menu] [About The Guide]
 dyncreate()         Create database without alias
------------------------------------------------------------------------------
 Declaration
   database.hdr

 Syntax
   func logical dyncreate extern
   param const char  cFileName, ;
         value uint  uFields, ;
         value uint  uDescLen, ;
         value ulong pFieldData

 Arguments
   cFileName is the name of the database file to be created.

   uFields is the number of fields in the database.

   uDescLen is the length of the arrays in the field descriptor
   structure.

   pFieldData is a pointer to a user-defined field data descriptor
   structure.

 Return
   A logical indicating if the database could be successfully created.

 Description
   The dyncreate() function creates an empty database according to the
   supplied field descriptor data structure. The dyncreate() function does
   not operate on an alias hardcoded in a dbfdef block, instead it
   facilitates the creation of databases whose structure is determined at
   runtime.

   The field descriptor structure passed to dyncreate() as the pFieldData
   parameter must be typedefed in the user code as shown below:

   // maximum number of fields in the database(s) created with this
   // particular structure
   #define MAX_FIELDS 10

   // field descriptor structure
   typedef struct FIELDDESC
      char( 10 ) cName[ MAX_FIELDS ]  // field name
      byte       bType[ MAX_FIELDS ]  // dynamic field type
      byte       bLen[  MAX_FIELDS ]  // field length
      byte       bDec[  MAX_FIELDS ]  // field decimals
   enddef

   The name of the structure and of its members are arbitrary, but the
   type, length, order, and information-content of the members must
   conform the sample shown above. The size of the individual member
   arrays in the structure depends on the value of the user-defined
   MAX_FIELDS constant, and it is fixed at compile time. The value of
   the MAX_FIELDS constant is passed to the dyncreate() function as the
   uDescLen parameter.

   The number of fields actually created depends on the uFields parameter of
   dyncreate(), which can be changed at runtime according to the needs of
   various databases. The programmer must ensure that the value of
   uFields never exceeds the value of the MAX_FIELDS constant, that is
   the maximum capacity of the field descriptor structure.

 Example
   #define EXAMPLE_DATABASE
   #define _DYNALIAS_
   #define _ALIASPTR_
   #include example.hdr

   // this application has a capacity to create databases with
   // maximum 20 fields
   #define MAX_FIELDS 20
   
   // field descriptor structure (parameter to dyncreate())
   typedef struct FIELDDESC
      char( 10 ) cName[    MAX_FIELDS ]  // field name
      byte       nDynType[ MAX_FIELDS ]  // dynamic field type
      byte       nLen[     MAX_FIELDS ]  // field length
      byte       nDec[     MAX_FIELDS ]  // field decimals
   enddef
   
   // in a real world application the contents of this structure would
   // be determined at runtime (here it is hardcoded)
   vardef sfar
      FIELDDESC sFields := ;
         { "first",  "last",    "sex",   "due",   "fi",    "fu",    "fg",     "fn",   "fd", "notes" }, ;
         { FT_CHAR, FT_CHAR, FT_LOGIC, FT_DATE, FT_INT, FT_UINT, FT_LONG, FT_ULONG, FT_DBL, FT_MEMO }, ;
         {      10,      15,        1,       8,      6,       6,      10,       10,      9,      10 }, ;
         {       0,       0,        0,       0,      0,       0,       0,        0,      2,       0 }
   enddef
   
   proc Test_dyncreate
   vardef
      _PALIAS pDbf                                      // dynamic alias
      uint    n
   enddef
   if dyncreate( "test.dbf", 10, MAX_FIELDS, &sFields ) // create database
      pDbf := dynopen( 0, "test.dbf", DO_SHARED )       // open database
      if pDbf
         ? "Database fields:"
         for n := 1 to afldcnt( pDbf )
            ? n, afieldname( pDbf, n )                  // display field names
         next
         close databases                                // clean up
         erase "test.dbf"
         erase "test.dbt"
      else
         ? "Database could not be opened"
      endif
   else
      ? "Database could not be created"
   endif
   endproc

   proc main
   Test_dyncreate()
   endproc

See Also: dynopen()

Online resources provided by: http://www.X-Hacker.org --- NG 2 HTML conversion by Dave Pearson