C++ c++;mysql++;若查询为空,则代码永远不会执行,但应该执行
若查询为空,则代码永远不会执行。我尝试了多种变化。下面是简单的代码C++ c++;mysql++;若查询为空,则代码永远不会执行,但应该执行,c++,mysql++,C++,Mysql++,若查询为空,则代码永远不会执行。我尝试了多种变化。下面是简单的代码 mysqlpp::Query query = conn.query(); query << "SELECT * FROM users WHERE username= " << mysqlpp::quote_only << username << "AND password= " << mysqlpp
mysqlpp::Query query = conn.query();
query << "SELECT * FROM users WHERE username= "
<< mysqlpp::quote_only << username
<< "AND password= "
<< mysqlpp::quote_only << password;
mysqlpp::StoreQueryResult res = query.store();
mysqlpp::StoreQueryResult::const_iterator it;
for (it = res.begin(); it != res.end(); ++it)
{
mysqlpp::Row row = *it;
if (!row.empty())
{
// user name and password match, log them in
std::cout << "You are logged" << std::endl;
// rest of code goes here
}
else if (row.empty()) // even just 'else' doesnt get executed
{
// no username or password that matches with user input
std::cout << "Wrong username or password" << std::endl;
// rest of code goes here
// this never get executed, and i have no idea why
}
}
mysqlpp::Query Query=conn.Query();
查询我甚至可以使用>if(row){//log them in}但是,如果(!row)它永远不会执行。胡乱猜测:res.begin()==res.end()
刚刚尝试过,代码仍然不会执行。如果没有行,并且迭代器的begin和end是相等的,那么它就是!=res.end()将在第一次迭代时失败,循环的内部代码将不会执行。。。如(i=0;i facepalm)的中所述。你是对的,我重新编辑了代码,并且代码正常工作。谢谢你,约翰