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

  1. // BDE - (C) Copyright 1995 by Borland International
  2.  
  3. // lsqljoin.c       
  4. #include "snipit.h"
  5.  
  6. //=====================================================================
  7. //  Function:
  8. //          LocalSQLJoin();
  9. //
  10. //  Description:
  11. //          This example shows how to do local SQL on a dBASE table.
  12. //=====================================================================
  13. void
  14. LocalSQLJoin (void)
  15. {
  16.     DBIResult       rslt;       // Return value from IDAPI functions
  17.     hDBIDb          hDb;        // Handle to the database
  18.     hDBICur         hCur;       // Handle to the answer table
  19.     hDBIStmt        hStmt;      // Handle to the SQL statement
  20.     CHAR            szQry[]     = { // The text of the SQL statement
  21.            "SELECT c.name, c.city, o.'total invoice', o.'amount paid'\r\n"
  22.            "FROM   cust c, orders o \r\n"
  23.            "WHERE  o.'customer no' = c.cust_no and\r\n"
  24.            "       c.cust_no < 2000\r\n"
  25.            "ORDER BY c.name"
  26.                                   };
  27.  
  28.     Screen("*** Local SQL Example ***\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, "SetDirectory");
  42.  
  43.     Screen("    Perform the following SQL statement on the table:\r\n");
  44.     Screen(szQry);
  45.  
  46.     Screen("\r\n    Prepare the SQL statement...");
  47.     rslt = DbiQPrepare(hDb, qrylangSQL, szQry, &hStmt);
  48.     if (ChkRslt(rslt, "QPrepare") != DBIERR_NONE)
  49.     {
  50.         CloseDbAndExit(&hDb);
  51.         Screen("\r\n*** End of Example ***");
  52.         return;
  53.     }
  54.  
  55.     Screen("    Execute the SQL statement...");
  56.     rslt = DbiQExec(hStmt, &hCur);
  57.     if (ChkRslt(rslt, "QExec") != DBIERR_NONE)
  58.     {
  59.         CloseDbAndExit(&hDb);
  60.         Screen("\r\n*** End of Example ***");
  61.         return;
  62.     }
  63.  
  64.     if (hCur)
  65.     {
  66.         // If the cursor is valid, display the information.
  67.         Screen("    Display the first 10 Records in the answer table...");
  68.         rslt = DbiSetToBegin(hCur);
  69.         ChkRslt(rslt, "SetToBegin");
  70.         DisplayTable(hCur, 10);
  71.  
  72.         Screen("\r\n    Close the cursor to the answer set...");
  73.         rslt = DbiCloseCursor(&hCur);
  74.         ChkRslt(rslt, "CloseCursor");
  75.     }
  76.     else
  77.     {
  78.         Screen("        Could not get cursor to the answer table");
  79.     }
  80.  
  81.     Screen("    Release memory allocated for the query...");
  82.     rslt = DbiQFree(&hStmt);
  83.     ChkRslt(rslt, "QryFree");
  84.  
  85.     Screen("    Close the database and exit IDAPI...");
  86.     CloseDbAndExit(&hDb);
  87.  
  88.     Screen("\r\n*** End of Example ***");
  89. }
  90.