I was trying to select rows using mysql++
Here is my code
int main(int argc,char *argv[])
{
mysql_init(&mysql);
connection = mysql_real_connect(&mysql,.............,"3306",0);
if (connection == NULL)
{
cout << mysql_error(&mysql);
return 1;
}
else
{
int id=1;
mysqlpp::Query query=connection.query("select * from pipe");
//query3<<"select pipe_id from pipe where version_id='"<<id<<"'";
if (mysqlpp::StoreQueryResult ares=query.store());
for(size_t i=0;i<ares.num_rows();i++)
38 cout<<ares[i]["version_id"]<<ares[i]["pipe_id"]<<std::endl;
// my开发者_如何学Gosql_query(&mysql,query3.str().c_str());
}
mysql_close(connection);
return 0;
};
I am getting this error
In function ‘int main(int, char**)’:
error: request for member ‘query’ in ‘connection’, which is of non-class type ‘MYSQL*’
You are mixing calls to the C API and the C++ API, this approach won't work.
I suggest you look at the MySQL++ turorial, and e.g. the example at http://tangentsoft.net/mysql++/doc/html/userman/tutorial.html
In particular, replace this:
mysql_init(&mysql);
connection = mysql_real_connect(&mysql,.............,"3306",0);
if (connection == NULL)
{
cout << mysql_error(&mysql);
return 1;
}
else
{
with
mysqlpp::Connection conn(false);
if (!conn.connect("mydb", "localhost", "myuser", "mypassword")) {
cout << "DB connection failed: " << conn.error() << endl;
return 1;
} else {
remove ; after if here:
if (mysqlpp::StoreQueryResult ares=query.store()); <----
I would recommend to use brackets around if body, just for readability and other surprises. You could also check the compiler warnings. I am sure that the compiler will warn you about this statement.
Move the declaration and initialization of ares variable outside if statement, too.
Later edit: connection is a pointer. So use indirection, meaning -> instead of .
精彩评论