views:

69

answers:

1

I've read the tutorial at, and I generally get how that works: http://tangentsoft.net/mysql++/doc/html/userman/tutorial.html#simple

I am trying to build this mysql++ code, and I'm getting an error:

std::ostringstream query3;
query3<<"select pipe_id from pipe where version_id='"<<id<<"'";
std::storeQueryResult ares=query3.store();

for(size_t i=0;i<ares.num_rows();i++)   
   cout<<ares[i]["version_id"]<<ares[i]["pipe_id"]<<std::endl;

mysql_query(&mysql,query3.str().c_str());

The error is that store is not a member of ostringstream. I'm not sure how to correct the problem.


Hi Merlyn,

Thanks for the code and looking at my problem.

I tried the above code but again I am getting the error

error: request for member 'query' in 'connection' which is non-class type 'MYSQL*'

on this line of code

// Construct a query object with the query string mysqlpp::Query query = 
connection.query(query_string);

kindly help where I am going wrong?

+3  A: 

The problem is that you have to use a mysql++ query object to execute a query, not an ostringstream. The ostringstream just lets you build the query string, but won't let you perform the query.

There is a tutorial that shows basic usage at: http://tangentsoft.net/mysql++/doc/html/userman/tutorial.html#simple

To get from your code to a query that works, you need to take your dynamic query, turn it into a string, and use it to construct the mysql++ query object.

// todo: create the connection here

// Construct the query string.  You were already doing this in your code
std::ostringstream query_builder;
query_builder << "select pipe_id from pipe where version_id='" << id << "'";

// Convert the ostringstream to a string
std::string query_string = query_builder.str();

// Construct a query object with the query string
mysqlpp::Query query = connection.query(query_string);

// Perform the query
mysqlpp::StoreQueryResult result = query.store();
for(size_t i = 0; i < result.num_rows(); i++)
   std::cout << result[i]["version_id"] << result[i]["pipe_id"] << std::endl;
Merlyn Morgan-Graham
If `connection.query(query_string)` doesn't work, you can try `connection.query(query_string.c_str())` instead.
Merlyn Morgan-Graham