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

  1. // BDE32 3.x - (C) Copyright 1996 by Borland International
  2.  
  3. // qbe2.c       
  4. #include "snipit.h"
  5.  
  6. //=====================================================================
  7. //  Function:
  8. //          QBE2();
  9. //
  10. //  Description:
  11. //          This example shows how to query non-SQL tables using the
  12. //          DbiQExec function.  
  13. //=====================================================================
  14. void
  15. QBE2 (void)
  16. {
  17.     hDBIDb          hDb;        // Handle to the database
  18.     hDBICur         hCur;       // Cursor to the result set
  19.     hDBIStmt        hStrmt;     // Handle to the query
  20.     CHAR            szQry[]     = { // The Text of the query
  21.                 "CUST.DBF   | Cust_No | Name  | Phone | State_prov |\r\n"
  22.                 "           |    _x   | Check | Check |  = CA |\r\n"
  23.                 "\r\n"
  24.                 "ORDERS.DB  | Customer No | Ship Date |\r\n"
  25.                 "           |     _x      |   Check   |\r\n"
  26.                                   };
  27.     DBIResult       rslt;       // Return value from IDAPI functions
  28.  
  29.     Screen("*** Expanded 'Query by Example' Example ***\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.     Screen("    Setting the database directory...");
  40.     rslt = DbiSetDirectory(hDb, (pCHAR) szTblDirectory);
  41.     Screen("  THE TABLE DIRECTORY IS --- %s", szTblDirectory);
  42.     ChkRslt(rslt, "SetDirectory");
  43.  
  44.     Screen("    Perform the following query on the table:\r\n");
  45.     Screen(szQry);
  46.  
  47.     Screen("    Allocate a statment handle...");
  48.     rslt = DbiQAlloc(hDb, qrylangQBE, &hStrmt);
  49.     if (ChkRslt(rslt, "QAlloc") != DBIERR_NONE)
  50.     {
  51.         CloseDbAndExit(&hDb);
  52.         Screen("\r\n*** End of Example ***");
  53.         return;
  54.     }
  55.  
  56.     Screen("    Prepare the query command...");
  57.     rslt = DbiQPrepare(hStrmt, szQry);
  58.     if (ChkRslt(rslt, "QPrepare") != DBIERR_NONE)
  59.     {
  60.         CloseDbAndExit(&hDb);
  61.         Screen("\r\n*** End of Example ***");
  62.         return;
  63.     }
  64.  
  65.     Screen("    Execute the query...");
  66.     rslt = DbiQExec(hStrmt, &hCur);
  67.     if (ChkRslt(rslt, "QExec") != DBIERR_NONE)
  68.     {
  69.         CloseDbAndExit(&hDb);
  70.         Screen("\r\n*** End of Example ***");
  71.         return;
  72.     }
  73.  
  74.     // Check for a valid cursor
  75.     if (hCur)
  76.     {
  77.         Screen("    Display the first 10 records in the answer table...");
  78.  
  79.         rslt = DbiSetToBegin(hCur);
  80.         ChkRslt(rslt, "SetToBegin");
  81.         DisplayTable(hCur, 10);
  82.  
  83.         Screen("\r\n    Close the cursor to the answer set...");
  84.         rslt = DbiCloseCursor(&hCur);
  85.         ChkRslt(rslt, "CloseCursor");
  86.     }
  87.     else
  88.     {
  89.         Screen("        Could not get cursor to the answer table");
  90.     }
  91.  
  92.     Screen("    Release memory allocated for the query...");
  93.     rslt = DbiQFree(&hStrmt);
  94.     ChkRslt(rslt, "QFree");
  95.     
  96.     Screen("    Close the database and exit IDAPI...");
  97.     CloseDbAndExit(&hDb);
  98.  
  99.     Screen("\r\n*** End of Example ***");
  100. }
  101.  
  102.