home *** CD-ROM | disk | FTP | other *** search
/ C Programming Starter Kit 2.0 / SamsPublishing-CProgrammingStarterKit-v2.0-Win31.iso / bde / snipit.pak / CR8TXTBL.C < prev    next >
C/C++ Source or Header  |  1997-07-23  |  6KB  |  151 lines

  1. // BDE - (C) Copyright 1995 by Borland International
  2.  
  3. // cr8txtbl.c
  4. #include "snipit.h"
  5.  
  6. static const char szTblName[] = "texttbl";
  7. static const char szTblType[] = szASCII;
  8.  
  9. // Field descriptor used in creating a table.
  10. //   Note that you have to use physical field descriptors
  11. static SNIPFAR FLDDesc fldDesc[] = {
  12.               { // Field 1 - CHARACTER
  13.                 1,            // Field number
  14.                 "CHARACTER",  // Field name
  15.                 fldASCCHAR,   // Field type
  16.                 fldUNKNOWN,   // Field subtype
  17.                 10,           // Field size ( 1 or 0, except
  18.                               //     BLOB, CHAR fields, Text FLOAT,
  19.                               //     NUMERIC fields )
  20.                 0,            // Decimal places   ( 0 )
  21.                               //     computed
  22.                 0,            // Offset in record ( 0 )
  23.                 0,            // Length in bytes  ( 0 )
  24.                 0,            // Null offset      ( 0 )
  25.                 fldvNOCHECKS, // Validity checks  ( 0 )
  26.                 fldrREADWRITE // Rights
  27.               }, 
  28.               { // Field 2 - SHORT
  29.                 2, "NUMERIC", fldASCNUM, fldUNKNOWN,
  30.                 4, 0, 10, 0, 0, fldvNOCHECKS, fldrREADWRITE
  31.               },
  32.               { // Field 3 - DATE
  33.                 3, "DATE", fldASCDATE, fldUNKNOWN,
  34.                 10, 0, 14, 0, 0, fldvNOCHECKS, fldrREADWRITE
  35.               },
  36.               { // Field 4 - LOGICAL
  37.                 4, "LOGICAL", fldASCBOOL, fldUNKNOWN,
  38.                 2, 0, 24, 0, 0, fldvNOCHECKS, fldrREADWRITE
  39.               },
  40.               { // Field 5 - TIME
  41.                 5, "TIME", fldASCTIME, fldUNKNOWN,
  42.                 8, 0, 26, 0, 0, fldvNOCHECKS, fldrREADWRITE
  43.               },
  44.               { // Field 6 - TIMESTAMP
  45.                 6, "TIMESTAMP", fldASCTIMESTAMP, fldUNKNOWN,
  46.                 20, 0, 34, 0, 0, fldvNOCHECKS, fldrREADWRITE
  47.               },
  48.               { // Field 7 - LONG
  49.                 7, "LONG", fldASCLONG, fldUNKNOWN,
  50.                 4, 0, 54, 0, 0, fldvNOCHECKS, fldrREADWRITE
  51.               },
  52.               { // Field 8 - FLOAT
  53.                 8, "FLOAT", fldASCFLOAT, fldUNKNOWN,
  54.                 8, 2, 58, 0, 0, fldvNOCHECKS, fldrREADWRITE
  55.               },
  56.               { // Field 9 - MONEY
  57.                 9, "MONEY", fldASCMONEY, fldstMONEY,
  58.                 8, 2, 66, 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. //          CreateAndFillSampleTXT();
  68. //
  69. //  Description:
  70. //          This sample code will create a text table, insert
  71. //          10 records into the table, then delete the table.
  72. //
  73. //          Note that the fields of a text table are set using the
  74. //          DbiSetFieldMap function.  It is required that the field
  75. //          descriptor used when setting the fields of a text table
  76. //          contains the physical field types.
  77. //=====================================================================
  78. void
  79. CreateAndFillSampleTXT (void)
  80. {
  81.     hDBIDb      hDb;                // Handle to the database
  82.     hDBICur     hCur;               // Handle to the table
  83.     CRTblDesc   TblDesc;            // Create table descriptor
  84.     UINT16      uDispNumRecs = 10;  // Number of records to add and
  85.                                     //   display
  86.     DBIResult   rslt;               // Return value from IDAPI functions
  87.  
  88.     Screen("*** Create/Open/Fill Text Table Example ***\r\n");
  89.  
  90.     BREAK_IN_DEBUGGER();
  91.  
  92.     Screen("    Initializing IDAPI...");
  93.     if (InitAndConnect(&hDb) != DBIERR_NONE)
  94.     {
  95.         Screen("\r\n*** End of Example ***");
  96.         return;
  97.     }
  98.  
  99.     Screen("    Setting the database directory...");
  100.     rslt = DbiSetDirectory(hDb, (pCHAR)szTblDirectory);
  101.     ChkRslt(rslt, "SetDirectory");
  102.  
  103.     Screen("    Force the use of a schema file for the text table...");
  104.     rslt = DbiSetProp(hDb, dbUSESCHEMAFILE, (UINT32)TRUE);
  105.     ChkRslt(rslt, "SetProp");
  106.  
  107.     Screen("    Initializing the table descriptor...");
  108.     memset((void *) &TblDesc , 0, sizeof(CRTblDesc));
  109.     lstrcpy(TblDesc.szTblName, szTblName);
  110.     lstrcpy(TblDesc.szTblType, szTblType);
  111.     TblDesc.iFldCount = uNumFields;
  112.     TblDesc.pfldDesc = fldDesc;
  113.  
  114.     Screen("    Creating the text table...");
  115.     rslt = DbiCreateTable(hDb, TRUE, &TblDesc);
  116.     if (ChkRslt(rslt, "CreateTable") != DBIERR_NONE)
  117.     {
  118.         CloseDbAndExit(&hDb);
  119.         Screen("\r\n*** End of Example ***");
  120.         return;
  121.     }
  122.  
  123.     Screen("    Fill the table with Random Data...");
  124.     FillTable(hDb, (pCHAR)szTblName, "ASCIIDRV", uDispNumRecs);
  125.  
  126.     Screen("    Open the %s table....", szTblName);
  127.     rslt = DbiOpenTable(hDb, (pCHAR)szTblName, "ASCIIDRV",
  128.                         NULL, NULL, 0, dbiREADWRITE,
  129.                         dbiOPENSHARED, xltFIELD, FALSE, NULL, &hCur);
  130.     if (ChkRslt(rslt, "OpenTable") != DBIERR_NONE)
  131.     {
  132.         CloseDbAndExit(&hDb);
  133.         Screen("\r\n*** End of Example ***");
  134.         return;
  135.     }
  136.  
  137.     Screen("    Display the %s table we just created...", szTblName);
  138.     DisplayTable(hCur, uDispNumRecs);
  139.  
  140.     Screen("\r\n    Close the %s table...", szTblName);
  141.     rslt = DbiCloseCursor(&hCur);
  142.     ChkRslt(rslt, "CloseCursor");
  143.  
  144.     Screen("    Close the database and exit IDAPI...");
  145.     CloseDbAndExit(&hDb);
  146.  
  147.     Screen("\r\n*** End of Example ***");
  148. }
  149.  
  150.  
  151.