[View Borland Home Page][View Product List][Search This Web Site][View Available Downloads][Join Borland Online][Enter Discussion Area][Send Email To Webmaster]

BDE API Examples (DbiCreateTable)

DbiCreateTable creates a table in the database associated with the given database handle.
Create a table with a different level, block size, and fill factor than specified in the BDE configuration: This example is included with Delphi in the BDE32.HLP file.


Example 1: Create a table with a different language driver than the one specified in the LANGDIRVER driver setting

To use this example, you must first setup a pFLDDesc field descriptor and already know the abbrievation for the language driver to use. To get this abbreviation, use DbiOpenLDList.
This example uses the following input: fDbiCreateTable(Database1.Handle, 'TestLDTable', 3, @fldDes, 'anhundc');
procedure fDbiCreateTable(hTmpDb: hDBIDb; TableName: String; Fields: Word;
             pFlds: pFLDDesc; LDName: string);
var
  pOpDesc: pFLDDesc;
  pOpData: pBYTE;
  TblDesc: CRTblDesc;

begin
  pOpDesc := AllocMem(3 * sizeof(FLDDesc));
  pOpData := AllocMem(20);
  try
    // Set up the parameter
    pOpDesc.iOffset := 0;
    pOpDesc.iLen := Length(LDName) + 1;
    StrPCopy(pOpDesc.szName, 'LANGDRIVER');
    StrPCopy(PChar(pOpData), LDName);

    // Format the table descriptor
    FillChar(TblDesc, sizeof(TblDesc), #0);
    StrPCopy(TblDesc.szTblName, TableName);
    StrCopy(TblDesc.szTblType, szPARADOX);

    TblDesc.iOptParams := 1;
    TblDesc.pFldOptParams := pOpDesc;
    TblDesc.pOptData := pOpData;
    TblDesc.iFldCount := Fields;
    TblDesc.pFldDesc := pFlds;
    // Create the table
    Check(DbiCreateTable(hTmpDb, True, TblDesc));
  finally
    FreeMem(pOpDesc, 3 * sizeof(FLDDesc));
    FreeMem(pOpData, 20);
  end;
end;

Here is an example of the pFLDDesc that is passed into the above function:
const
    fldDes: array[0..2] of FLDDesc = (
              ( { Field 1 - AUTOINC }
               iFldNum:      1;            { Field Number }
               szName:       'AUTOINC';    { Field Name }
               iFldType:     fldINT32;     { Field Type }
               iSubType:     fldstAUTOINC; { Field Subtype }
               iUnits1:      0;            { Field Size }
               iUnits2:      0;            { Decimal places ( 0 ) }
               iOffset:      0;            { Offset in record ( 0 ) }
               iLen:         0;            { Length in Bytes  ( 0 ) }
               iNullOffset:  0;            { For Null Bits    ( 0 ) }
               efldvVchk:    fldvNOCHECKS; { Validiy checks   ( 0 ) }
               efldrRights:  fldrREADWRITE { Rights }
              ),
              ( { Field 2 - ALPHA }
               iFldNum:      2; szName:       'ALPHA';
               iFldType:     fldZSTRING; iSubType:     fldUNKNOWN;
               iUnits1:      10; iUnits2:      0;
               iOffset:      0; iLen:         0;
               iNullOffset:  0; efldvVchk:    fldvNOCHECKS;
               efldrRights:  fldrREADWRITE
              ),
              ( { Field 3 - NUMERIC }
               iFldNum:      3; szName:       'NUMERIC';
               iFldType:     fldFLOAT; iSubType:     fldUNKNOWN;
               iUnits1:      0; iUnits2:      0;
               iOffset:      0; iLen:         0;
               iNullOffset:  0; efldvVchk:    fldvNOCHECKS;
               efldrRights:  fldrREADWRITE
              ));

Example 2: Create a table with a single index

This example will create a Fox Pro table and .CDX index but can be changed to create any table or index type.
This example uses the following input: CreateTableWithIndex(Database1.Handle); or CreateTableWithIndex(Table1.DBHandle);
procedure CreateTableWithIndex(hDB: hDBIDb);
const
  // Constant for Fox Pro table if it is missing from the BDE unit...
  szFOXPRO = 'FOXPRO';

  // Type of table to create: i.e. szPARADOX, szDBASE, szFOXPRO, etc...
  TABLETYPE = szFOXPRO;
  // Number of fields in the table...
  FIELDS = 2;
  // Table Name...
  TABLENAME = 'FoxPro.dbf';
  // Index Name...
  INDEXNAME = 'FoxIdx';
  // Number of fields in the index...
  FIELDS_IN_INDEX = 1;
  // Field numbers which the index will be placed...
  FIELD_NUMBERS: packed array[0..FIELDS_IN_INDEX - 1] of WORD = (1);
  // Is this a primary index...
  PRIMARY_INDEX: BOOL = FALSE;

var
  TblDesc: CRTblDesc;
  pFlds: pFLDDesc;
  NewIndex: IDXDesc;

begin
  pFlds := Allocmem(FIELDS * sizeof(FLDDesc));
  try
    // Enter field information for the first field...
    pFlds^.iFldNum := 1;
    pFlds^.szName := 'FIELD1';
    pFlds^.iFldType := fldFLOAT;
    pFlds^.iUnits1 := 8;

    Inc(pFlds);

    // Enter field information for the second field...
    pFlds^.iFldNum := 2;
    pFlds^.szName := 'FIELD2';
    pFlds^.iFldType := fldZSTRING;
    pFlds^.iUnits1 := 15;

    Dec(pFlds, FIELDS - 1);

    FillChar(TblDesc, sizeof(TblDesc), 0);
    // Set table descriptor information...
    TblDesc.szTblName := TABLENAME;
    TblDesc.szTblType := TABLETYPE;
    TblDesc.iFldCount := FIELDS;
    TblDesc.pfldDesc := pFlds;

    // Create the table...
    Check(DbiCreateTable(hDb, TRUE, TblDesc));

    FillChar(NewIndex, sizeof(NewIndex), 0);
    // Set the index descriptor information...
    NewIndex.szTagName:= INDEXNAME;
    NewIndex.bMaintained:= TRUE;
    NewIndex.iFldsInKey:= FIELDS_IN_INDEX;
    NewIndex.bPrimary := PRIMARY_INDEX;
    Move(FIELD_NUMBERS, NewIndex.aiKeyFld, sizeof(FIELD_NUMBERS));

    // Create the index...
    Check(DbiAddIndex(hDb, nil, TABLENAME, TABLETYPE, NewIndex, nil));
  finally
    FreeMem(pFlds);
  end;
end;

Example 3: Create a dBASE 7 table with a primary key

This example will create a Visual dBASE 7 table with a primary key on the first field. As with all of these examples, it can be easily changed to fit any needs.
This example uses the following input: CreatedBASE7Table(Database1.Handle); or CreatedBASE7Table(Table1.DBHandle);
procedure CreatedBASE7Table(hDB: hDBIDb);
const
  FIELDS = 3;
  INDEXES = 1;
  TABLE_NAME = 'dBASE 7 Table.dbf';

var
  pFields: pFLDDesc;
  Index: IDXDesc;
  TblDesc: CRTblDesc;

begin
  // Initialize structures to zero...
  FillChar(Index, sizeof(Index), 0);
  FillChar(TblDesc, sizeof(TblDesc), 0);

  // Allocate memory for the field descriptors...
  pFields := AllocMem(FIELDS * sizeof(FLDDesc));
  try
    // Create an AUTOINC field (Visual dBASE 7 field type only)...
    pFields^.iFldNum := 1;
    pFields^.szName := 'Field_Number_1';
    pFields^.iFldType := fldINT32;
    pFields^.iSubType := fldstAUTOINC;
    Inc(pFields);

    // Create a STRING field...
    pFields^.iFldNum := 2;
    pFields^.szName := 'Field_Number_2';
    pFields^.iFldType := fldZSTRING;
    pFields^.iUnits1 := 20;
    Inc(pFields);

    // Create a TIMESTAMP field (Visual dBASE 7 field type only)...
    pFields^.iFldNum := 3;
    pFields^.szName := 'Field_Number_3';
    pFields^.iFldType := fldTIMESTAMP;
    Dec(pFields, FIELDS - 1);

    // Create primary index...
    Index.szName := 'PKey';
    Index.bPrimary := TRUE;
    Index.bMaintained := TRUE;
    Index.iFldsInKey := 1;
    Index.aiKeyFld[0] := 1;

    // Add the field and index information to the table descriptor...
    TblDesc.szTblName := TABLE_NAME;
    TblDesc.szTblType := szDBASE;
    TblDesc.iFldCount := FIELDS;
    TblDesc.pfldDesc := pFields;
    TblDesc.iIdxCount := INDEXES;
    TblDesc.pidxDesc := @Index;

    //Create the table (even if it already exists)...
    Check(DbiCreateTable(hDb, TRUE, TblDesc));
  finally
    Freemem(pFields);
  end;
end;

Back to BDE API Reference Page


DISCLAIMER: You have the right to use this technical information subject to the terms of the No-Nonsense License Statement that you received with the Borland product to which this information pertains.
Trademarks & Copyright © 1998 Borland International, Inc.