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. SQLite databases queries issues
Forum Updated to NodeBB v4.3 + New Features

SQLite databases queries issues

Scheduled Pinned Locked Moved Unsolved General and Desktop
6 Posts 4 Posters 727 Views 1 Watching
  • 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.
  • M Offline
    M Offline
    MR_mn
    wrote on 14 Jan 2019, 10:07 last edited by
    #1

    Hello everybody,
    I'm trying to work with SQLite database to create an application which, based on an user input to be chosen in a comboBox, automatically fills a lineEdit field with a corresponding value from the same table of the database.
    So, for example, I have a database which contains employee's info (name, last name, age, etc..), the comboBox is linked to the column "name" and, based on which name the user chooses, the lineEdit should be populated automatically with the corresponding age.
    So far, I was able to connect the comboBox with the right column in the database, displaying the correct values, extract the corresponding chosen value by the user, but I cannot fill anything in the lineEdit.
    Here is the code I'm working on:

    void MainWindow::on_pushButton_Load_clicked()
    {
        connOpen();
        QSqlQueryModel *modal = new QSqlQueryModel();
    
        QSqlQuery *qry = new QSqlQuery(mydb);
        qry->prepare("select Name from employeeinfo");
        qry->exec();
        modal->setQuery(*qry);
    
        ui->comboBox->setModel(modal);
    
    
        int indexComboBox = ui->comboBox->currentIndex();
    
        QString text= ui->comboBox->currentText();
       
    }
    
    
    void MainWindow::on_comboBox_currentIndexChanged(const QString &arg1)
    {
          
        QString text = ui->comboBox->currentText();
        QSqlQuery *qry_2 = new QSqlQuery;
        qry_2->prepare("SELECT Age FROM Employeeinfo"); //WHERE Age='"+text+"'");
        if(qry_2->exec()) {
                while(qry_2->next()) {
                ui->lineEdit->setText(qry_2->value(5).toString()); }
                         }
        
        qDebug() << qry_2->size()<< qry_2->lastError().text();
    
    }
    
    

    The query size returned from qDebug is -1, and with another qDebug command I was able to see that what extracted from comboBox is fine. No error is returned from qry_2->last error command.

    Does anyone have any hint on this?
    Thanks!

    1 Reply Last reply
    0
    • S Offline
      S Offline
      SGaist
      Lifetime Qt Champion
      wrote on 14 Jan 2019, 10:28 last edited by
      #2

      Hi,

      Please re-read the documentation of QSqlQueryModel. And more specifically the second version of the setQuery method.

      Then there are several things that look wrong:

      • There's no reason to allocate your QSqlQuery object on the heap.
      • The query you use for the model is linked to the mydb database object while the other use not. Why is that ?
      • You are not using the same table name for both queries.<
      • You are leaking QSqlQuery objects in on_comboBox_currentIndexChanged since you never delete them.

      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
      3
      • M Offline
        M Offline
        MR_mn
        wrote on 14 Jan 2019, 13:53 last edited by
        #3

        Hello,
        thanks for your prompt answer, I had a look at the links you provided but honestly I can't see what I'm doing wrong. I'm getting the right value of the comboBox but that's as far as I can go, the code cannot look for the same value in the database table and have a corresponding value filling the lineEdit.

        I'm using the same table name for both queries, the problem is that I changed all the names to post on here just for the sake of clarity.

        Thanks!

        1 Reply Last reply
        0
        • M Offline
          M Offline
          MrShawn
          wrote on 14 Jan 2019, 17:44 last edited by
          #4
          ui->lineEdit->setText(qry_2->value(5).toString())
          

          Okay well first of all in the returned query i would be surprised if you are getting 5 columns returned from that query that only returns 1 column worth of information. Specifically, the 5 there says you want to 5th column's data. You have one column being returned with id of "Age". Change 5 to 0 or "Age" and that will access the column you care about.

          It seems you are just troubleshooting, but right now you are going to get x number of rows and really what will happen is you will set the text x number of times and you will only see the final result which is the last record pulled up. Not sure what you are trying to do with the while loop but if you plan to have only one record, say after setting your where clause with the employee name then i suggest changing your code to simply be

          if(!qry_2->exec())
          {
               //Error occurred while trying to execute query
               ui->lineEdit->setText("Age not found");
               qDebug() << qry_2->lastError().text();
               return;
          }
          
          qry_2->next();
          if (qry_2->value("Age").isNull())
               ui->lineEdit->setText("Age not found");     //Your query did not find any records...
          else
               ui->lineEdit->setText(qry_2->value("Age").toString());
          
          1 Reply Last reply
          0
          • M Offline
            M Offline
            MrShawn
            wrote on 14 Jan 2019, 17:48 last edited by
            #5

            @SGaist is right about leaking, but you can just change off of pointers and they will just be scoped to your function.

            QSqlQuery qry_2(mydb);
            
            1 Reply Last reply
            0
            • V Offline
              V Offline
              VRonin
              wrote on 14 Jan 2019, 17:53 last edited by
              #6

              In addition to what @SGaist said, you should not keep QSqlDatabase variables around. they should be local scoped. see the docs for the official warning.

              Coming to your problem, why don't you just change the query to "select Name, Age from employeeinfo" and then use ui->lineEdit->setText(ui->comboBox->model()->index(ui->comboBox->currentIndex(),1).data().tostring()); to retrieve the age? no need to execute the query twice

              "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
              ~Napoleon Bonaparte

              On a crusade to banish setIndexWidget() from the holy land of Qt

              1 Reply Last reply
              2

              3/6

              14 Jan 2019, 13:53

              • Login

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