⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 dbicreatetable.html

📁 Delphi API应用手册
💻 HTML
字号:
<HTML>
<HEAD>
<TITLE>BDE API Examples (DbiCreateTable)</TITLE>
<META NAME="KEYWORDS" CONTENT="BDE, Borland">
<META NAME="DESCRIPTION" CONTENT="">
</HEAD>

<BODY BGCOLOR="#FFFFFF" TEXT="#000000" LINK="#000000" ALINK="#999999" VLINK="#666666">

<CENTER>
<TABLE CELLPADDING="4" CELLSPACING="4" BORDER="0" WIDTH="600">

<TR>

<TD ALIGN="CENTER" VALIGN="TOP" COLSPAN="1"><NOBR><A HREF="/"><IMG SRC="/images/bolus.gif" ALT="[View Borland Home Page]" BORDER="0"></A><A HREF="/products.html"><IMG SRC="/images/products.gif" ALT="[View Product List]" BORDER="0"></A><A HREF="/searchsite/"><IMG SRC="/images/search.gif" ALT="[Search This Web Site]" BORDER="0"></A><A HREF="/download.html"><IMG SRC="/images/downloads.gif" ALT="[View Available Downloads]" BORDER="0"></A><A HREF="/membership.html"><IMG SRC="/images/membership.gif" ALT="[Join Borland Online]" BORDER="0"></A><A HREF="/newsgroups/"><IMG SRC="/images/newsgroups.gif" ALT="[Enter Discussion Area]" BORDER="0"></A><A HREF="/feedback/"><IMG SRC="/images/feedback.gif" ALT="[Send Email To Webmaster]" BORDER="0"></A></NOBR></TD>
</TR>

<TR>

<TR><TD ALIGN="LEFT" VALIGN="TOP" COLSPAN="1"><font size=2>

<h2>BDE API Examples (DbiCreateTable)</h2> 
DbiCreateTable creates a table in the database associated with the given database handle. 

<hr size=2 noshade>
<b>Create a table with a different level, block size, and fill factor than 
specified in the BDE configuration:</b> 
This example is included with Delphi in the BDE32.HLP file.
<p>

<hr size=2 noshade>

<H3>Example 1: Create a table with a different language driver than the one specified in the
LANGDIRVER driver setting</H3>
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.<BR>
This example uses the following input: <I>fDbiCreateTable(Database1.Handle, 'TestLDTable', 3, @fldDes, 'anhundc');</I>
<PRE>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;</PRE>

<hr size=1 noshade>

<B>Here is an example of the pFLDDesc that is passed into the above function:</B>
<PRE>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
              ));</PRE>


<hr size=2 noshade>
<H3>Example 2: Create a table with a single index</H3>
This example will create a Fox Pro table and .CDX index but can be changed to create 
any table or index type.<BR>
This example uses the following input: <I>CreateTableWithIndex(Database1.Handle);</I> or
<I>CreateTableWithIndex(Table1.DBHandle);</I>
<PRE>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;</PRE>

<hr size=2 noshade>
<H3>Example 3: Create a dBASE 7 table with a primary key</H3>
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.<BR>
This example uses the following input: <I>CreatedBASE7Table(Database1.Handle);</I> or
<I>CreatedBASE7Table(Table1.DBHandle);</I>
<PRE>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;</PRE>

<hr size=2 noshade>
<p>

<A HREF="/devsupport/bde/bdeapiex/index.html">Back to BDE API Reference Page</A>

</TD></TR>

<TR><TD ALIGN="LEFT" VALIGN="TOP" COLSPAN="1"><font size=2>


<HR SIZE="2" NOSHADE>

<FONT SIZE = 2><i><b>DISCLAIMER:</b> 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.</FONT></i></B>


</dl>
</td>

</TR>

<TR>

<!-- THIS IS THE BOTTOM COPYRIGHT & UPDATED MESSAGE -->

<TD ALIGN="CENTER" VALIGN="TOP" COLSPAN="3"><FONT SIZE="2"><A HREF="/copyright.html">Trademarks &amp; Copyright</A> &#169; 1998 Borland International, Inc.

<SCRIPT LANGUAGE="JavaScript">
<!-- HIDE FROM OLD BROWSERS

// THIS WILL WRITE OUT THE LAST MODIFIED DATE
// YOU DO NOT NEED TO CHANGE ANYTHING HERE

function makeArray(arraySize) {
     this.length = arraySize
     return this
}

monthNames = new makeArray(12)
monthNames[1] = "January"
monthNames[2] = "February"
monthNames[3] = "March"
monthNames[4] = "April"
monthNames[5] = "May"
monthNames[6] = "June"
monthNames[7] = "July"
monthNames[8] = "August"
monthNames[9] = "September"
monthNames[10] = "October"
monthNames[11] = "November"
monthNames[12] = "December"

updated = new Date(document.lastModified)
theMonth = monthNames[updated.getMonth() + 1]
theDate = updated.getDate()
theYear = updated.getYear() + 1900

document.write("Last modified on " + theDate +  "-" + theMonth + "-" + theYear + ".")
// END HIDING -->
</SCRIPT></FONT></TD>
</TR>

</TABLE>
</CENTER>

</BODY>
</HTML>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -