home *** CD-ROM | disk | FTP | other *** search
/ C Programming Starter Kit 2.0 / SamsPublishing-CProgrammingStarterKit-v2.0-Win31.iso / bde / snipit.pak / CR8DBTBL.C < prev    next >
Encoding:
C/C++ Source or Header  |  1997-07-24  |  5.6 KB  |  152 lines

  1. // BDE - (C) Copyright 1995 by Borland International
  2.  
  3. // cr8dbtbl.c
  4. #include "snipit.h"
  5.  
  6. static const char szTblName[] = "CR8DBTBL";
  7. static const char szTblType[] = szDBASE;
  8.  
  9. // Field descriptor used in creating a table.
  10. static SNIPFAR FLDDesc fldDesc[] = {
  11.               { // Field 1 - FLOAT
  12.                 1,            // Field number
  13.                 "FLOAT",      // Field name
  14.                 fldFLOAT,     // Field type
  15.                 fldUNKNOWN,   // Field subtype
  16.                 8,            // Field size ( 1 or 0, except
  17.                               //     BLOB, CHAR fields, dBASE FLOAT,
  18.                               //     NUMERIC fields )
  19.                 0,            // Decimal places   ( 0 )
  20.                               //     computed
  21.                 0,            // Offset in record ( 0 )
  22.                 0,            // Length in bytes  ( 0 )
  23.                 0,            // For Null bits    ( 0 )
  24.                 fldvNOCHECKS, // Validity checks  ( 0 )
  25.                 fldrREADWRITE // Rights
  26.               },
  27.               { // Field 2 - CHARACTER
  28.                 2, "CHARACTER", fldZSTRING, fldUNKNOWN,
  29.                 12, 0, 0, 0, 0,
  30.                 fldvNOCHECKS, fldrREADWRITE
  31.               },
  32.               { // Field 3 - NUMERIC
  33.                 3, "NUMERIC", fldFLOAT, fldUNKNOWN,
  34.                 6, 0, 0, 0, 0, fldvNOCHECKS, fldrREADWRITE
  35.               },
  36.               { // Field 4 - DATE
  37.                 4, "DATE", fldDATE, fldUNKNOWN,
  38.                 0, 0, 0, 0, 0, fldvNOCHECKS, fldrREADWRITE
  39.               },
  40.               { // Field 5 - LOGICAL
  41.                 5, "LOGICAL", fldBOOL, fldUNKNOWN,
  42.                 0, 0, 0, 0, 0, fldvNOCHECKS, fldrREADWRITE
  43.               },
  44.               { // Field 6 - MEMO
  45.                 6, "MEMO", fldBLOB, fldstMEMO,
  46.                 10, 0, 0, 0, 0, fldvNOCHECKS, fldrREADWRITE
  47.               },
  48.               { // Field 7 - OLE
  49.                 7, "OLE", fldBLOB, fldstDBSOLEOBJ,
  50.                 10, 0, 0, 0, 0, fldvNOCHECKS, fldrREADWRITE
  51.               },
  52.               { // Field 8 - BINARY
  53.                 8, "BINARY", fldBLOB, fldstTYPEDBINARY,
  54.                 10, 0, 0, 0, 0, fldvNOCHECKS, fldrREADWRITE
  55.               },
  56.               { // Field 9 - DBASELOCK
  57.                 9, "DBASELOCK", fldLOCKINFO, fldUNKNOWN,
  58.                 8, 0, 0, 0, 0, fldvNOCHECKS, fldrREADWRITE
  59.               }
  60.               };  // Array of field descriptors.
  61.  
  62. // The number of fields in the table.
  63. static const unsigned uNumFields = sizeof(fldDesc) / sizeof (fldDesc[0]);
  64.  
  65. //=====================================================================
  66. //  Function:
  67. //          CreateAndFillSampleDB();
  68. //
  69. //  Description:
  70. //          This sample code will create a dBASE table, insert
  71. //          10 records into the table, then delete the table.
  72. //
  73. //  Note:   This example uses the new dBASE 5 table format.
  74. //=====================================================================
  75. void
  76. CreateAndFillSampleDB (void)
  77. {
  78.     hDBIDb      hDb;                // Handle to the database
  79.     hDBICur     hCur;               // Handle to the table
  80.     CRTblDesc   TblDesc;            // Create table descriptor
  81.     UINT16      uDispNumRecs = 10;  // Number of records to add and
  82.                                     //   display
  83.     DBIResult   rslt;               // Return value from IDAPI functions
  84.  
  85.     Screen("*** Create/Open/Fill dBASE Table Example ***\r\n");
  86.  
  87.     BREAK_IN_DEBUGGER();
  88.  
  89.     Screen("    Initializing IDAPI...");
  90.     if (InitAndConnect(&hDb) != DBIERR_NONE)
  91.     {
  92.         Screen("\r\n*** End of Example ***");
  93.         return;
  94.     }
  95.  
  96.     Screen("    Setting the database directory...");
  97.     rslt = DbiSetDirectory(hDb, (pCHAR) szTblDirectory);
  98.     ChkRslt(rslt, "SetDirectory");
  99.  
  100.     Screen("    Initializing the table descriptor...");
  101.     memset((void *) &TblDesc , 0, sizeof(CRTblDesc));
  102.     lstrcpy(TblDesc.szTblName, szTblName);
  103.     lstrcpy(TblDesc.szTblType, szTblType);
  104.     TblDesc.iFldCount = uNumFields;
  105.     TblDesc.pfldDesc = fldDesc;
  106.  
  107.     Screen("    Creating the %s dBASE table...", szTblName);
  108.     rslt = DbiCreateTable(hDb, TRUE, &TblDesc);
  109.     if (ChkRslt(rslt, "CreateTable") != DBIERR_NONE)
  110.     {
  111.         Screen("        Make certain that the dBASE table level is set"
  112.                " to '5' in IDAPI.CFG");
  113.         CloseDbAndExit(&hDb);
  114.         Screen("\r\n*** End of Example ***");
  115.         return;
  116.     }            
  117.  
  118.     Screen("    Fill the table with random data...");
  119.     FillTable(hDb, (pCHAR) szTblName, (pCHAR) szTblType, uDispNumRecs);
  120.  
  121.     Screen("    Open the %s table....", szTblName);
  122.     rslt = DbiOpenTable(hDb, (pCHAR) szTblName, (pCHAR) szTblType,
  123.                         NULL, NULL, 0, dbiREADWRITE, dbiOPENSHARED,
  124.                         xltFIELD, FALSE, NULL, &hCur);
  125.     if (ChkRslt(rslt, "OpenTable") != DBIERR_NONE)
  126.     {
  127.         rslt = DbiDeleteTable(hDb, (pCHAR) szTblName, (pCHAR) szTblType);
  128.         ChkRslt(rslt, "DeleteTable");
  129.         CloseDbAndExit(&hDb);
  130.         Screen("\r\n*** End of Example ***");
  131.         return;
  132.     }
  133.  
  134.     Screen("    Display the %s table we just created...", szTblName);
  135.     DisplayTable(hCur, uDispNumRecs);
  136.  
  137.     Screen("\r\n    Close the %s table...", szTblName);
  138.     rslt = DbiCloseCursor(&hCur);
  139.     ChkRslt(rslt, "CloseCursor");
  140.  
  141.     Screen("    Deleting the %s table...", szTblName);
  142.     rslt = DbiDeleteTable(hDb, (pCHAR) szTblName, (pCHAR) szTblType);
  143.     ChkRslt(rslt, "DeleteTable");
  144.  
  145.     Screen("    Close the database and exit IDAPI...");
  146.     CloseDbAndExit(&hDb);
  147.  
  148.     Screen("\r\n*** End of Example ***");
  149. }
  150.  
  151.  
  152.