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

  1. // BDE - (C) Copyright 1995 by Borland International
  2.  
  3. // TextImp.c
  4. #include "snipit.h"
  5.  
  6. #define NAMELEN  10 // Length of the name fields.
  7. #define PLACELEN 20 // Length of the POB field.
  8. #define DATELEN  11 // Length that a date will be displayed: mm\dd\yyyy.
  9.  
  10. static const char szDBTblName[] = "DBPeople";
  11. static const char szDBTblType[] = szDBASE;
  12.  
  13. static const char szTXTTblName[] = "People";
  14. static const char szTXTTblType[] = szASCII;
  15.  
  16. // Field descriptor used in creating a table.
  17. static SNIPFAR FLDDesc DBfldDesc[] = {
  18.                           { // Field 1 - First name
  19.                             1,            // Field number
  20.                             "Name",       // Field name
  21.                             fldZSTRING,   // Field type
  22.                             fldUNKNOWN,   // Field subtype
  23.                             NAMELEN,      // Field size (1 or 0, except
  24.                                           //   BLOB or CHAR field)
  25.                             0,            // Decimal places   ( 0 )
  26.                                           //   computed
  27.                             0,            // Offset in record ( 0 )
  28.                             0,            // Length in bytes  ( 0 )
  29.                             0,            // For NULL bits    ( 0 )
  30.                             fldvNOCHECKS, // Validity checks  ( 0 )
  31.                             fldrREADWRITE // Rights
  32.                           },
  33.                           { // Field 2 - Place of Birth
  34.                             2, "POB", fldZSTRING, fldUNKNOWN, PLACELEN,
  35.                             0, 0, 0, 0, fldvNOCHECKS, fldrREADWRITE
  36.                           },
  37.                           { // Field 3 - Date of Birth
  38.                             3, "DOB", fldZSTRING, fldUNKNOWN, DATELEN,
  39.                             0, 0, 0, 0, fldvNOCHECKS, fldrREADWRITE
  40.                           }
  41.                      };  // Array of field descriptors.
  42.  
  43. static DBIResult AddRecordToTXT(phDBICur phCur, pCHAR pszName,
  44.                                 pCHAR pszPOB, pCHAR pszDate);
  45. static DBIResult CreateImportDBTable(phDBIDb phDb);
  46.  
  47. //=====================================================================
  48. //  Function:
  49. //          TextImport();
  50. //
  51. //  Description:
  52. //          This example shows how to import data from a text table
  53. //          to a dBASE table.  This example will create two tables, one
  54. //          text, one dBASE.  Records will be added to the .TXT table
  55. //          using DbiInsertRecord.  The records will then be copied from
  56. //          the text table to the dBASE table using the DbiBatchMove
  57. //          function.
  58. //=====================================================================
  59. void
  60. TextImport (void)
  61. {
  62.     DBIResult       rslt;               // Return value from IDAPI functions
  63.     hDBIDb          hDb;                // Handle to the database
  64.     hDBICur         DBhCur;             // dBASE cursor handle
  65.     hDBICur         TXThCur;            // Text cursor handle
  66.     CRTblDesc       crTblDsc;           // Table descriptor
  67.     pFLDDesc        pTXTFlds;           // List of text fields
  68.     BOOL            bOverWrite = TRUE;  // Overwrite, yes/no flag
  69.     unsigned        uNumFields;         // Number of fields in the table
  70.  
  71.     Screen("*** Text Import Example ***\r\n");
  72.  
  73.     BREAK_IN_DEBUGGER();
  74.  
  75.     Screen("    Initializing IDAPI...");
  76.     if (InitAndConnect(&hDb) != DBIERR_NONE)
  77.     {
  78.         Screen("\r\n*** End of Example ***");
  79.         return;
  80.     }
  81.  
  82.     Screen("    Setting the database directory...");
  83.     rslt = DbiSetDirectory(hDb, (pCHAR) szTblDirectory);
  84.     ChkRslt(rslt, "SetDirectory");
  85.  
  86.     Screen("    Create a dBASE table...");
  87.     if (CreateImportDBTable(&hDb) != DBIERR_NONE)
  88.     {
  89.         Screen("    Clean up IDAPI...");
  90.         CloseDbAndExit(&hDb);
  91.         Screen("\r\n*** End of Example ***");
  92.         return;
  93.     }
  94.  
  95.     Screen("    Open the %s dBASE table...", szDBTblName);
  96.     rslt = DbiOpenTable(hDb, (pCHAR) szDBTblName, (pCHAR) szDBTblType,
  97.                         NULL, NULL, 0, dbiREADWRITE, dbiOPENSHARED,
  98.                         xltFIELD, FALSE, NULL, &DBhCur);
  99.     if (ChkRslt(rslt, "OpenTable") != DBIERR_NONE)
  100.     {
  101.         CloseDbAndExit(&hDb);
  102.         Screen("\r\n*** End of Example ***");
  103.         return;
  104.     }
  105.  
  106.     // Initialize the create table descriptor.
  107.     memset(&crTblDsc, 0, sizeof(CRTblDesc));
  108.     strcpy(crTblDsc.szTblName, szTXTTblName);
  109.     strcpy(crTblDsc.szTblType, szTXTTblType);
  110.  
  111.     Screen("    Create a Text table...");
  112.     rslt = DbiCreateTable(hDb, bOverWrite, &crTblDsc);
  113.     if (ChkRslt(rslt, "CreateTable") != DBIERR_NONE)
  114.     {
  115.         rslt = DbiCloseCursor(&DBhCur);
  116.         ChkRslt(rslt, "CloseCursor");
  117.         rslt = DbiDeleteTable(hDb,(pCHAR) szDBTblName, (pCHAR) szDBTblType);
  118.         ChkRslt(rslt, "DeleteTable");
  119.         CloseDbAndExit(&hDb);
  120.         Screen("\r\n*** End of Example ***");
  121.         return;
  122.     }
  123.  
  124.     Screen("    Open the %s text table...", szTXTTblName);
  125.     rslt = DbiOpenTable(hDb, (pCHAR) szTXTTblName, "ASCIIDRV",
  126.                         NULL, NULL, 0, dbiREADWRITE, dbiOPENSHARED,
  127.                         xltFIELD, FALSE, NULL, &TXThCur);
  128.     if (ChkRslt(rslt, "OpenTable") != DBIERR_NONE)
  129.     {
  130.         rslt = DbiCloseCursor(&DBhCur);
  131.         ChkRslt(rslt, "CloseCursor");
  132.         rslt = DbiDeleteTable(hDb,(pCHAR) szDBTblName, (pCHAR) szDBTblType);
  133.         ChkRslt(rslt, "DeleteTable");
  134.         CloseDbAndExit(&hDb);
  135.         Screen("\r\n*** End of Example ***");
  136.         return;
  137.     }
  138.  
  139.     // The number of fields in the table.
  140.     uNumFields = sizeof(DBfldDesc) / sizeof (DBfldDesc[0]);
  141.  
  142.     // Allocate memory for field descriptors.
  143.     pTXTFlds = (pFLDDesc) malloc(uNumFields * sizeof(FLDDesc));
  144.     if (pTXTFlds == NULL)
  145.     {
  146.         rslt = DbiCloseCursor(&DBhCur);
  147.         ChkRslt(rslt, "CloseCursor");
  148.         rslt = DbiCloseCursor(&TXThCur);
  149.         ChkRslt(rslt, "CloseCursor");
  150.         rslt = DbiDeleteTable(hDb,(pCHAR) szDBTblName, (pCHAR) szDBTblType);
  151.         ChkRslt(rslt, "DeleteTable");
  152.         CloseDbAndExit(&hDb);
  153.         Screen("\r\n*** End of Example ***");
  154.         return;
  155.     }
  156.  
  157.     // Convert the record structure to the text table format.
  158.     rslt = DbiTranslateRecordStructure(NULL, uNumFields, DBfldDesc,
  159.                                        (pCHAR) szTXTTblType, NULL,
  160.                                        pTXTFlds);
  161.     ChkRslt(rslt, "TranslateRecordStruct");
  162.  
  163.     // Set the field map on the Text table.
  164.     rslt = DbiSetFieldMap(TXThCur, uNumFields, pTXTFlds);
  165.     if (ChkRslt(rslt, "SetFieldMap") != DBIERR_NONE)
  166.     {
  167.         rslt = DbiCloseCursor(&DBhCur);
  168.         ChkRslt(rslt, "CloseCursor");
  169.         rslt = DbiCloseCursor(&TXThCur);
  170.         ChkRslt(rslt, "CloseCursor");
  171.         rslt = DbiDeleteTable(hDb,(pCHAR) szDBTblName, (pCHAR) szDBTblType);
  172.         ChkRslt(rslt, "DeleteTable");
  173.         CloseDbAndExit(&hDb);
  174.         Screen("\r\n*** End of Example ***");
  175.         return;
  176.     }
  177.  
  178.     Screen("    Add records to the text table...");
  179.     rslt = AddRecordToTXT(&TXThCur, "Jeffery", "1/28/1967", "San Jose" );
  180.     rslt = AddRecordToTXT(&TXThCur, "William",  "8/12/1970", "New York");
  181.     rslt = AddRecordToTXT(&TXThCur, "Jimmy",  "3/10/1924", "San Francisco");
  182.     rslt = AddRecordToTXT(&TXThCur, "Larry", "12/22/1933", "Germany");
  183.  
  184.     rslt = DbiSetToBegin(DBhCur);
  185.     ChkRslt(rslt, "SetToBegin");
  186.  
  187.     rslt = DbiSetToBegin(TXThCur);
  188.     ChkRslt(rslt, "SetToBegin");
  189.  
  190.     // Copy the table.
  191.     Screen("    Import the records to the dBASE table...");
  192.     rslt = DbiBatchMove(NULL, TXThCur, NULL, DBhCur, batAPPEND,
  193.                         0, NULL, NULL, NULL, 0, NULL, NULL, NULL,
  194.                         NULL, NULL, NULL, FALSE, FALSE, NULL, TRUE);
  195.     ChkRslt(rslt, "Batch Move");
  196.  
  197.     rslt = DbiSetToBegin(DBhCur);
  198.     ChkRslt(rslt, "SetToBegin");
  199.  
  200.     Screen("\r\n    Displaying the updated dBASE table...");
  201.     DisplayTable(DBhCur, 0);
  202.  
  203.     // Clean up.
  204.     free(pTXTFlds);
  205.  
  206.     Screen("\r\n    Close the dBASE table...");
  207.     rslt = DbiCloseCursor(&DBhCur);
  208.     ChkRslt(rslt, "CloseCursor");
  209.  
  210.     Screen("    Delete the dBASE table...");
  211.     rslt = DbiDeleteTable(hDb,(pCHAR) szDBTblName, (pCHAR) szDBTblType);
  212.     ChkRslt(rslt, "DeleteTable");
  213.  
  214.     Screen("    Close the TEXT table...");
  215.     rslt = DbiCloseCursor(&TXThCur);
  216.     ChkRslt(rslt, "CloseCursor");
  217.  
  218.     Screen("    Close the database and exit IDAPI...");
  219.     CloseDbAndExit(&hDb);
  220.  
  221.     Screen("\r\n*** End of Example ***");
  222. }    
  223.  
  224. //=====================================================================
  225. //  Function:
  226. //          AddRecordToTXT(phCur, pszName, pszPOB, pszDate);
  227. //
  228. //  Input:  phCur   - Pointer to the cursor handle
  229. //          pszName - Name
  230. //          pszPOB  - Place of birth
  231. //          pszDate - Date
  232. //
  233. //  Return: Result of adding the record to the table
  234. //
  235. //  Description:
  236. //          This function will add a record to the given TEXT table.
  237. //=====================================================================
  238. DBIResult
  239. AddRecordToTXT (phDBICur phCur, pCHAR pszName, pCHAR pszPOB,
  240.                 pCHAR pszDate)
  241. {
  242.     DBIResult   rslt;       // Return value from IDAPI functions
  243.     CURProps    TblProps;   // Table properties
  244.     pBYTE       pRecBuf;    // Record buffer
  245.  
  246.     // Allocate a record buffer.
  247.     rslt = DbiGetCursorProps(*phCur, &TblProps);
  248.     ChkRslt(rslt, "GetProps");
  249.  
  250.     pRecBuf = (pBYTE) malloc(TblProps.iRecBufSize * sizeof(BYTE));
  251.     if (pRecBuf == NULL)
  252.     {
  253.         Screen("Out of memory");
  254.         return DBIERR_NOMEMORY;
  255.     }
  256.  
  257.     // Make sure we're starting with a clean record buffer.
  258.     rslt = DbiInitRecord(*phCur, pRecBuf);
  259.     ChkRslt(rslt, "InitRecord");
  260.  
  261.     // Name.
  262.     rslt = DbiPutField(*phCur, 1, pRecBuf, (pBYTE) pszName);
  263.     ChkRslt(rslt, "PutField");
  264.  
  265.     // Date of birth.
  266.     rslt = DbiPutField(*phCur, 2,  pRecBuf, (pBYTE) pszDate);
  267.     ChkRslt(rslt, "PutField");
  268.  
  269.     // Place fo Birth.
  270.     rslt = DbiPutField(*phCur, 3,  pRecBuf, (pBYTE) pszPOB);
  271.     ChkRslt(rslt, "PutField");
  272.  
  273.     rslt = DbiInsertRecord(*phCur, dbiNOLOCK, pRecBuf);
  274.     ChkRslt(rslt, "InsertRec");
  275.  
  276.     free(pRecBuf);
  277.     return rslt;
  278. }
  279.  
  280. //=====================================================================
  281. //  Function:
  282. //          CreateImportDBTable(phDb);
  283. //
  284. //  Input:  phDb    - Pointer to the database handle
  285. //
  286. //  Return: Result of the table initialization
  287. //
  288. //  Description:
  289. //          This function creates a dBASE table.
  290. //=====================================================================
  291. DBIResult
  292. CreateImportDBTable (phDBIDb phDb)
  293. {
  294.     DBIResult   rslt;               // Value returned from IDAPI functions
  295.     CRTblDesc   crTblDsc;           // Table descriptor
  296.     BOOL        bOverWrite = TRUE;  // Overwrite, yes/no flag
  297.  
  298.     // The number of fields in the table.
  299.     const unsigned uNumFields = sizeof(DBfldDesc) / sizeof (DBfldDesc[0]);
  300.  
  301.     // Initialize the create table descriptor.
  302.     memset(&crTblDsc, 0, sizeof(CRTblDesc));
  303.     strcpy(crTblDsc.szTblName, szDBTblName);
  304.     strcpy(crTblDsc.szTblType, szDBTblType);
  305.     crTblDsc.iFldCount     = uNumFields;
  306.     crTblDsc.pfldDesc      = DBfldDesc;
  307.  
  308.     rslt = DbiCreateTable(*phDb, bOverWrite, &crTblDsc);
  309.     if (ChkRslt(rslt, "CreateTable") != DBIERR_NONE)
  310.     {
  311.         return rslt;
  312.     }
  313.  
  314.     return rslt;
  315. }
  316.