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

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. #include <NdbOut.hpp>
  14. #include <sqlcli.h>
  15. #include <stdio.h>
  16. using namespace std;
  17. SQLHDBC     hdbc;
  18. SQLHSTMT    hstmt;
  19. SQLHENV     henv;
  20. SQLHDESC    hdesc;
  21. SQLINTEGER  strangehandle;
  22. SQLRETURN   retcode, SQLSTATEs;
  23. SQLCHAR Sqlstate[5];
  24. SQLINTEGER    NativeError;
  25. SQLSMALLINT   i, MsgLen;
  26. SQLCHAR   Msg[SQL_MAXIMUM_MESSAGE_LENGTH];
  27.        
  28. void DisplayError(SQLSMALLINT HandleType, SQLHSTMT InputHandle);
  29. int SQLGetDiagRecTest()
  30. {
  31.   strangehandle = 67;
  32.   /* hstmt */
  33.   // Execute a statement to retrieve rows from the Customers table. We can create the table and inside rows in 
  34.   // NDB by another program TestDirectSQL 
  35.   //  const SQLCHAR *StatementText = "SELECT CustID, Name, Address, Phone FROM Customers";
  36.   //  retcode = SQLPrepare(hstmt, (SQLCHAR*)"SELECT CustID, Name, Address, Phone FROM Customers", 56);
  37.   retcode = SQLPrepare(hstmt, (SQLCHAR*)"SELECT CustID, Name, Address, Phone FROM Customers", SQL_NTS);
  38.   if (retcode == SQL_SUCCESS_WITH_INFO || retcode == SQL_SUCCESS) {
  39.      i = 1;
  40.      while ((SQLSTATEs = SQLGetDiagRec(67, 67, i, 
  41.              Sqlstate, &NativeError, Msg, sizeof(Msg), 
  42.              &MsgLen)) != SQL_NO_DATA)                   {
  43.      if (SQLSTATEs == SQL_ERROR || retcode == SQL_SUCCESS_WITH_INFO) {
  44.      ndbout << "the HandleType is:" << HandleType << endl;
  45.      ndbout << "the InputHandle is :" << InputHandle << endl;
  46.      ndbout << "the output state is:" << (char *)Sqlstate << endl; 
  47.                                                                      }
  48.      i ++;
  49.                                                          }
  50.                                                                   }
  51.  
  52.   /* HandleType indicates ENVIRNMENT HANDLE and Handle does not identify an allocated SQL_environment */
  53.   retcode = SQLPrepare(hstmt, (SQLCHAR*)"SELECT CustID, Name, Address, Phone FROM Customers", SQL_NTS);
  54.   if (retcode == SQL_SUCCESS_WITH_INFO || retcode == SQL_SUCCESS) {
  55.      i = 1;
  56.      while ((SQLSTATEs = SQLGetDiagRec(SQL_HANDLE_ENV, hdbc, i, 
  57.              Sqlstate, &NativeError, Msg, sizeof(Msg), 
  58.              &MsgLen)) != SQL_NO_DATA)                   {
  59.      if (SQLSTATEs == SQL_ERROR || retcode == SQL_SUCCESS_WITH_INFO) {
  60.      ndbout << "the HandleType is:" << HandleType << endl;
  61.      ndbout << "the InputHandle is :" << InputHandle << endl;
  62.      ndbout << "the output state is:" << (char *)Sqlstate << endl; 
  63.                                                                      }
  64.      i ++;
  65.                                                          }
  66.                                                                   }
  67.   /*  HandleType indicates CONNECTION HANDLE and Handle does not identify an allocated SQL_connection */
  68.   retcode = SQLPrepare(hstmt, (SQLCHAR*)"SELECT CustID, Name, Address, Phone FROM Customers", SQL_NTS);
  69.   if (retcode == SQL_SUCCESS_WITH_INFO || retcode == SQL_SUCCESS) {
  70.      i = 1;
  71.      while ((SQLSTATEs = SQLGetDiagRec(SQL_HANDLE_DBC, henv, i, 
  72.              Sqlstate, &NativeError, Msg, sizeof(Msg), 
  73.              &MsgLen)) != SQL_NO_DATA)                   {
  74.      if (SQLSTATEs == SQL_ERROR || retcode == SQL_SUCCESS_WITH_INFO) {
  75.      ndbout << "the HandleType is:" << HandleType << endl;
  76.      ndbout << "the InputHandle is :" << InputHandle << endl;
  77.      ndbout << "the output state is:" << (char *)Sqlstate << endl; 
  78.                                                                      }
  79.      i ++;
  80.                                                          }
  81.                                                                   }
  82.   /*  HandleType indicates STATEMENT HANDLE and Handle does not identify an allocated SQL_statement */
  83.   retcode = SQLPrepare(hstmt, (SQLCHAR*)"SELECT CustID, Name, Address, Phone FROM Customers", SQL_NTS);
  84.   if (retcode == SQL_SUCCESS_WITH_INFO || retcode == SQL_SUCCESS) {
  85.      i = 1;
  86.      while ((SQLSTATEs = SQLGetDiagRec(SQL_HANDLE_STMT, hdbc, i, 
  87.              Sqlstate, &NativeError, Msg, sizeof(Msg), 
  88.              &MsgLen)) != SQL_NO_DATA)                   {
  89.      if (SQLSTATEs == SQL_ERROR || retcode == SQL_SUCCESS_WITH_INFO) {
  90.      ndbout << "the HandleType is:" << HandleType << endl;
  91.      ndbout << "the InputHandle is :" << InputHandle << endl;
  92.      ndbout << "the output state is:" << (char *)Sqlstate << endl; 
  93.                                                                      }
  94.      i ++;
  95.                                                          }
  96.                                                                   }
  97.   /*  HandleType indicates DESCRIPTOR HANDLE and Handle does not identify an allocated SQL_descriptor */
  98.   retcode = SQLPrepare(hstmt, (SQLCHAR*)"SELECT CustID, Name, Address, Phone FROM Customers", SQL_NTS);
  99.   if (retcode == SQL_SUCCESS_WITH_INFO || retcode == SQL_SUCCESS) {
  100.      i = 1;
  101.      while ((SQLSTATEs = SQLGetDiagRec(SQL_HANDLE_DESC, hdbc, i, 
  102.              Sqlstate, &NativeError, Msg, sizeof(Msg), 
  103.              &MsgLen)) != SQL_NO_DATA)                   {
  104.      if (SQLSTATEs == SQL_ERROR || retcode == SQL_SUCCESS_WITH_INFO) {
  105.      ndbout << "the HandleType is:" << HandleType << endl;
  106.      ndbout << "the InputHandle is :" << InputHandle << endl;
  107.      ndbout << "the output state is:" << (char *)Sqlstate << endl; 
  108.                                                                      }
  109.      i ++;
  110.                                                          }
  111.                                                                   }
  112.   /*  RecordNumber is less than one */
  113.   retcode = SQLPrepare(hstmt, (SQLCHAR*)"SELECT CustID, Name, Address, Phone FROM Customers", SQL_NTS);
  114.   if (retcode == SQL_SUCCESS_WITH_INFO || retcode == SQL_SUCCESS) {
  115.      
  116.      where ((SQLSTATEs = SQLGetDiagRec(SQL_HANDLE_DBC, hdbc, -1, 
  117.              Sqlstate, &NativeError, Msg, sizeof(Msg), 
  118.              &MsgLen)) != SQL_NO_DATA)                   {
  119.      if (SQLSTATEs == SQL_ERROR || retcode == SQL_SUCCESS_WITH_INFO) {
  120.      ndbout << "the HandleType is:" << HandleType << endl;
  121.      ndbout << "the InputHandle is :" << InputHandle << endl;
  122.      ndbout << "the output state is:" << (char *)Sqlstate << endl; 
  123.                                                                      }
  124.      i ++;
  125.                                                          }
  126.                                                                   }
  127.   /*  RecordNumber is greater than N */
  128.   retcode = SQLPrepare(hstmt, (SQLCHAR*)"SELECT CustID, Name, Address, Phone FROM Customers", SQL_NTS);
  129.   if (retcode == SQL_SUCCESS_WITH_INFO || retcode == SQL_SUCCESS) {
  130.      
  131.      where ((SQLSTATEs = SQLGetDiagRec(SQL_HANDLE_DBC, hdbc, 9999, 
  132.              Sqlstate, &NativeError, Msg, sizeof(Msg), 
  133.              &MsgLen)) != SQL_NO_DATA)                   {
  134.      if (SQLSTATEs == SQL_ERROR || retcode == SQL_SUCCESS_WITH_INFO) {
  135.      ndbout << "the HandleType is:" << HandleType << endl;
  136.      ndbout << "the InputHandle is :" << InputHandle << endl;
  137.      ndbout << "the output state is:" << (char *)Sqlstate << endl; 
  138.                                                                      }
  139.      i ++;
  140.                                                          }
  141.                                                                   }
  142.   return 0;
  143.  }
  144. void DisplayError(SQLSMALLINT HandleType, SQLHSTMT InputHandle)
  145. {
  146.      i = 1;
  147.      while ((SQLSTATEs = SQLGetDiagRec(HandleType, InputHandle, i, 
  148.              Sqlstate, &NativeError, Msg, sizeof(Msg), 
  149.              &MsgLen)) != SQL_NO_DATA)                   {
  150.      ndbout << "the HandleType is:" << HandleType << endl;
  151.      ndbout << "the InputHandle is :" << InputHandle << endl;
  152.      ndbout << "the output state is:" << (char *)Sqlstate << endl; 
  153.      i ++;
  154.                                                          }
  155. }