Reputation: 319
The following code fails on QSqlQuery::exec()
. When I run the query without the prepared statement argument, it returns the correct results, available as one would expect through QSqlQuery::next()
executedQuery()
reports that ":username"
was in fact not replaced by the argument, but with a ?
(Is it supposed to, since the query did not execute successfully?).
db_
is a QSqlDatabase class variable and isOpen()
reports true
. The Qt framework verison is 4.7.3.
DBSql::userInfo(const QString &username,
QString &passwd,
QString &name,
UserPriv priv)
{
QSqlQuery userQuery(db_);
const QString userStr("SELECT u.id, u.fullname, u.password, p.description \
FROM users u INNER JOIN privilege p ON u.privilege_id = p.id \
WHERE u.username = :username");
userQuery.bindValue(":username", username);
if (!userQuery.prepare(userStr))
std::cout << "prepare failed" << std::endl;
if (userQuery.exec()) {
while (userQuery.next()) {
userId = userQuery.value(0).toInt();
name = userQuery.value(1).toString();
passwd = userQuery.value(2).toString();
const QString privilegeDesc = userQuery.value(3).toString();
}
}
}
Upvotes: 4
Views: 6025
Reputation: 1277
You have to call userQuery->prepare(userStr)
before userQuery->bindValue(":username", username)
Upvotes: 7