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

  1. // BDE32 3.x - (C) Copyright 1996 by Borland International
  2.  
  3. // TextExp.c
  4. #include "snipit.h"
  5.  
  6. static const char szPXTblName[] = "CUSTOMER.DB";
  7. static const char szPXTblType[] = szPARADOX;
  8.  
  9. static const char szTXTTblName[] = "CUSTOMER.TXT";
  10. static const char szTXTTblType[] = szASCII;
  11.  
  12. //=====================================================================
  13. //  Function:
  14. //          TextExport();
  15. //
  16. //  Description:
  17. //          This example shows exporting data from a Paradox table
  18. //          to a text file. The "customer.db" table will be used as the
  19. //          source table, and a "customer.txt" table will be created as
  20. //          the destination table. The DbiBatchMove function is used to
  21. //          copy the records from one table to another.
  22. //=====================================================================
  23. void
  24. TextExport (void)
  25. {
  26.     CURProps        PDTblProps;         // Table properties
  27.     hDBIDb          hDb;                // Handle to the database
  28.     hDBICur         PDhCur;             // Paradox cursor handle 
  29.     hDBICur         TXThCur;            // Text cursor handle
  30.     pFLDDesc        pPDFlds;            // List of Paradox fields
  31.     pFLDDesc        pTXTFlds;           // List of text fields
  32.     CRTblDesc       crTblDsc;           // Table descriptor
  33.     BOOL            bOverWrite = TRUE;  // Overwrite, yes/no flag
  34.     DBIPATH         filename;           // Text table name.
  35.     DBIResult       rslt;               // Return value from IDAPI functions
  36.  
  37.     Screen("*** Text Export Example ***\r\n");
  38.  
  39.     BREAK_IN_DEBUGGER();
  40.  
  41.     Screen("    Initializing IDAPI...");
  42.     if (InitAndConnect(&hDb) != DBIERR_NONE)
  43.     {
  44.         Screen("\r\n*** End of Example ***");
  45.         return;
  46.     }
  47.  
  48.     Screen("    Setting the database directory...");
  49.     rslt = DbiSetDirectory(hDb, (pCHAR) szTblDirectory);
  50.     ChkRslt(rslt, "SetDirectory");
  51.  
  52.     // Initialize the table descriptor
  53.     memset(&crTblDsc, 0, sizeof(CRTblDesc));
  54.     strcpy(crTblDsc.szTblName, szTXTTblName) ;
  55.     strcpy(crTblDsc.szTblType, szTXTTblType) ;
  56.  
  57.     Screen("    Creating the text table...");
  58.     rslt = DbiCreateTable(hDb, bOverWrite, &crTblDsc);
  59.     if (ChkRslt(rslt, "CreateTable") != DBIERR_NONE)
  60.     {
  61.         CloseDbAndExit(&hDb);
  62.         Screen("\r\n*** End of Example ***");
  63.         return;
  64.     }
  65.  
  66.     Screen("\r\n    Opening the %s Paradox table...", szPXTblName);
  67.     rslt = DbiOpenTable(hDb, (pCHAR) szPXTblName, (pCHAR) szPXTblType,
  68.                         NULL, NULL, 0, dbiREADWRITE, dbiOPENSHARED,
  69.                         xltFIELD, FALSE, NULL, &PDhCur);
  70.     if (ChkRslt(rslt, "OpenTable") != DBIERR_NONE)
  71.     {
  72.         CloseDbAndExit(&hDb);
  73.         Screen("\r\n*** End of Example ***");
  74.         return;
  75.     }
  76.  
  77.     // Note that the table type has to be specified with the delimiter
  78.     // and separator since we are opening the table as a delimited text
  79.     // file.
  80.     Screen("    Opening the %s Text table...\r\n", szTXTTblName);
  81.     rslt = DbiOpenTable(hDb, (pCHAR) szTXTTblName, "ASCIIDRV-\"-,",
  82.                         NULL, NULL, 0, dbiREADWRITE, dbiOPENSHARED,
  83.                         xltFIELD, FALSE, NULL, &TXThCur);
  84.     if (ChkRslt(rslt, "OpenTable") != DBIERR_NONE)
  85.     {
  86.         rslt = DbiCloseCursor(&PDhCur);
  87.         ChkRslt(rslt, "CloseCursor");
  88.         CloseDbAndExit(&hDb);
  89.         Screen("\r\n*** End of Example ***");
  90.         return;
  91.     }
  92.  
  93.     rslt = DbiGetCursorProps(PDhCur, &PDTblProps);
  94.     ChkRslt(rslt, "GetProps");
  95.  
  96.     // Allocate memory for field descriptors.
  97.     pPDFlds = (pFLDDesc) malloc(PDTblProps.iFields * sizeof( FLDDesc));
  98.     pTXTFlds = (pFLDDesc) malloc(PDTblProps.iFields * sizeof( FLDDesc));
  99.     if ((pTXTFlds == NULL) || (pPDFlds == NULL))
  100.     {
  101.         Screen("    Error - Out of memory");
  102.         if (pPDFlds) free(pPDFlds);
  103.         if (pTXTFlds) free(pTXTFlds);
  104.         DbiCloseCursor(&PDhCur);
  105.         DbiCloseCursor(&TXThCur);
  106.         CloseDbAndExit(&hDb);
  107.         Screen("\r\n*** End of Example ***");
  108.         return;
  109.     }
  110.  
  111.     // Change the translation mode to xltNONE to get the physical 
  112.     // field descriptors.
  113.     rslt = DbiSetProp(PDhCur, curXLTMODE, xltNONE);
  114.     ChkRslt(rslt, "SetProp");
  115.  
  116.     // Get the physical field descriptors of the Paradox table.
  117.     rslt = DbiGetFieldDescs(PDhCur, pPDFlds);
  118.     ChkRslt(rslt, "GetFieldDescs");
  119.  
  120.     // Change the translation mode to back to xltFIELD.
  121.     rslt = DbiSetProp(PDhCur, curXLTMODE, xltFIELD);
  122.     ChkRslt(rslt, "SetProp");
  123.  
  124.     // Convert the record structure to the text table format.
  125.     rslt = DbiTranslateRecordStructure(PDTblProps.szTableType,
  126.                                        PDTblProps.iFields, pPDFlds,
  127.                                        (pCHAR) szTXTTblType,
  128.                                        NULL, pTXTFlds, FALSE);
  129.     ChkRslt(rslt, "TranslateRecordStruct");
  130.  
  131.     rslt = DbiSetFieldMap(TXThCur, PDTblProps.iFields, pTXTFlds);
  132.     if (ChkRslt(rslt, "SetFieldMap") != DBIERR_NONE)
  133.     {
  134.         free(pTXTFlds);
  135.         free(pPDFlds);
  136.         DbiCloseCursor(&PDhCur);
  137.         DbiCloseCursor(&TXThCur);
  138.         CloseDbAndExit(&hDb);
  139.         Screen("\r\n*** End of Example ***");
  140.         return;
  141.     }
  142.  
  143.     rslt = DbiSetToBegin(PDhCur);
  144.     ChkRslt(rslt, "SetToBegin");
  145.  
  146.     rslt = DbiSetToBegin(TXThCur);
  147.     ChkRslt(rslt, "SetToBegin");
  148.  
  149.     rslt = DbiBatchMove(NULL, PDhCur, NULL, TXThCur, batAPPEND,
  150.                         0, NULL, NULL, NULL, 0, NULL, NULL,
  151.                         NULL, NULL, NULL, NULL,
  152.                         FALSE, FALSE, NULL, TRUE);
  153.     ChkRslt(rslt, "Batch Move");
  154.  
  155.     rslt = DbiSetToBegin(TXThCur);
  156.     ChkRslt(rslt, "SetToBegin");
  157.  
  158.     Screen("    Displaying the first 10 records of the %s text table...",
  159.            szTXTTblType);
  160.     DisplayInMemoryTable(TXThCur, 10);
  161.  
  162.     rslt = DbiGetProp(TXThCur, curFILENAME, &filename, sizeof(DBIPATH),
  163.                       NULL);
  164.     ChkRslt(rslt, "GetProp");
  165.  
  166.     Screen("\r\n    The TEXT table is at: %s", filename);
  167.  
  168.     // Cleanup
  169.     free(pPDFlds);
  170.     free(pTXTFlds);
  171.  
  172.     Screen("\r\n    Closing the TEXT table...");
  173.     rslt = DbiCloseCursor(&TXThCur);
  174.     ChkRslt(rslt, "CloseCur");
  175.  
  176.     Screen("\r\n    Closing the PARADOX table...");
  177.     rslt = DbiCloseCursor(&PDhCur);
  178.     ChkRslt(rslt, "CloseCur");
  179.  
  180.     Screen("    Close the database and exit IDAPI...");
  181.     CloseDbAndExit(&hDb);
  182.  
  183.     Screen("\r\n*** End of Example ***");
  184. }
  185.