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

  1. // BDE32 3.x - (C) Copyright 1996 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. static const CHAR szKey[] = "Sarasota";     // CHAR value to search for
  9. static const DFLOAT fCustNum = 3052.00;     // DFLOAT value to seach for
  10.  
  11. //========================================================================
  12. //  Function:
  13. //          SearchTable();
  14. //
  15. //  Description:
  16. //          This sample code will open a customer table and search it
  17. //          for a key value.
  18. //========================================================================
  19. void
  20. TableSearch (void)
  21. {
  22.     hDBIDb      hDb = 0;              // Handle to the database
  23.     hDBICur     hCur = 0;             // Handle to the table
  24.     DBIResult   rslt;                 // Value returned from IDAPI functions
  25.     CURProps    TblProps;             // Table properties
  26.     pBYTE       pBuf;                 // Pointer to the record buffer
  27.  
  28.     Screen("*** Locating A Key Value In A Table ***\r\n");
  29.  
  30.     BREAK_IN_DEBUGGER();
  31.  
  32.     Screen("    Initializing IDAPI...");
  33.     if (InitAndConnect(&hDb) != DBIERR_NONE)
  34.     {
  35.         Screen("\r\n*** End of Example ***");
  36.         return;
  37.     }
  38.  
  39.     Screen("    Setting the database directory...");
  40.     rslt = DbiSetDirectory(hDb, (pCHAR)szTblDirectory);
  41.     ChkRslt(rslt, "SetDirectorry");
  42.  
  43.     Screen("    Opening the Customer Table...");
  44.     rslt = DbiOpenTable(hDb, (pCHAR)szTblName, (pCHAR)szTblType, NULL, NULL,
  45.                         NULL, dbiREADWRITE, dbiOPENSHARED, xltFIELD, FALSE,
  46.                         NULL, &hCur);
  47.     if (ChkRslt(rslt, "OpenTable") != DBIERR_NONE)
  48.     {
  49.         CloseDbAndExit(&hDb);
  50.         Screen("\r\n*** End of Example ***");
  51.         return;
  52.     }
  53.  
  54.     // Get the properties of the table in order to create the correct
  55.     //   buffer size for the record.
  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, (UINT16)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.