我正在构建一个在MySQL数据库上进行查询的QT应用程序。当我query.exec("call SelectUsersPhotos()")
时,我得到的回复很好但是如果我在prepare
上设置查询字符串,如下面的代码所示,数据库不会返回任何值,也不会返回任何值。扔任何错误。
if (db.open()) {
cout << "Querying GetUserPhotos from user " << user->Name << endl;
QSqlQuery query;
query.prepare("call SelectUsersPhotos()");
query.exec();
while (query.next()) {
UserPhoto *userPhoto = new UserPhoto();
userPhoto->UserPhotoId = query.value(0).toInt();
userPhoto->UserId = query.value(1).toInt();
userPhoto->Created = query.value(2).toDateTime();
userPhoto->Name = query.value(3).toString().toStdString();
usersPhotos->push_back(userPhoto);
}
query.finish();
db.close();
cout << "Photos " << usersPhotos->size() << endl;
}
答案 0 :(得分:-1)
“准备执行SQL查询查询。如果查询准备成功,则返回true;否则返回false。”
也许你应该尝试
bool b = query.prepare("call SelectUsersPhotos()");
while(!b)
b = query.prepare("call SelectUsersPhotos()");
query.exec();
编辑: 功能签名是
bool QSqlQuery::prepare ( const QString & query )
所以它需要值的地址但是你传递一个rvalue作为参数。也许你应该试试
QString qstr("call SelectUsersPhotos()");
query.prepare(qstr);
query.exec(qstr);
注意我不知道为什么exec(const QString&amp; query)即使它具有相同的引用参数也能正常工作。