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. Using variables in fetching data from Database

Using variables in fetching data from Database

Scheduled Pinned Locked Moved General and Desktop
9 Posts 4 Posters 2.7k 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.
  • G Offline
    G Offline
    gor kogalo
    wrote on last edited by
    #1

    hi. I have a code which is suppose to chec if the admission number being assigned to the new student is already assigned to another student. if already assigned, the system is to alert the user. i have this code but it is not working as required. what might be the problem?
    @
    QString admNum = admNumLineEdit->text();
    QSqlDatabase db = QSqlDatabase::addDatabase(QMYSQL);
    db.setHostName("localhost");
    db.setDatabaseName("school");
    db.setUserName("student");
    db.setPassword("student");
    QSqlQuery qeury;
    if(!db.open())
    {
    ....
    }
    else
    {
    query.prepare("SELECT adm FROM student_info WHERE adm = ':admission'");
    query.bindValue(":admission", admNum);
    if(!query.exec())
    {
    qDebug() << query.lastError();
    }
    else
    {
    QSqlRecord rec = query.record();
    int cols = rec.count();
    if(cols == 0)
    {
    QMessageBox::information(this, "admission number available", "Admission number can be assigned");
    }
    else
    {
    QMessageBox::information(this, "Invalid admission number", "Admission number already exist");
    }
    }
    }
    @
    if I assign admNum to the same value as the one in the database or a different value, second qmessagebox is displayed. what can be the problem?

    Gor

    1 Reply Last reply
    0
    • S Offline
      S Offline
      sptrakesh
      wrote on last edited by
      #2

      Why are you counting columns to determine if your query had results?

      1 Reply Last reply
      0
      • SGaistS Offline
        SGaistS Offline
        SGaist
        Lifetime Qt Champion
        wrote on last edited by
        #3

        Hi,

        QSqlRecord::count returns the number of fields that your query returns, so you'll always have one (adm).

        You should rather test query.next()

        Interested in AI ? www.idiap.ch
        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

        1 Reply Last reply
        0
        • G Offline
          G Offline
          gor kogalo
          wrote on last edited by
          #4

          thanks for your feedback. what i need is to check if there is any row in the table with the same admNum because if the same value is in the table the number of row should be > 0. how do i count the number iof rows?

          Gor

          1 Reply Last reply
          0
          • SGaistS Offline
            SGaistS Offline
            SGaist
            Lifetime Qt Champion
            wrote on last edited by
            #5

            Since you're supposed to only have one row for each number, next will fail if there aren't any. If that's not enough, use QSqlQuery::size() (don't forget to check that your driver has this feature).

            Interested in AI ? www.idiap.ch
            Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

            1 Reply Last reply
            0
            • G Offline
              G Offline
              gor kogalo
              wrote on last edited by
              #6

              i modified my code as shown and it worked. Thanks for the advice SGaist.
              @
              query. prepare( "SELECT
              adm FROM student_info WHERE
              adm = ':admission'" );
              query. bindValue
              ( ":admission" , admNum);
              if(! query. exec())
              {
              qDebug() << query. lastError();
              }
              else
              {
              int numrow = query.size();
              if (numrow == 0)
              {
              QMessageBox::information
              ( this , "admission number
              available" , "Admission
              number can be assigned" );
              }
              else
              {
              QMessageBox::information
              ( this , "Invalid admission
              number" , "Admission number
              already exist" );
              }
              @

              Gor

              1 Reply Last reply
              0
              • G Offline
                G Offline
                gor kogalo
                wrote on last edited by
                #7

                Am sorry. just noted that the code now runs and the first qmessage box displayed whether the admNum value is the same with what is in the database table or not. any suggestion?

                Gor

                1 Reply Last reply
                0
                • G Offline
                  G Offline
                  gor kogalo
                  wrote on last edited by
                  #8

                  it is now working after removing bindValue
                  @
                  query.prepare("SELECT adm FROM student_info WHERR adm = '"+admNum+"'");
                  @

                  Gor

                  1 Reply Last reply
                  0
                  • Q Offline
                    Q Offline
                    qxoz
                    wrote on last edited by
                    #9

                    Hi!
                    When you bind value to QSqlQuery you shouldn't care about var formats. In your case don't put :admission in to `` . Just write like this:
                    @query. prepare( "SELECT adm FROM student_info WHERE adm = :admission" );
                    query. bindValue ( ":admission" , admNum);
                    @

                    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