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

  1. // BDE32 3.x - (C) Copyright 1996 by Borland International
  2.  
  3. // lsqllive.c
  4. #include "snipit.h"
  5.  
  6. // Name of table to be created.
  7. static const char szTblName[] = "cust";
  8. // Table type to use.
  9. static const char szTblType[] = szDBASE;
  10.  
  11. //=====================================================================
  12. //  Function:
  13. //          LiveSQL();
  14. //
  15. //  Description:
  16. //          This example shows how to do live local SQL on a dBASE table.
  17. //=====================================================================
  18. void
  19. LiveSQL (void)
  20. {
  21.     hDBIDb          hDb;        // Handle to the database
  22.     hDBICur         hCur;       // Handle to the answer table
  23.     hDBIStmt        hStmt;      // Handle to the SQL statement
  24.     hDBICur         hMCur;      // Handle to the master table.
  25.     CHAR            szQry[]     = { // The text of the SQL statement
  26.                 "SELECT cust_no, name, city, phone, country \r\n"
  27.                 "FROM \"cust.dbf\" \r\n"
  28.                 "WHERE cust_no>4000"
  29.                                   };
  30.     BOOL            bLive = TRUE;
  31.     CURProps        TblProps;       // Table descriptor
  32.     CURProps        TblMProps;      // Table descriptor
  33.     pBYTE           pRecBuf;        // Pointer to the record buffer
  34.     pBYTE           pMRecBuf;       // Pointer to the record buffer
  35.     BYTE            szCity[20];
  36.     BOOL            bEmpty;
  37.     DFLOAT          ulCustNum = 0.0;
  38.     BYTE            szNewCity[]="Chicago";
  39.     DBIResult       rslt;
  40.  
  41.     Screen("*** Live Answer Table Example ***\r\n");
  42.  
  43.     BREAK_IN_DEBUGGER();
  44.  
  45.     Screen("    Initializing IDAPI...");
  46.     if (InitAndConnect(&hDb) != DBIERR_NONE)
  47.     {                                        
  48.         Screen("\r\n*** End of Example ***");
  49.         return;
  50.     }
  51.                              
  52.     Screen("    Setting the database directory...");
  53.     rslt = DbiSetDirectory(hDb, (pCHAR) szTblDirectory);
  54.     ChkRslt(rslt, "SetDirectory");
  55.  
  56.     Screen("    Perform the following SQL statement on the table:\r\n");
  57.     Screen(szQry);
  58.  
  59.     Screen("\r\n    Allocate the statement handle...");
  60.     rslt = DbiQAlloc(hDb, qrylangSQL, &hStmt);
  61.     if (ChkRslt(rslt, "QAlloc") != DBIERR_NONE)
  62.     {
  63.         CloseDbAndExit(&hDb);
  64.         Screen("\r\n*** End of Example ***");
  65.         return;
  66.     }
  67.  
  68.  
  69.     Screen("    Prepare the SQL statement...");
  70.     rslt = DbiQPrepare(hStmt, szQry);
  71.     if (ChkRslt(rslt, "QPrepare") != DBIERR_NONE)
  72.     {
  73.         CloseDbAndExit(&hDb);
  74.         Screen("\r\n*** End of Example ***");
  75.         return;
  76.     }
  77.  
  78.     // Check if the statement cursor is live.
  79.     if (bLive != wantLIVE)
  80.     {
  81.         // Now set the statement cursor to be live.
  82.         rslt = DbiSetProp((hDBIObj) hStmt, stmtLIVENESS, wantLIVE);
  83.         ChkRslt(rslt, "SetProp");
  84.     }
  85.  
  86.     Screen("    Execute the SQL statement...");
  87.     rslt = DbiQExec(hStmt, &hCur);
  88.     if (ChkRslt(rslt, "QExec") != DBIERR_NONE)
  89.     {
  90.         CloseDbAndExit(&hDb);
  91.         Screen("\r\n*** End of Example ***");
  92.         return;
  93.     }
  94.  
  95.     // Check for a valid cursor
  96.     if (hCur)
  97.     {
  98.         rslt = DbiGetCursorProps(hCur, &TblProps);
  99.         ChkRslt(rslt, "GetCursorProps");
  100.  
  101.         // Check if the answer set is live (exit example if not)....
  102.         if (TblProps.bTempTable == TRUE)
  103.         {
  104.             Screen("\r\n    Could not get a live answer set...");
  105.             rslt = DbiCloseCursor(&hCur);
  106.             ChkRslt(rslt, "CloseCursor");
  107.             CloseDbAndExit(&hDb);
  108.             Screen("\r\n*** End of Example ***");
  109.             return;
  110.         }
  111.         
  112.         Screen("    Opening the master table...");
  113.         rslt = DbiOpenTable(hDb, (pCHAR) szTblName, (pCHAR) szTblType,
  114.                             NULL, NULL, 0, dbiREADWRITE, dbiOPENSHARED,
  115.                             xltFIELD, FALSE, NULL, &hMCur);
  116.         if (ChkRslt(rslt, "OpenTable") != DBIERR_NONE)
  117.         {
  118.             rslt = DbiCloseCursor(&hCur);
  119.             ChkRslt(rslt, "CloseCursor");
  120.             CloseDbAndExit(&hDb);
  121.             Screen("\r\n*** End of Example ***");
  122.             return;
  123.         }
  124.  
  125.         // Get the table properties so we can allocate memory for the record
  126.         // buffer.
  127.         rslt = DbiGetCursorProps(hMCur, &TblMProps);
  128.         ChkRslt(rslt, "GetCursorProps");
  129.  
  130.         // Allocate memory for the record buffer.
  131.         pMRecBuf = (pBYTE) malloc(TblMProps.iRecBufSize);
  132.         if (!pMRecBuf)
  133.         {
  134.             rslt = DbiCloseCursor(&hCur);
  135.             ChkRslt(rslt, "CloseCursor");
  136.             rslt = DbiCloseCursor(&hMCur);
  137.             ChkRslt(rslt, "CloseCursor");
  138.             CloseDbAndExit(&hDb);
  139.             Screen("\r\n*** End of Example ***");
  140.             return;
  141.         }
  142.  
  143.         Screen("\r\n    Switching to an index in the master table... ");
  144.         rslt = DbiSwitchToIndex(&hMCur, "CUST.MDX", "CUST_NO", NULL, FALSE);
  145.         ChkRslt(rslt, "SwitchToIndex");
  146.  
  147.         Screen("    Clear the record buffer...");
  148.         rslt = DbiInitRecord(hMCur, pMRecBuf);
  149.         ChkRslt(rslt, "InitRecord");
  150.  
  151.         Screen("    Display the first record in the answer table...");
  152.         rslt = DbiSetToBegin(hCur);
  153.         ChkRslt(rslt, "SetToBegin");
  154.         DisplayNextRecord(hCur);
  155.  
  156.         // Modify the table and change back to the original
  157.         // values.
  158.  
  159.         // Allocate memory for the record buffer.
  160.         pRecBuf = (pBYTE) malloc(TblProps.iRecBufSize);
  161.         if (!pRecBuf)
  162.         {
  163.             free(pMRecBuf);
  164.             rslt = DbiCloseCursor(&hCur);
  165.             ChkRslt(rslt, "CloseCursor");
  166.             rslt = DbiCloseCursor(&hMCur);
  167.             ChkRslt(rslt, "CloseCursor");
  168.             CloseDbAndExit(&hDb);
  169.             Screen("\r\n*** End of Example ***");
  170.             return;
  171.         }
  172.  
  173.         // Get the first record in the live query.
  174.         rslt = DbiSetToBegin(hCur);
  175.         ChkRslt(rslt, "SetToBegin");
  176.  
  177.         rslt = DbiGetNextRecord(hCur, dbiNOLOCK, pRecBuf, NULL);
  178.         ChkRslt(rslt, "GetNextRecord");
  179.  
  180.         // Get the master table's fourth field (which is the 3rd field for
  181.         // the answer table) value so that it can be restored after
  182.         // modifying the table.
  183.         rslt = DbiGetField(hCur, 3, pRecBuf, szCity, &bEmpty);
  184.         ChkRslt(rslt, "GetField");
  185.  
  186.         rslt = DbiGetField(hCur, 1, pRecBuf, (pBYTE) &ulCustNum, &bEmpty);
  187.         ChkRslt(rslt, "GetField");
  188.  
  189.         // Modify the City of the first record.
  190.         rslt = DbiPutField(hCur, 3, pRecBuf, szNewCity);
  191.         ChkRslt(rslt, "PutField");
  192.  
  193.         Screen("\r\n    Modifying the City field in the first record "
  194.                "of the DynaSet from %s to %s....", szCity, szNewCity);
  195.  
  196.         rslt = DbiModifyRecord(hCur, pRecBuf, TRUE);
  197.         ChkRslt(rslt, "ModifyRecord");
  198.  
  199.         Screen("    Re-displaying the modified record of the master table "
  200.                "to show the change took place...");
  201.  
  202.         // Set the value for the search into the record buffer.
  203.         rslt = DbiPutField(hMCur, 1, pMRecBuf, (pBYTE)&ulCustNum);
  204.         ChkRslt(rslt, "PutField");
  205.  
  206.         // Set the cursor to the field in the master table - to verify
  207.         // that it was modified.
  208.         rslt = DbiSetToKey(hMCur, keySEARCHEQ, FALSE, NULL, NULL, pMRecBuf);
  209.         ChkRslt(rslt, "SetToKey");
  210.  
  211.         DisplayNextRecord(hMCur);
  212.  
  213.         rslt = DbiSetToBegin(hCur);
  214.         ChkRslt(rslt, "SetToBegin");
  215.  
  216.         // Change the data back to the way it was.
  217.         rslt = DbiGetNextRecord(hCur, dbiNOLOCK, pRecBuf, NULL);
  218.         ChkRslt(rslt, "GetRecord");
  219.  
  220.         rslt = DbiPutField(hCur, 3, pRecBuf, szCity);
  221.         ChkRslt(rslt, "PutField");
  222.  
  223.         Screen("\r\n    Saving back the original City name to keep the "
  224.                "example table consistent...");
  225.         rslt = DbiModifyRecord(hCur, pRecBuf, TRUE);
  226.         ChkRslt(rslt, "ModifyRecord");
  227.  
  228.         Screen("\r\n    Close the cursor to the answer set...");
  229.         rslt = DbiCloseCursor(&hCur);
  230.         ChkRslt(rslt, "CloseCursor");                                  
  231.  
  232.         Screen("\r\n    Close the cursor to the master table...");
  233.         rslt = DbiCloseCursor(&hMCur);
  234.         ChkRslt(rslt, "CloseCursor");
  235.  
  236.         free(pRecBuf);
  237.         free(pMRecBuf);
  238.     }
  239.     else
  240.     {
  241.         Screen("        Could not get cursor to the answer table");
  242.     }
  243.  
  244.     Screen("    Release memory allocated for the query...");
  245.     rslt = DbiQFree(&hStmt);
  246.     ChkRslt(rslt, "QryFree");
  247.  
  248.     Screen("    Close the database and exit IDAPI...");
  249.     CloseDbAndExit(&hDb);
  250.  
  251.     Screen("\r\n*** End of Example ***");
  252. }
  253.  
  254.