SQLFetchTest.cpp
上传用户:romrleung
上传日期:2022-05-23
资源大小:18897k
文件大小:13k
源码类别:

MySQL数据库

开发平台:

Visual C++

  1. /* Copyright (C) 2003 MySQL AB
  2.    This program is free software; you can redistribute it and/or modify
  3.    it under the terms of the GNU General Public License as published by
  4.    the Free Software Foundation; either version 2 of the License, or
  5.    (at your option) any later version.
  6.    This program is distributed in the hope that it will be useful,
  7.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  8.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  9.    GNU General Public License for more details.
  10.    You should have received a copy of the GNU General Public License
  11.    along with this program; if not, write to the Free Software
  12.    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */
  13.  /**
  14.  * @file SQLFetchTest.cpp
  15.  */
  16. #include <common.hpp>
  17. #define F_MESSAGE_LENGTH 200
  18. using namespace std;
  19. #define F_NAME_LEN 20
  20. #define F_PHONE_LEN 20
  21. #define F_ADDRESS_LEN 20
  22. SQLHSTMT    F_hstmt;
  23. SQLHDESC    F_hdbc;
  24. SQLHENV     F_henv;
  25. SQLHDESC    F_hdesc;
  26. void SQLFetchTest_DisplayError(SQLSMALLINT F_HandleType, 
  27.        SQLHDESC F_InputHandle);
  28. /** 
  29.  * Test to advance a cursor to the next row of data in a data result set
  30.  * and to retrieve data from any bound columns that exist for that row 
  31.  * into their associated application variables
  32.  *
  33.  * Tests:
  34.  * _# Test1 Execute statements and display the results 
  35.  * -# Test2 There is no executed statement 
  36.  * @return Zero, if test succeeded
  37.  */
  38. int SQLFetchTest()
  39. {
  40.   SQLRETURN retcode;
  41.   SQLCHAR  SQLStmt[120];
  42.   SQLCHAR  SQLStmt1[120];
  43.   SQLCHAR  SQLStmt2[120];
  44.   SQLCHAR  SQLStmt3[120];
  45.   SQLCHAR  SQLStmt4[120];
  46.   SQLCHAR  F_CustID[20];
  47.   SQLCHAR  F_Name[F_NAME_LEN], F_Phone[F_PHONE_LEN];
  48.   SQLCHAR  F_Address[F_ADDRESS_LEN];
  49.   ndbout << "Start SQLFetch Testing!" << endl;
  50.   //************************************
  51.   //** Allocate An Environment Handle **
  52.   //************************************
  53.   retcode = SQLAllocHandle(SQL_HANDLE_ENV, 
  54.    SQL_NULL_HANDLE, 
  55.    &F_henv);
  56.   
  57. if (retcode == SQL_SUCCESS || retcode == SQL_SUCCESS_WITH_INFO)
  58.     ndbout << "Allocated an environment Handle!" << endl;
  59.   
  60.   //*********************************************
  61.   //** Set the ODBC application Version to 3.x **
  62.   //*********************************************
  63.   retcode = SQLSetEnvAttr(F_henv, 
  64.   SQL_ATTR_ODBC_VERSION, 
  65.   (SQLPOINTER) SQL_OV_ODBC3, 
  66.   SQL_IS_UINTEGER);
  67.   
  68. if (retcode == SQL_SUCCESS || retcode == SQL_SUCCESS_WITH_INFO)
  69.     ndbout << "Set the ODBC application Version to 3.x!" << endl;
  70.   //**********************************
  71.   //** Allocate A Connection Handle **
  72.   //**********************************
  73.   retcode = SQLAllocHandle(SQL_HANDLE_DBC, 
  74.    F_henv, 
  75.    &F_hdbc);
  76. if (retcode == SQL_SUCCESS || retcode == SQL_SUCCESS_WITH_INFO)
  77.     ndbout << "Allocated a connection Handle!" << endl;
  78.   
  79.   // *******************
  80.   // ** Connect to DB **
  81.   // *******************
  82.   retcode = SQLConnect(F_hdbc, 
  83.        (SQLCHAR *) connectString(), 
  84.        SQL_NTS, 
  85.        (SQLCHAR *) "", 
  86.        SQL_NTS, 
  87.        (SQLCHAR *) "", 
  88.        SQL_NTS);
  89.   
  90. if (retcode == SQL_SUCCESS || retcode == SQL_SUCCESS_WITH_INFO)
  91.     ndbout << "Connected to DB : OK!" << endl;
  92.   else 
  93.     {  
  94.       ndbout << "Failure to Connect DB!" << endl;
  95.       return NDBT_FAILED;
  96.     }
  97.   //*******************************
  98.   //** Allocate statement handle **
  99.   //*******************************
  100.   
  101.   retcode = SQLAllocHandle(SQL_HANDLE_STMT, 
  102.    F_hdbc, 
  103.    &F_hstmt); 
  104. if(retcode == SQL_SUCCESS || retcode == SQL_SUCCESS_WITH_INFO) 
  105.     ndbout << "Allocated a statement handle!" << endl;
  106.   
  107.   //************************
  108.   //** Define a statement **
  109.   //************************
  110.   /* *** CustID is Integer *** */
  111.   strcpy((char *) SQLStmt1, "CREATE TABLE Customers (CustID Integer, Name Char(12), Address Char(12), Phone Char(12), Primary Key(CustID, Name))");
  112.   /* *** the primary key is alone *** */
  113. // strcpy((char *) SQLStmt1, "CREATE TABLE Customers (CustID Integer, Name Char(12), Address Char(12), Phone Char(12), Primary Key(CustID))");
  114.    strcpy((char *) SQLStmt2, "INSERT INTO Customers (CustID, Name, Address,Phone) VALUES(188, 'peter','LM Vag8','7190890')");
  115.   /* *** CustID is Float *** */
  116. //  strcpy((char *) SQLStmt1, 
  117. //  "CREATE TABLE Customers (CustID float, Name Char(12), Address Char(12), Phone Char(12), Primary Key(CustID))");
  118. //  strcpy((char *) SQLStmt2, "INSERT INTO Customers (CustID, Name, Address,Phone) VALUES(1.1516, 'peter','LM Vag8','7190890')");
  119.   /* *** CustID is Char *** */
  120.   // strcpy((char *) SQLStmt1, "CREATE TABLE Customers (CustID char(6), Name Char(12), Address Char(12), Phone Char(12), Primary Key(CustID))");
  121.   // strcpy((char *) SQLStmt2, "INSERT INTO Customers (CustID, Name, Address,Phone) VALUES('000001', 'peter','LM Vag8','7190890')");
  122.   /* The UPDATE statements */
  123.   //  strcpy((char *) SQLStmt3, "UPDATE Customers SET Phone = '98998' WHERE CustID = 1.1516");
  124.   //  strcpy((char *) SQLStmt3, "UPDATE Customers SET Phone = '98998' WHERE CustID = '000001'");
  125.   //  strcpy((char *) SQLStmt3, "UPDATE Customers SET Phone = '98998' WHERE CustID = 188"); 
  126.   strcpy((char *) SQLStmt3, "UPDATE Customers SET Phone = '98998' WHERE CustID = 188 AND Name = 'peter'"); 
  127.   // DELETE statements
  128.   //  DELETE all records
  129.   //  strcpy((char *) SQLStmt4, "DELETE FROM Customers");
  130.   //  DELETE One record
  131.   //  strcpy((char *) SQLStmt4, "DELETE FROM Customers WHERE CustID = 1.1516");
  132.   //  strcpy((char *) SQLStmt4, "DELETE FROM Customers WHERE CustID = '000001'");
  133.   //  strcpy((char *) SQLStmt4, "DELETE FROM Customers WHERE CustID = 188 AND Name = 'peter'");
  134.   //  strcpy((char *) SQLStmt4, "DELETE FROM Customers WHERE CustID = 188");
  135.   strcpy((char *) SQLStmt4, "DELETE FROM Customers WHERE CustID = 188 AND Name = 'peter'");
  136.   //SELECT statements
  137.   strcpy((char *) SQLStmt, "SELECT * FROM Customers");
  138.   //********************************
  139.   //** Prepare  CREATE statements **
  140.   //********************************
  141.   ndbout << ">>>>" << (char*)SQLStmt1 << "<<<<" << endl;
  142.   retcode = SQLPrepare(F_hstmt, 
  143.        SQLStmt1, 
  144.        SQL_NTS);
  145.   if (retcode == SQL_ERROR || retcode == SQL_SUCCESS_WITH_INFO) 
  146.     SQLFetchTest_DisplayError(SQL_HANDLE_STMT, F_hstmt);
  147.   //******************************************************************
  148.   //** There is no executed statement associated with the allocated **
  149.   //** SQL-statement identified by StatementHandle                  **
  150.   //******************************************************************
  151.   //This function is correct after testing. We don't test again.
  152.   /*
  153.     retcode = SQLFetch(F_hstmt);
  154.     ndbout << endl << "retcode = SQLFetch(F_hstmt) = " << retcode << endl;
  155.     if  (retcode == SQL_ERROR || retcode == SQL_SUCCESS_WITH_INFO)
  156.     {
  157.     ndbout << "There is no executed statement associated with" << endl;
  158.     ndbout << "the allocated SQL-statement" << endl;
  159.     SQLFetchTest_DisplayError(SQL_HANDLE_DESC, F_hstmt);
  160.     
  161.     }
  162.   */
  163.   //*******************************
  164.   //** Execute  CREATE statement **
  165.   //*******************************
  166.   retcode = SQLExecute(F_hstmt);
  167.   if (retcode == 0)
  168.     ndbout << endl << "Execute CREATE TABLE Statement OK!" << endl;
  169.   if (retcode == SQL_ERROR || retcode == SQL_SUCCESS_WITH_INFO) 
  170.     SQLFetchTest_DisplayError(SQL_HANDLE_STMT, F_hstmt);
  171.   //********************************
  172.   //** Prepare  INSERT statements **
  173.   //********************************
  174.   ndbout << ">>>>" << (char*)SQLStmt2 << "<<<<" << endl;
  175.   retcode = SQLPrepare(F_hstmt, 
  176.        SQLStmt2, 
  177.        SQL_NTS);
  178.   if (retcode == SQL_ERROR || retcode == SQL_SUCCESS_WITH_INFO) 
  179.     SQLFetchTest_DisplayError(SQL_HANDLE_STMT, F_hstmt);
  180.   
  181.   //******************************
  182.   //** Execute INSERT statement **
  183.   //******************************
  184.   retcode = SQLExecute(F_hstmt);
  185.   if (retcode == 0)
  186.   ndbout << endl <<"Execute INSERT Statement OK!" << endl;
  187.   if (retcode == SQL_ERROR || retcode == SQL_SUCCESS_WITH_INFO) 
  188.     SQLFetchTest_DisplayError(SQL_HANDLE_STMT, F_hstmt);
  189.   //********************************
  190.   //** Prepare  UPDATE statements **
  191.   //********************************
  192.   ndbout << ">>>>" << (char*)SQLStmt3 << "<<<<" << endl;
  193.   retcode = SQLPrepare(F_hstmt, 
  194.        SQLStmt3, 
  195.        SQL_NTS);
  196.   if (retcode == SQL_ERROR || retcode == SQL_SUCCESS_WITH_INFO) 
  197.     SQLFetchTest_DisplayError(SQL_HANDLE_STMT, F_hstmt);
  198.   //******************************
  199.   //** Execute UPDATE statement **
  200.   //******************************
  201.   retcode = SQLExecute(F_hstmt);
  202.   if (retcode == 0)
  203.   ndbout << endl <<"Execute UPDATE Statement OK!" << endl;
  204.   if (retcode == SQL_ERROR || retcode == SQL_SUCCESS_WITH_INFO) 
  205.     SQLFetchTest_DisplayError(SQL_HANDLE_STMT, F_hstmt);
  206.   //********************************
  207.   //** Prepare  DELETE statements **
  208.   //********************************
  209.   ndbout << ">>>>" << (char*)SQLStmt4 << "<<<<" << endl;
  210.   retcode = SQLPrepare(F_hstmt, 
  211.        SQLStmt4, 
  212.        SQL_NTS);
  213.   if (retcode == SQL_ERROR || retcode == SQL_SUCCESS_WITH_INFO) 
  214.     {
  215.       ndbout << endl << "Preparing DELETE Statement failure!" << endl;
  216.       SQLFetchTest_DisplayError(SQL_HANDLE_STMT, F_hstmt);
  217.     }
  218.   //******************************
  219.   //** Execute DELETE statement **
  220.   //******************************
  221.   retcode = SQLExecute(F_hstmt);
  222.   if (retcode == 0)
  223.   ndbout << endl <<"Execute DELETE Statement OK!" << endl;
  224.   if (retcode == SQL_ERROR || retcode == SQL_SUCCESS_WITH_INFO) 
  225.     {
  226.       ndbout << "DELETE Statement executing failure!" << endl;
  227.       SQLFetchTest_DisplayError(SQL_HANDLE_STMT, F_hstmt);
  228.     }
  229.   //********************************
  230.   //** Prepare  SELECT statements **
  231.   //********************************
  232.   ndbout << ">>>>" << (char*)SQLStmt << "<<<<" << endl;
  233.   retcode = SQLPrepare(F_hstmt, 
  234.        SQLStmt, 
  235.        SQL_NTS);
  236.   if (retcode == SQL_ERROR || retcode == SQL_SUCCESS_WITH_INFO) 
  237.     SQLFetchTest_DisplayError(SQL_HANDLE_STMT, F_hstmt);
  238.   /*
  239.   //******************************
  240.   //** Execute SELECT statement **
  241.   //******************************
  242.   retcode = SQLExecute(F_hstmt);
  243.   if (retcode == 0)
  244.   ndbout << endl <<"Execute SELECT Statement OK!" << endl;
  245.   if (retcode == SQL_ERROR || retcode == SQL_SUCCESS_WITH_INFO) 
  246.     SQLFetchTest_DisplayError(SQL_HANDLE_STMT, F_hstmt);
  247.   */
  248.   //********************
  249.   //** Bind columns   **
  250.   //********************
  251.   retcode =SQLBindCol(F_hstmt, 
  252.       1, 
  253.       SQL_C_CHAR, 
  254.       F_CustID, 
  255.       sizeof(F_CustID),
  256.       NULL);
  257.   ndbout << endl << "Bind Col1 retcode = " << retcode << " OK!" << endl;
  258.   retcode =SQLBindCol(F_hstmt, 
  259.       2, 
  260.       SQL_C_CHAR, 
  261.       F_Name, 
  262.       F_NAME_LEN,
  263.       NULL);
  264.   ndbout << "Bind Col2 retcode = " << retcode << " OK!" << endl;
  265.   retcode = SQLBindCol(F_hstmt, 
  266.        3,
  267.        SQL_C_CHAR, 
  268.        F_Address, 
  269.        F_ADDRESS_LEN, 
  270.        NULL);
  271.   ndbout << "Bind Col3 retcode = " << retcode << " OK!" << endl;
  272.   retcode = SQLBindCol(F_hstmt, 
  273.        4, 
  274.        SQL_C_CHAR, 
  275.        F_Phone, 
  276.        F_PHONE_LEN,
  277.        NULL);
  278.   ndbout << "Bind Col4 retcode = " << retcode << " OK!" << endl;
  279.   //******************************
  280.   //** Execute SELECT statement **
  281.   //******************************
  282.   retcode = SQLExecute(F_hstmt);
  283.   if (retcode == 0)
  284.   ndbout << endl <<"Execute SELECT Statement OK!" << endl;
  285.   if (retcode == SQL_ERROR || retcode == SQL_SUCCESS_WITH_INFO) 
  286.     SQLFetchTest_DisplayError(SQL_HANDLE_STMT, F_hstmt);
  287.   //***************
  288.   //* Fetch data **
  289.   //***************
  290.   ndbout << endl <<"Executing Fetch SELECT Statement ......" << endl;
  291.   retcode = SQLFetch(F_hstmt);
  292.   if (retcode == 100)
  293.     ndbout << endl <<"Execute Fetch SELECT Statement, But No DATA!" << endl;
  294.   if (retcode == 0)
  295.   ndbout << endl <<"Execute Fetch SELECT Statement OK!" << endl;
  296.   if (retcode == SQL_ERROR || retcode == SQL_SUCCESS_WITH_INFO) 
  297.     SQLFetchTest_DisplayError(SQL_HANDLE_STMT, F_hstmt);
  298.   //*******************
  299.   //* Display result **
  300.   //*******************
  301.   ndbout << endl << "The results is : " << endl;
  302.   ndbout << "CustID = " << (char *)F_CustID << endl;
  303.   ndbout << "Name = " << (char *)F_Name << endl;
  304.   ndbout << "Address = " << (char *)F_Address << endl;
  305.   ndbout << "Phone = " << (char *)F_Phone << endl;
  306.   // *********************************
  307.   // ** Disconnect and Free Handles **
  308.   // *********************************  
  309.   SQLDisconnect(F_hdbc);
  310.   SQLFreeHandle(SQL_HANDLE_STMT, F_hstmt);
  311.   SQLFreeHandle(SQL_HANDLE_DBC, F_hdbc);
  312.   SQLFreeHandle(SQL_HANDLE_ENV, F_henv);
  313.   return NDBT_OK;
  314.  }
  315. void SQLFetchTest_DisplayError(SQLSMALLINT F_HandleType, 
  316.        SQLHSTMT F_InputHandle)
  317. {
  318.   SQLCHAR     Sqlstate[50], Msg[F_MESSAGE_LENGTH];
  319.   SQLRETURN   SQLSTATEs;
  320.   SQLINTEGER  NativeError;
  321.   SQLSMALLINT i, MsgLen;
  322.   Msg[0] = 0;
  323.   i = 1;
  324.   
  325.   ndbout << "-------------------------------------------------" << endl;
  326.   ndbout << "Error diagnostics:" << endl;
  327.   
  328.   while ((SQLSTATEs = SQLGetDiagRec(F_HandleType, 
  329.     F_InputHandle, 
  330.     i, 
  331.     Sqlstate, 
  332.     &NativeError, 
  333.     Msg, 
  334.     sizeof(Msg), 
  335.     &MsgLen)) 
  336.  != SQL_NO_DATA)                   
  337. {
  338.     
  339.   ndbout << "the HandleType is:" << F_HandleType << endl;
  340.   ndbout << "the InputHandle is :" <<(long)F_InputHandle << endl;
  341.   ndbout << "the Msg is :" << (char *)Msg << endl;
  342.   ndbout << "the output state is:" << (char *)Sqlstate << endl; 
  343.   
  344.   i ++;
  345.   break;
  346. }
  347.   ndbout << "-------------------------------------------------" << endl;  
  348. }