QSqlQuery prepared SELECT statement does not work on exec ()

The following code does not work on QSqlQuery::exec() . When I run a query without an argument to a finished statement, it returns the correct results, available, as one would expect through QSqlQuery::next()

executedQuery() reports that ":username" not actually replaced with an argument, but with ? (Is it assumed since the request was not successful?).

db_ is a QSqlDatabase class variable and isOpen() reports true . The skeleton designation Qt 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(); } } } 
+4
source share
1 answer

You need to call userQuery->prepare(userStr) to userQuery->bindValue(":username", username)

+6
source

Source: https://habr.com/ru/post/1445030/


All Articles