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

  1. // BDE - (C) Copyright 1995 by Borland International
  2.  
  3. // search.c
  4. #include "snipit.h"
  5.  
  6. static const char szTblName[] = "customer";
  7. static const char szTblType[] = szPARADOX;
  8.  
  9. //========================================================================
  10. //  Function:
  11. //          SearchTable();
  12. //
  13. //  Description:
  14. //          This sample code will open a customer table and search it
  15. //          for a key value.
  16. //========================================================================
  17. void
  18. TableSearch (void)
  19. {
  20.     DBIResult   rslt;                 // Value returned from IDAPI
  21.                                       //   functions
  22.     hDBIDb      hDb;                  // Handle to the database
  23.     hDBICur     hCur;                 // Handle to the table
  24.     CURProps    TblProps;             // Table properties
  25.     pBYTE       pBuf = NULL;          // Pointer to the record buffer
  26.     FLOAT       fCustNum = 3052.00;   // FLOAT value to seach for
  27.     CHAR        szKey[] = "Sarasota"; // CHAR value to search for
  28.  
  29.     Screen("*** Locating A Key Value In A Table ***\r\n");
  30.  
  31.     BREAK_IN_DEBUGGER();
  32.  
  33.     Screen("    Initializing IDAPI...");
  34.     if (InitAndConnect(&hDb) != DBIERR_NONE)
  35.     {                                        
  36.         Screen("\r\n*** End of Example ***");
  37.         return;
  38.     }
  39.  
  40.     Screen("    Setting the database directory...");
  41.     rslt = DbiSetDirectory(hDb, (pCHAR) szTblDirectory);
  42.     ChkRslt(rslt, "SetDirectorry");
  43.  
  44.     Screen("    Opening the Customer table...");
  45.     rslt = DbiOpenTable(hDb, (pCHAR) szTblName, (pCHAR) szTblType,
  46.                         NULL, NULL, NULL, dbiREADWRITE, dbiOPENSHARED,
  47.                         xltFIELD, FALSE, NULL, &hCur);
  48.     if (ChkRslt(rslt, "OpenTable") != DBIERR_NONE)
  49.     {
  50.         CloseDbAndExit(&hDb);
  51.         Screen("\r\n*** End of Example ***");
  52.         return;
  53.     }
  54.  
  55.     // Allocate a record buffer.
  56.     rslt = DbiGetCursorProps(hCur,&TblProps);
  57.     ChkRslt(rslt, "GetCursorProps");
  58.  
  59.     pBuf = (pBYTE) malloc(TblProps.iRecBufSize);
  60.     if (pBuf == NULL)
  61.     {
  62.         Screen("    Error - Out of memory");
  63.         CloseDbAndExit(&hDb);
  64.         Screen("\r\n*** End of Example ***");
  65.         return;
  66.     }
  67.  
  68.     rslt = DbiPutField(hCur, 1, pBuf, (pBYTE) &fCustNum);
  69.     ChkRslt(rslt, "PutField");
  70.  
  71.     // Example 1: Search for full key value using a record buffer.
  72.     Screen("    Searching for the record where the 'Customer Number' field"
  73.            " = %f...", fCustNum);
  74.     rslt = DbiSetToKey(hCur, keySEARCHEQ, FALSE, 0, 0, pBuf);
  75.     if (rslt == DBIERR_NONE)
  76.     {
  77.         // Cursor is left on the crack before the record.
  78.         Screen("\r\n    Record was found:");
  79.         DisplayNextRecord(hCur);
  80.     }
  81.     else
  82.     {
  83.         Screen("\r\n    Record was not found...");
  84.         ChkRslt(rslt, "SetToKey");
  85.     }
  86.  
  87.     // Example 2: Search on a partial key value using a direct buffer.
  88.     Screen("\r\n    Switching to the \"Place\" index...");
  89.  
  90.     rslt = DbiSwitchToIndex(&hCur, "Place", NULL, NULL, FALSE);
  91.     ChkRslt(rslt, "SwitchToIndex");
  92.  
  93.     Screen("    Searching for the record using the \"Place\" index"
  94.            " where\r\n         the 'City' field = %s...",szKey);
  95.     rslt = DbiGetRecordForKey(hCur, TRUE, 0, strlen(szKey),
  96.                               (pBYTE) szKey, NULL);
  97.     if (rslt == DBIERR_NONE)
  98.     {
  99.         // Cursor is left on the record.
  100.         Screen("\r\n    Record was found:");
  101.         DisplayCurrentRecord(hCur);
  102.     }
  103.     else
  104.     {
  105.         Screen("\r\n    Record was not found... ");
  106.         ChkRslt(rslt, "GetRecordForKey");
  107.     }
  108.  
  109.     Screen("\r\n    Close the table...");
  110.     rslt = DbiCloseCursor(&hCur);
  111.     ChkRslt(rslt, "CloseCursor");
  112.  
  113.     free(pBuf);
  114.  
  115.     Screen("    Close the database and exit IDAPI...");
  116.     CloseDbAndExit(&hDb);
  117.  
  118.     Screen("\r\n*** End of Example ***");
  119. }
  120.  
  121.  
  122.