home *** CD-ROM | disk | FTP | other *** search
/ QBasic & Borland Pascal & C / Delphi5.iso / C / BC_502 / 32SNIPIT.PAK / TEXTIMP.C < prev    next >
Encoding:
C/C++ Source or Header  |  1997-05-06  |  11.3 KB  |  315 lines

  1. // BDE32 3.x - (C) Copyright 1996 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.     hDBIDb          hDb;                // Handle to the database
  63.     hDBICur         DBhCur;             // dBASE cursor handle 
  64.     hDBICur         TXThCur;            // Text cursor handle
  65.     CRTblDesc       crTblDsc;           // Table descriptor
  66.     pFLDDesc        pTXTFlds;           // List of text fields
  67.     BOOL            bOverWrite = TRUE;  // Overwrite, yes/no flag
  68.     UINT16          uNumFields;         // Number of fields in the table.
  69.     DBIResult       rslt;               // Return value from IDAPI functions
  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.     // Initiailize 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, FALSE);
  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.     AddRecordToTXT(&TXThCur, "Jeffery", "1/28/1967", "San Jose" );
  180.     AddRecordToTXT(&TXThCur, "William",  "8/12/1970", "New York");
  181.     AddRecordToTXT(&TXThCur, "Jimmy",  "3/10/1924", "San Francisco");
  182.     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.     Screen("    Import the records to the dBASE table...");
  191.     rslt = DbiBatchMove(NULL, TXThCur, NULL, DBhCur, batAPPEND,
  192.                         0, NULL, NULL, NULL, 0, NULL, NULL, NULL,
  193.                         NULL, NULL, NULL, FALSE, FALSE, NULL, TRUE);
  194.     ChkRslt(rslt, "Batch Move");
  195.  
  196.     rslt = DbiSetToBegin(DBhCur);
  197.     ChkRslt(rslt, "SetToBegin");
  198.  
  199.     Screen("\r\n    Displaying the updated dBASE table...");
  200.     DisplayTable(DBhCur, 0);
  201.  
  202.     // Cleanup.
  203.     free(pTXTFlds);
  204.  
  205.     Screen("\r\n    Close the dBASE table...");
  206.     rslt = DbiCloseCursor(&DBhCur);
  207.     ChkRslt(rslt, "CloseCursor");
  208.  
  209.     Screen("    Delete the dBASE table...");
  210.     rslt = DbiDeleteTable(hDb,(pCHAR) szDBTblName, (pCHAR) szDBTblType);
  211.     ChkRslt(rslt, "DeleteTable");
  212.  
  213.     Screen("    Close the TEXT table...");
  214.     rslt = DbiCloseCursor(&TXThCur);
  215.     ChkRslt(rslt, "CloseCursor");
  216.  
  217.     Screen("    Close the database and exit IDAPI...");
  218.     CloseDbAndExit(&hDb);
  219.  
  220.     Screen("\r\n*** End of Example ***");
  221. }    
  222.  
  223. //=====================================================================
  224. //  Function:
  225. //          AddRecordToTXT(phCur, pszName, pszPOB, pszDate);
  226. //
  227. //  Input:  phCur   - pointer to the cursor handle
  228. //          pszName - Name
  229. //          pszPOB  - Place of birth
  230. //          pszDate - Date
  231. //
  232. //  Return: Result of adding the record to the table
  233. //
  234. //  Description:
  235. //          This function will add a record to the given TEXT table.
  236. //=====================================================================
  237. DBIResult
  238. AddRecordToTXT (phDBICur phCur, pCHAR pszName, pCHAR pszPOB,
  239.                 pCHAR pszDate)
  240. {
  241.     DBIResult   rslt;       // Value returned from IDAPI functions
  242.     CURProps    TblProps;   // Table properties
  243.     pBYTE       pRecBuf;    // Record buffer
  244.  
  245.     //  Allocate a record buffer
  246.     rslt = DbiGetCursorProps(*phCur, &TblProps);
  247.     ChkRslt(rslt, "GetProps");
  248.  
  249.     pRecBuf = (pBYTE) malloc(TblProps.iRecBufSize * sizeof(BYTE));
  250.     if (pRecBuf == NULL)
  251.     {
  252.         Screen("Out of memory");
  253.         return DBIERR_NOMEMORY;
  254.     }
  255.  
  256.     //  Make sure we're starting with a clean record buffer
  257.     rslt = DbiInitRecord(*phCur, pRecBuf);
  258.     ChkRslt(rslt, "InitRecord");
  259.  
  260.     // Name
  261.     rslt = DbiPutField(*phCur, 1, pRecBuf, (pBYTE) pszName);
  262.     ChkRslt(rslt, "PutField");
  263.  
  264.     // Date of birth
  265.     rslt = DbiPutField(*phCur, 2,  pRecBuf, (pBYTE) pszDate);
  266.     ChkRslt(rslt, "PutField");
  267.  
  268.     // Place fo Birth
  269.     rslt = DbiPutField(*phCur, 3,  pRecBuf, (pBYTE) pszPOB);
  270.     ChkRslt(rslt, "PutField");
  271.  
  272.     rslt = DbiInsertRecord(*phCur, dbiNOLOCK, pRecBuf);
  273.     ChkRslt(rslt, "InsertRec");
  274.  
  275.     free(pRecBuf);
  276.     return rslt;
  277. }
  278.  
  279. //=====================================================================
  280. //  Function:
  281. //          CreateImportDBTable(phDb);
  282. //
  283. //  Input:  phDb    - Pointer to the database handle.
  284. //
  285. //  Return: result of the table initialization.
  286. //
  287. //  Description:
  288. //          This function creates a dBASE table.
  289. //=====================================================================
  290. DBIResult
  291. CreateImportDBTable (phDBIDb phDb)
  292. {
  293.     DBIResult   rslt;               // Value returned from IDAPI functions
  294.     CRTblDesc   crTblDsc;           // Table descriptor
  295.     BOOL        bOverWrite = TRUE;  // Overwrite, yes/no flag
  296.  
  297.     // The number of fields in the table.
  298.     const UINT16 uNumFields = sizeof(DBfldDesc) / sizeof (DBfldDesc[0]);
  299.  
  300.     // Initialize the create table descriptor.
  301.     memset(&crTblDsc, 0, sizeof(CRTblDesc));
  302.     strcpy(crTblDsc.szTblName, szDBTblName);
  303.     strcpy(crTblDsc.szTblType, szDBTblType);
  304.     crTblDsc.iFldCount     = uNumFields;
  305.     crTblDsc.pfldDesc      = DBfldDesc;
  306.  
  307.     rslt = DbiCreateTable(*phDb, bOverWrite, &crTblDsc);
  308.     if (ChkRslt(rslt, "CreateTable") != DBIERR_NONE)
  309.     {
  310.         return rslt;
  311.     }
  312.  
  313.     return rslt;
  314. }
  315.