Expecting different result from query to database QSqlQuery
-
I make a query to check if a user exists by login and password, I supply the logs and password of an existing user with the correct password, but the database returns "no". Here is the code:
auto Server::MessageProcessing(QByteArray message) -> void { doc_ = QJsonDocument::fromJson(message, &doc_error_); if (doc_error_.errorString()=="no error occurred") { if (doc_.object().value("type").toString() == "auth") { QByteArray result = "{\"type\":\"resultSelect\",\"result\":"; QSqlQuery * query = new QSqlQuery(db_); query->prepare("select id from users where login = :login and pass = :pass;"); query->bindValue(":login", "\'" + doc_.object().value("login").toString() + "\'"); query->bindValue(":pass:", "\'" + doc_.object().value("pass").toString()); if (query->exec()) { query->next(); if (query->isNull(0)) { result.append("\"no\"}"); } else { result.append("\"yes\"}"); } SendToClient(result); } else { qDebug() << "invalid query"; } } } else { qDebug() << "Error read message"; } }
here is result query:
-
I make a query to check if a user exists by login and password, I supply the logs and password of an existing user with the correct password, but the database returns "no". Here is the code:
auto Server::MessageProcessing(QByteArray message) -> void { doc_ = QJsonDocument::fromJson(message, &doc_error_); if (doc_error_.errorString()=="no error occurred") { if (doc_.object().value("type").toString() == "auth") { QByteArray result = "{\"type\":\"resultSelect\",\"result\":"; QSqlQuery * query = new QSqlQuery(db_); query->prepare("select id from users where login = :login and pass = :pass;"); query->bindValue(":login", "\'" + doc_.object().value("login").toString() + "\'"); query->bindValue(":pass:", "\'" + doc_.object().value("pass").toString()); if (query->exec()) { query->next(); if (query->isNull(0)) { result.append("\"no\"}"); } else { result.append("\"yes\"}"); } SendToClient(result); } else { qDebug() << "invalid query"; } } } else { qDebug() << "Error read message"; } }
here is result query:
@Idodoqdo
I rewrote it but it still doesn't work:QByteArray result = "{\"type\":\"resultSelect\",\"result\":"; QSqlQuery * query = new QSqlQuery(db_); query->prepare("select id from users where login = ? and pass = ?;"); query->bindValue(0, "\'" + doc_.object().value("login").toString() + "\'"); query->bindValue(1, "\'" + doc_.object().value("pass").toString() + "\'"); if (query->exec()) { qDebug() << query->executedQuery(); if (query->first()) { result.append("\"yes\"}"); } else { result.append("\"no\"}"); } SendToClient(result);
-
@Idodoqdo
I rewrote it but it still doesn't work:QByteArray result = "{\"type\":\"resultSelect\",\"result\":"; QSqlQuery * query = new QSqlQuery(db_); query->prepare("select id from users where login = ? and pass = ?;"); query->bindValue(0, "\'" + doc_.object().value("login").toString() + "\'"); query->bindValue(1, "\'" + doc_.object().value("pass").toString() + "\'"); if (query->exec()) { qDebug() << query->executedQuery(); if (query->first()) { result.append("\"yes\"}"); } else { result.append("\"no\"}"); } SendToClient(result);
QByteArray result = "{\"type\":\"resultSelect\",\"result\":"; QSqlQuery * query = new QSqlQuery(db_); query->prepare("select id from users where login =:login and pass =:pass;"); query->bindValue(":login", "\'" + doc_.object().value("login").toString() + "\'"); query->bindValue(":pass", "\'" + doc_.object().value("pass").toString() + "\'"); if (query->exec()) { qDebug() << query->executedQuery(); if (query->first()) { result.append("\"yes\"}"); } else { result.append("\"no\"}"); } SendToClient(result);
i get the following: "select id from users where login =:login and pass =:pass;"
-
QByteArray result = "{\"type\":\"resultSelect\",\"result\":"; QSqlQuery * query = new QSqlQuery(db_); query->prepare("select id from users where login =:login and pass =:pass;"); query->bindValue(":login", "\'" + doc_.object().value("login").toString() + "\'"); query->bindValue(":pass", "\'" + doc_.object().value("pass").toString() + "\'"); if (query->exec()) { qDebug() << query->executedQuery(); if (query->first()) { result.append("\"yes\"}"); } else { result.append("\"no\"}"); } SendToClient(result);
i get the following: "select id from users where login =:login and pass =:pass;"
@Idodoqdo
When you bind string parameters for SQL queries you are supposed to just set them to the desired string, not quote them. You are putting'
s around your string values, that would be appropriate if you wrote the query out as expanded text but not for bound parameter values. -
@Idodoqdo
When you bind string parameters for SQL queries you are supposed to just set them to the desired string, not quote them. You are putting'
s around your string values, that would be appropriate if you wrote the query out as expanded text but not for bound parameter values.