List:MySQL++« Previous MessageNext Message »
From:Joseph Hesse Date:August 6 2013 7:02pm
Subject:Re: Problem with a query
View as plain text  
On 08/05/2013 07:26 PM, Warren Young wrote:
> On 8/5/2013 16:51, Joseph Hesse wrote:
>> Is the following code OK?  Assume "conn" is valid.  Queries Q1 and Q2
>> are MySQL queries written as C++ strings and do not return anything.
>>
>>    Query query = conn.query(Q1);
>>    query.exec();
>>    query.reset();
>>    query = conn.query(Q2);  // query.str() is empty after this statement
>>    query.exec();
>
> You don't need the reset() call since you aren't using parse(). Query 
> objects auto-reset after a successful query otherwise, so that you can 
> do this very sort of Query object reuse you're trying here.
>
>> I diagnosed the problem by printing query.str() after both conn.query.
>
> Are you sure you didn't call exec() first, thus auto-resetting the 
> Query object?
>
> If not, are you certain the first exec() didn't fail?  If you're 
> suppressing exceptions, are you checking return values?  If the query 
> fails, the Query object won't auto-reset, on purpose, so that you can 
> retrieve an automatically built-up query string for error messages, 
> logging, etc:
>
>     if (!query.exec()) {
>         cerr << "Query failed: " << query << endl;
>     }
>
> If MySQL++ didn't behave this way, those wanting to log failed queries 
> would need to cache the query string before executing it, purely on 
> the off chance that it would fail.
>
I have included working test code with exceptions and output to 
demonstrate the problem.  Basically it boils down to doing "query = 
conn.query(Q2);" and finding that query.str() is empty.
Thank you for looking at this.

Here is the code:
=================Test.cpp==================================================
#include <mysql++.h>
#include <iostream>

using namespace std;
using namespace mysqlpp;

// The strings below, as MySQL queries, create temporary tables
static string TempTable1 = string(" not shown ");
static string TempTable2 = string(" not shown ");

int main(int argc, char *argv[])
{
   try
   {
     // connect, exceptions enabled
     Connection conn("xxxx", "localhost",    "xx", "xx");  // password 
not shown

     Query query = conn.query(TempTable1);
     query.exec();

     query = conn.query(TempTable2);
     cerr << "Size of string TempTable2 is : " << TempTable2.length() <<

endl;
     cerr << "Size of 2nd query string is : " << query.str().length() <<

", value is : " << query.str() << endl;
     query.exec();

     return 0;
   }
   catch(const ConnectionFailed& err)
   {
     cerr << "CONNECTION ERROR: " << err.what() << endl;
     return 1;
   }
   catch(const mysqlpp::BadFieldName& er)
   {
     cerr << "BAD FIELD NAME ERROR: " << er.what() << endl;
     return 1;
   }
   catch(const mysqlpp::BadQuery& er)
   {
     cerr << "QUERY ERROR: " << er.what() << endl;
     return 1;
   }
   catch(const BadConversion& er)
   {
     cerr << "CONVERSION ERROR: " << er.what() << endl;
     cerr << "Retrieved data size: " << er.retrieved << ", actual size: 
" << er.actual_size << endl;
     return 1;
   }
   catch(const mysqlpp::Exception& er)
   {
     // Catch-all for any other MySQL++ exceptions
     cerr << "GENERAL ERROR: " << er.what() << endl;
     return 1;
   }
}
===========================================================================
Here is the program output:
$ g++ -o Test -I/usr/include/mysql -I/usr/include/mysql++ -lmysqlpp Test.cpp
$ ./Test
Size of string TempTable2 is : 118
Size of 2nd query string is : 0, value is :
QUERY ERROR: Query was empty


Thread
Problem with a queryJoseph Hesse5 Aug
  • Re: Problem with a queryWarren Young6 Aug
    • Re: Problem with a queryJoseph Hesse6 Aug
      • Re: Problem with a queryWarren Young6 Aug