Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Expecting different result from query to database QSqlQuery
Forum Updated to NodeBB v4.3 + New Features

Expecting different result from query to database QSqlQuery

Scheduled Pinned Locked Moved Solved General and Desktop
5 Posts 2 Posters 323 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • I Offline
    I Offline
    Idodoqdo
    wrote on last edited by
    #1

    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:
    98ec2a7c-bd2c-4fbc-9fa2-9e70ea05be86-image.png

    I 1 Reply Last reply
    0
    • I Idodoqdo

      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:
      98ec2a7c-bd2c-4fbc-9fa2-9e70ea05be86-image.png

      I Offline
      I Offline
      Idodoqdo
      wrote on last edited by
      #2

      @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);
      
      I 1 Reply Last reply
      0
      • I Idodoqdo

        @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);
        
        I Offline
        I Offline
        Idodoqdo
        wrote on last edited by
        #3

        @Idodoqdo

        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;"

        JonBJ 1 Reply Last reply
        0
        • I Idodoqdo

          @Idodoqdo

          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;"

          JonBJ Offline
          JonBJ Offline
          JonB
          wrote on last edited by
          #4

          @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.

          I 1 Reply Last reply
          2
          • JonBJ JonB

            @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.

            I Offline
            I Offline
            Idodoqdo
            wrote on last edited by
            #5

            @JonB Thank you very much for everything, it helped

            1 Reply Last reply
            0

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved