我已经阅读了该教程,我通常会得到它的工作方式:http://tangentsoft.net/mysql+ +/doc/html/userman/tutorial.html#simple

我正在尝试构建此MySQL ++代码,并且我遇到了一个错误:

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());

错误是 store 不是 ostringstream. 。我不确定如何纠正问题。


嗨,梅林,

感谢您的代码并查看我的问题。

我尝试了上述代码,但我再次遇到了错误

错误:在“连接”中要求成员'QUERY'的请求,该'连接'是非类型'mysql*'的请求

在这条代码上

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

请帮助我出错的地方吗?

有帮助吗?

解决方案

问题在于,您必须使用MySQL ++查询对象来执行查询,而不是OSTRINGSTREAM。 OSTRINGSTREAM可以允许您构建查询字符串,但不会让您执行查询。

有一个教程显示基本用法:http://tangentsoft.net/mysql+ +/doc/html/userman/tutorial.html#simple

要从代码到有效的查询,您需要采用动态查询,将其转换为字符串,然后使用它来构造MySQL ++查询对象。

// 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;
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top