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

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 SQLBindColTest.cpp
  15.  */
  16. #include <common.hpp>
  17. using namespace std;
  18. #define BindCol_NAME_LEN 10
  19. #define BindCol_PHONE_LEN 10
  20. #define BindCol_ADDRESS_LEN 10
  21. #define BindCol_Price_LEN 10
  22. #define BindCol_Weight_LEN 10
  23. #define BindCol_Tax_LEN 10
  24. #define BindCol_SQL_MAXIMUM_MESSAGE_LENGTH 200
  25. //SQLHDBC     BindCol_hdbc;
  26. //SQLHSTMT    BindCol_hstmt;
  27. //SQLHENV     BindCol_henv;
  28. //SQLHDESC    BindCol_hdesc;
  29. //SQLRETURN   BCret;
  30. //SQLCHAR     BindCol_Name[BindCol_NAME_LEN], BindCol_Phone[BindCol_PHONE_LEN];
  31. //SQLCHAR     BindCol_Address[BindCol_ADDRESS_LEN];
  32. //SQLINTEGER  NativeError;
  33. //unsigned long BindCol_CustID;
  34. void BindCol_DisplayError(SQLSMALLINT BindCol_HandleType, 
  35.   SQLHSTMT BindCol_InputHandle);
  36. /** 
  37.  * Test  setting column to bind
  38.  * for a column in a result 
  39.  *
  40.  * -# Bind columns  1 
  41.  * -# Bind columns  2 
  42.  * -# Bind columns  3 
  43.  * -# Bind columns  4 
  44.  * -# Bind columns  5 
  45.  * -# Bind columns  6 
  46.  * -# Bind columns  7
  47.  * @return Zero, if test succeeded
  48.  */
  49. int SQLBindColTest()
  50. {
  51.   SQLHDBC     BindCol_hdbc;
  52.   SQLHSTMT    BindCol_hstmt;
  53.   SQLHENV     BindCol_henv;
  54.   SQLHDESC    BindCol_hdesc;
  55.   
  56.   SQLCHAR SQLStmt1 [240];
  57.   SQLCHAR SQLStmt2 [240];
  58.   SQLCHAR SQLStmt3 [120];
  59.   SQLRETURN   BCret;
  60.   unsigned long  BindCol_CustID;
  61.   SQLCHAR        BindCol_Name[BindCol_NAME_LEN];
  62.   short          BindCol_Account;
  63.   unsigned short BindCol_Phone;
  64.   long           BindCol_Price;
  65.   float          BindCol_Weight;
  66.   double         BindCol_Tax;
  67.   ndbout << endl << "Start SQLBindCol Testing" << endl;
  68.   //*******************************************************************
  69.   //** hstmt 
  70.   //** Execute a statement to retrieve rows from the Customers table **
  71.   //** We can create the table and insert rows into Customers        **
  72.   //*******************************************************************
  73.   //************************************
  74.   //** Allocate An Environment Handle **
  75.   //************************************
  76.   BCret = SQLAllocHandle(SQL_HANDLE_ENV, 
  77.  SQL_NULL_HANDLE, 
  78.  &BindCol_henv);
  79.   
  80. if (BCret == SQL_SUCCESS || BCret == SQL_SUCCESS_WITH_INFO)
  81.     ndbout << "Allocated an environment Handle!" << endl;
  82.   
  83.   //*********************************************
  84.   //** Set the ODBC application Version to 3.x **
  85.   //*********************************************
  86.  BCret = SQLSetEnvAttr(BindCol_henv, 
  87.        SQL_ATTR_ODBC_VERSION, 
  88.        (SQLPOINTER) SQL_OV_ODBC3, 
  89.        SQL_IS_UINTEGER);
  90.   
  91. if (BCret == SQL_SUCCESS || BCret == SQL_SUCCESS_WITH_INFO)
  92.   ndbout << "Set the ODBC application Version to 3.x!" << endl;
  93. //**********************************
  94. //** Allocate A Connection Handle **
  95. //**********************************
  96.  
  97.  BCret = SQLAllocHandle(SQL_HANDLE_DBC, 
  98. BindCol_henv, 
  99. &BindCol_hdbc);
  100.  
  101.  if (BCret == SQL_SUCCESS || BCret == SQL_SUCCESS_WITH_INFO)
  102.    ndbout << "Allocated a connection Handle!" << endl;
  103.  
  104.  // *******************
  105.  // ** Connect to DB **
  106.  // *******************
  107.  BCret = SQLConnect(BindCol_hdbc, 
  108.     (SQLCHAR *) connectString(), 
  109.     SQL_NTS, 
  110.     (SQLCHAR *) "", 
  111.     SQL_NTS, 
  112.     (SQLCHAR *) "", 
  113.     SQL_NTS);
  114.  
  115.  if (BCret == SQL_SUCCESS || BCret == SQL_SUCCESS_WITH_INFO)
  116.    ndbout << "Connected to DB : OK!" << endl;
  117.  else 
  118.    {  
  119.      ndbout << "Failure to Connect DB!" << endl;
  120.      return NDBT_FAILED;
  121.    }
  122.  //*******************************
  123.  //** Allocate statement handle **
  124.  //*******************************
  125.  
  126.  BCret = SQLAllocHandle(SQL_HANDLE_STMT, 
  127. BindCol_hdbc, 
  128. &BindCol_hstmt); 
  129.  if(BCret == SQL_SUCCESS || BCret == SQL_SUCCESS_WITH_INFO) 
  130.    ndbout << "Allocated a statement handle!" << endl;
  131.  
  132.  //************************
  133.  //** Define a statement **
  134.  //************************
  135.  /* Primary key is Integer and Char */
  136.  strcpy((char *) SQLStmt1, "CREATE TABLE Customer1(CustID Integer, Name Char(12), Account Char(12), Phone Char(12), Price Char(6), Weight Char(6), Tax Char(6), Primary Key(CustID, Name))");
  137.  strcpy((char *) SQLStmt2, "INSERT INTO Customer1 (CustID, Name, Account, Phone, Price, Weight, Tax) VALUES(588, 'peter','6808','7190890', '5.68', '1.58', '0.88')");
  138.    
  139.  strcpy((char *) SQLStmt3, "SELECT * FROM Customer1");
  140.    
  141.  //************************************************
  142.  //** Prepare and Execute CREATE TABLE statement **
  143.  //************************************************
  144.  ndbout << endl << "Prepare and Execute CREATE TABLE statement ......" << endl;
  145.  ndbout << ">>>>" << (char*)SQLStmt1 << "<<<<" << endl;
  146.  BCret = SQLExecDirect(BindCol_hstmt, 
  147.        SQLStmt1, 
  148.        SQL_NTS);
  149.  if (BCret == SQL_SUCCESS)
  150.    ndbout << "Prepare and Execute CREATE TABLE statement OK!" 
  151.   << endl<< endl;
  152.   
  153.  if (BCret == SQL_ERROR || BCret == SQL_SUCCESS_WITH_INFO) 
  154.    BindCol_DisplayError(SQL_HANDLE_STMT, BindCol_hstmt);
  155.  
  156.  if (BCret == -2)
  157.    {
  158.      ndbout << "BCret = SQLExexDirect()=" << BCret << endl;
  159.      BindCol_DisplayError(SQL_HANDLE_STMT, BindCol_hstmt);
  160.    }
  161.  //*******************************************************
  162.  //** Prepare and Execute INSERT statement with prepare **
  163.  //*******************************************************
  164.  ndbout << "Prepare and Execute INSERT statement ......" << endl;
  165.  ndbout << ">>>>" << (char*)SQLStmt2 << "<<<<" << endl;
  166.  BCret = SQLExecDirect(BindCol_hstmt, 
  167.        SQLStmt2, 
  168.        SQL_NTS);
  169.  
  170.  if (BCret == SQL_SUCCESS)
  171.    ndbout << "Prepare and Execute INSERT statement OK!" 
  172.   << endl << endl;
  173.  
  174.  if (BCret == SQL_ERROR || BCret == SQL_SUCCESS_WITH_INFO) 
  175.    BindCol_DisplayError(SQL_HANDLE_STMT, BindCol_hstmt);
  176.   
  177.  if (BCret == -2)
  178.    {
  179.      ndbout << "BCret = SQLExexDirect()=" << BCret << endl;
  180.      BindCol_DisplayError(SQL_HANDLE_STMT, BindCol_hstmt);
  181.    }
  182.  //******************************************
  183.  //** Prepare and EXECUTE SELECT statement **
  184.  //******************************************
  185.  ndbout << "Prepare and Execute SELECT statement ......" << endl;
  186.  ndbout << ">>>>" << (char*)SQLStmt3 << "<<<<" << endl;
  187.  BCret = SQLExecDirect(BindCol_hstmt, 
  188.        SQLStmt3, 
  189.        SQL_NTS);
  190.  
  191.  if (BCret == SQL_SUCCESS)
  192.    ndbout << "Prepare and Execute SELECT statement OK!" 
  193.   << endl << endl;
  194.  
  195.  if (BCret == SQL_ERROR || BCret == SQL_SUCCESS_WITH_INFO) 
  196.    BindCol_DisplayError(SQL_HANDLE_STMT, BindCol_hstmt);
  197.   
  198.  if (BCret == -2)
  199.    {
  200.      ndbout << "BCret = SQLExexDirect()=" << BCret << endl;
  201.      BindCol_DisplayError(SQL_HANDLE_STMT, BindCol_hstmt);
  202.    }
  203.  
  204.  //*******************************
  205.  //** Execute SELECT statement  **
  206.  //******************************* 
  207.  // BCret = SQLExecute(BindCol_hstmt);
  208.  // if (BCret == SQL_ERROR || BCret == SQL_SUCCESS_WITH_INFO) 
  209.  //   {  
  210.  //     ndbout << "BCret = " << BCret << endl;
  211.  //     BindCol_DisplayError(SQL_HANDLE_STMT, BindCol_hstmt);
  212.  //   }
  213.  // else
  214.  //   {
  215.      
  216.  if (BCret == SQL_SUCCESS)
  217.    ndbout << "Execute INSERT statement OK!" << endl;
  218.      
  219.  //*********************
  220.  //** Test1           **
  221.  //** Bind columns  1 **
  222.  //*********************
  223.  
  224.  BCret =SQLBindCol(BindCol_hstmt, 
  225.    1, 
  226.    SQL_C_ULONG, 
  227.    &BindCol_CustID, 
  228.    sizeof(BindCol_CustID),
  229.    NULL);
  230.  
  231.  if (BCret == SQL_SUCCESS)
  232.    {
  233.      ndbout << endl << "Bind col 1 OK!" << endl;
  234.    }
  235.  else if (BCret == SQL_SUCCESS_WITH_INFO)
  236.    {
  237.      ndbout << "Bind Col 1 OK but with INFO" << endl;
  238.      BindCol_DisplayError(SQL_HANDLE_STMT, BindCol_hstmt);
  239.    }    
  240.  else if (BCret == SQL_ERROR)
  241.    { 
  242.      ndbout << "Bind Col 1 Failed!" << endl;
  243.      BindCol_DisplayError(SQL_HANDLE_STMT, BindCol_hstmt);
  244.      return NDBT_FAILED;
  245.    }
  246.  else
  247.    ndbout << endl;   
  248.  
  249.  //*********************
  250.  //** Test2           **
  251.  //** Bind columns  2 **
  252.  //*********************
  253.  
  254.  BCret =SQLBindCol(BindCol_hstmt, 
  255.    2, 
  256.    SQL_C_CHAR, 
  257.    &BindCol_Name, 
  258.    BindCol_NAME_LEN,
  259.    NULL);
  260.  
  261.  if (BCret == SQL_SUCCESS)
  262.    {
  263.      ndbout << "Bind col 2 OK!" << endl;
  264.    }
  265.  else if (BCret == SQL_SUCCESS_WITH_INFO)
  266.    {
  267.      ndbout << "Bind Col 2 OK but with INFO" << endl;
  268.      BindCol_DisplayError(SQL_HANDLE_STMT, BindCol_hstmt);
  269.    }    
  270.  else if (BCret == SQL_ERROR)
  271.    { 
  272.      ndbout << "Bind Col 2 Failed!" << endl;
  273.      BindCol_DisplayError(SQL_HANDLE_STMT, BindCol_hstmt);
  274.      return NDBT_FAILED;
  275.    }
  276.  else
  277.    ndbout << endl;
  278.  
  279.  //*********************
  280.  //** Test3           **
  281.  //** Bind columns 3  **
  282.  //*********************
  283.  
  284.  BCret = SQLBindCol(BindCol_hstmt, 
  285.     3,
  286.     SQL_C_USHORT, 
  287.     &BindCol_Account, 
  288.     sizeof(BindCol_Account),
  289.     NULL);
  290.  
  291.  if (BCret == SQL_ERROR)
  292.    { 
  293.      ndbout << "Bind Col 3 Failed!" << endl;
  294.      BindCol_DisplayError(SQL_HANDLE_STMT, BindCol_hstmt);
  295.      return NDBT_FAILED;
  296.    }
  297.  else if (BCret == SQL_SUCCESS_WITH_INFO)
  298.    {
  299.      ndbout << "Bind Col 3 OK but with INFO" << endl;
  300.      BindCol_DisplayError(SQL_HANDLE_STMT, BindCol_hstmt);
  301.    }
  302.  else if (BCret == SQL_SUCCESS)
  303.    {
  304.      ndbout << "Bind col 3 OK!" << endl;
  305.    }
  306.  else
  307.    ndbout << endl;
  308.  
  309.  //*********************
  310.  //** Test4           **
  311.  //** Bind columns 4  **
  312.  //*********************
  313.  
  314.  BCret = SQLBindCol(BindCol_hstmt, 
  315.     4, 
  316.     SQL_C_USHORT, 
  317.     &BindCol_Phone, 
  318.     sizeof(BindCol_Phone),
  319.     NULL);
  320.  
  321.  if (BCret == SQL_ERROR)
  322.    {
  323.      ndbout << "Bind Col 4 Failed!" << endl; 
  324.      BindCol_DisplayError(SQL_HANDLE_STMT, BindCol_hstmt);
  325.      return NDBT_FAILED;
  326.    }
  327.  else if (BCret == SQL_SUCCESS_WITH_INFO)
  328.    {
  329.      ndbout << "Bind Col 4 OK but with INFO" << endl;
  330.      BindCol_DisplayError(SQL_HANDLE_STMT, BindCol_hstmt);
  331.    }
  332.  else if (BCret == SQL_SUCCESS) 
  333.    {
  334.      ndbout << "Bind col 4 OK!" << endl;
  335.    }
  336.  else 
  337.    ndbout << endl;
  338.  //*********************
  339.  //** Test5           **
  340.  //** Bind columns 5  **
  341.  //*********************
  342.  
  343.  BCret = SQLBindCol(BindCol_hstmt, 
  344.     5, 
  345.     SQL_C_SLONG, 
  346.     &BindCol_Price, 
  347.     sizeof(BindCol_Price),
  348.     NULL);
  349.  
  350.  if (BCret == SQL_ERROR)
  351.    {
  352.      ndbout << "Bind Col 5 Failed!" << endl; 
  353.      BindCol_DisplayError(SQL_HANDLE_STMT, BindCol_hstmt);
  354.      return NDBT_FAILED;
  355.    }
  356.  else if (BCret == SQL_SUCCESS_WITH_INFO)
  357.    {
  358.      ndbout << "Bind Col 5 OK but with INFO" << endl;
  359.      BindCol_DisplayError(SQL_HANDLE_STMT, BindCol_hstmt);
  360.    }
  361.  else if (BCret == SQL_SUCCESS) 
  362.    {
  363.      ndbout << "Bind col 5 OK!" << endl;
  364.    }
  365.  else 
  366.    ndbout << endl;
  367.  //*********************
  368.  //** Test6           **
  369.  //** Bind columns 6  **
  370.  //*********************
  371.  
  372.  BCret = SQLBindCol(BindCol_hstmt, 
  373.     6,
  374.     SQL_C_FLOAT, 
  375.     &BindCol_Weight, 
  376.     sizeof(BindCol_Weight),
  377.     NULL);
  378.  
  379.  if (BCret == SQL_ERROR)
  380.    {
  381.      ndbout << "Bind Col 6 Failed!" << endl; 
  382.      BindCol_DisplayError(SQL_HANDLE_STMT, BindCol_hstmt);
  383.      return NDBT_FAILED;
  384.    }
  385.  else if (BCret == SQL_SUCCESS_WITH_INFO)
  386.    {
  387.      ndbout << "Bind Col 6 OK but with INFO" << endl;
  388.      BindCol_DisplayError(SQL_HANDLE_STMT, BindCol_hstmt);
  389.    }
  390.  else if (BCret == SQL_SUCCESS) 
  391.    {
  392.      ndbout << "Bind col 6 OK!" << endl;
  393.    }
  394.  else 
  395.    ndbout << endl;
  396.  //*********************
  397.  //** Test7           **
  398.  //** Bind columns 7  **
  399.  //*********************
  400.  BCret = SQLBindCol(BindCol_hstmt, 
  401.     7,
  402.     SQL_C_DOUBLE,
  403.     &BindCol_Tax, 
  404.     sizeof(BindCol_Tax),
  405.     NULL);
  406.  
  407.  if (BCret == SQL_ERROR)
  408.    {
  409.      ndbout << "Bind Col 7 Failed!" << endl; 
  410.      BindCol_DisplayError(SQL_HANDLE_STMT, BindCol_hstmt);
  411.      return NDBT_FAILED;
  412.    }
  413.  else if (BCret == SQL_SUCCESS_WITH_INFO)
  414.    {
  415.      ndbout << "Bind Col 7 OK but with INFO" << endl;
  416.      BindCol_DisplayError(SQL_HANDLE_STMT, BindCol_hstmt);
  417.    }
  418.  else if (BCret == SQL_SUCCESS) 
  419.    {
  420.      ndbout << "Bind col 7 OK!" << endl;
  421.    }
  422.  else 
  423.    ndbout << endl;
  424.  //}
  425. //*****************************************
  426. //* Fetch and print each row of data. On **
  427. //* an error, display a message and exit **
  428. //*****************************************
  429. BCret = SQLFetch(BindCol_hstmt);
  430.  ndbout << endl << "BCret = SQLFetch(BindCol_hstmt) = " 
  431. << BCret << endl;
  432. if (BCret == SQL_ERROR)
  433.   BindCol_DisplayError(SQL_HANDLE_STMT, BindCol_hstmt);
  434.   return NDBT_FAILED;
  435. }
  436. else if (BCret == SQL_SUCCESS_WITH_INFO) 
  437. {
  438.   ndbout << "CustID = " << (int)BindCol_CustID << endl;
  439.   ndbout << "Name = " << (char *)BindCol_Name << endl;
  440.   ndbout << "Account = " << (int)BindCol_Account << endl;
  441.   ndbout << "Phone = " << (int)BindCol_Phone << endl; 
  442.   ndbout << "Price = " << (int)BindCol_Price << endl;
  443.   ndbout << "Weight = " << (int)BindCol_Weight << endl;
  444.   ndbout << "Tax = " << (int)BindCol_Tax << endl;
  445.   BindCol_DisplayError(SQL_HANDLE_STMT, BindCol_hstmt);
  446. }
  447. else
  448. {
  449.   ndbout << "CustID = " << (int)BindCol_CustID << endl;
  450.   ndbout << "Name = " << (char *)BindCol_Name << endl;
  451.   ndbout << "Account = " << (int)BindCol_Account << endl;
  452.   ndbout << "Phone = " << (int)BindCol_Phone << endl;     
  453.   ndbout << "Price = " << (int)BindCol_Price << endl;
  454.   ndbout << "Weight = " << (int)BindCol_Weight << endl;
  455.   ndbout << "Tax = " << (int)BindCol_Tax << endl;
  456. }
  457. // *********************************
  458. // ** Disconnect and Free Handles **
  459. // *********************************  
  460. SQLDisconnect(BindCol_hdbc);
  461. SQLFreeHandle(SQL_HANDLE_STMT, BindCol_hstmt);
  462. SQLFreeHandle(SQL_HANDLE_DBC, BindCol_hdbc);
  463. SQLFreeHandle(SQL_HANDLE_ENV, BindCol_henv);
  464. return NDBT_OK;
  465. }
  466. void BindCol_DisplayError(SQLSMALLINT BindCol_HandleType, 
  467.   SQLHSTMT BindCol_InputHandle)
  468. {
  469.   SQLSMALLINT BindCol_i = 1;
  470.   SQLRETURN BindCol__SQLSTATEs;
  471.   SQLCHAR BindCol_Sqlstate[5];
  472.   SQLCHAR BindCol_Msg[BindCol_SQL_MAXIMUM_MESSAGE_LENGTH];
  473.   SQLSMALLINT BindCol_MsgLen;
  474.   SQLINTEGER  NativeError;
  475.   
  476.   ndbout << "-------------------------------------------------" << endl;
  477.   ndbout << "Error diagnostics:" << endl;
  478.   
  479.   while ((BindCol__SQLSTATEs = SQLGetDiagRec(BindCol_HandleType, 
  480.      BindCol_InputHandle, 
  481.      BindCol_i, 
  482.      BindCol_Sqlstate, 
  483.      &NativeError, 
  484.      BindCol_Msg, 
  485.      sizeof(BindCol_Msg), 
  486.      &BindCol_MsgLen)
  487.   ) != SQL_NO_DATA)
  488.     {
  489.       
  490.       ndbout << "the HandleType is:" << BindCol_HandleType << endl;
  491.       ndbout << "the InputHandle is :" << (long)BindCol_InputHandle << endl;
  492.       ndbout << "the BindCol_Msg is: " << (char *) BindCol_Msg << endl;
  493.       ndbout << "the output state is:" << (char *)BindCol_Sqlstate << endl; 
  494.       
  495.       BindCol_i ++;
  496.       break;
  497.     }
  498.   ndbout << "-------------------------------------------------" << endl;
  499. }