Function DbStruct() Foundation

Generates an array containing the structure data for a database file.

Syntax
DbStruct() --> aStructure
Return

The return value of DbStruct() is a two dimensional array containing the data for a database file structure. Each subarray contains the specification for a single field. The subarrays have four elements with the following contents:

Structure of the subarray for a field specification
Element No. Meaning Constant in Dbstruct.ch
1 Field name DBS_NAME
2 Field type DBS_TYPE
3 Field length DBS_LEN
4 Decimal places DBS_DEC

When no database file is open in the work area, an empty array ({}) is returned.

Description

The database function DbStruct() returns the structure of a database file open in a work area. When the function is used without the alias operator, it returns the structure of the database file in the current work area. DbStruct() generates a two dimensional array with four columns containing the same information about the database file as what would be created by the command COPY STRUCTURE EXTENDED.

Passing the array returned by DbStruct() to the function DbCreate(), a new database file can be created based on the structure of an existing database file.

Examples
DbStruct()

// In the example, the structure of a database file is read 
// and the field names are displayed. 

#include "Dbstruct.ch" 

PROCEDURE Main 
   LOCAL aStructure 

   USE Customer NEW 
   aStructure := DbStruct()         // read file structure 
                                    // list field names 
   AEval( aStructure, {|a| QOut( a[DBS_NAME] )} ) 
   CLOSE Customer 
RETURN 

Changes the structure of a database file

// The example shows how the structure of a database file can be 
// modified. An element is appended to the DbStruct() array. 
// A temporary file is created containing all records 
// of the original file. The original file is then deleted 
// and the temporary file renamed. 

PROCEDURE Main 
   LOCAL aStructure 

   USE Customer NEW 

   aStructure := DbStruct()         // read file structure 

   CLOSE Customer 
                                    // attach field for Fax number 
   AAdd( aStructure, {"FAX","C",15,0} ) 

   DbCreate("TEMP", aStructure )    // create and open 
                                    // temporary file with 
   USE Temp                         // new structure 
   APPEND FROM Customer             // copy records 
                                    // from CUSTOMER.DBF 
   CLOSE Temp 

   ERASE Customer.dbf               // delete source file 
   RENAME Temp.dbf TO Customer.dbf  // rename temporary file 

   IF File( "TEMP.DBT" )            // when memo file exists 
      ERASE Customer.dbt            // delete old file 
      RENAME Temp.dbt TO customer.dbt // rename temporary file 
   ENDIF 

RETURN 

Feedback

If you see anything in the documentation that is not correct, does not match your experience with the particular feature or requires further clarification, please use this form to report a documentation issue.