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. Only the last result value of the query is output

Only the last result value of the query is output

Scheduled Pinned Locked Moved Solved General and Desktop
4 Posts 3 Posters 258 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.
  • M Offline
    M Offline
    meria0503
    wrote on last edited by
    #1

    Hello, I am inevitably using QTableWidget, but I am troubled that only one result value of the query is printed continuously.

    How can I print out all the values?

    Also, I can't use the sqlQuery model for QTableWidget, so is there a way to use it?

    Post the code and pictures of the results below.

    Also, please understand that the translation is not smooth.

    void MainWindow::reporttable(){
      
        QString TIME = ui->selectdateEdit_2->text();
        QString CLOCK = ui->selectdateEdit_3->text();
        QString box = ui->comboBox_Select_2->currentText();
        QSqlQuery query;
        QString sql;
    
      sql = QString("select max(case when t_block='JB001' then T_FlOW1 end) as JB001, max(case when t_block='JB002' then T_FlOW1 end) as JB002 from ulsansstbl WHERE T_DATE BETWEEN '%1 00:00:00' AND '%2 23:59:59' AND t_block like '%3%' group by t_date order by t_date").arg(TIME).arg(CLOCK).arg(box);
    
            if(!query.exec(sql)){
               qDebug () <<"query error:" << query.lastError();
               } else {
                  qDebug () <<"query access:" << query.lastQuery();
    
                  int li = 7;
    
                  QString str;
    
                  while(query.next()) {
                      for(int i = 0; i != li; i+=1){
                          ui->tableWidget->setItem(i+5,1,new QTableWidgetItem(str));
                      }
                      ui->tableWidget->setRowCount(li);
    
    
                      str = query.value(0).toString();
    
    
                      qDebug() << "value:" << str;
    
                  }
    
    
    
            }
    }
    

    This is what I'm developing on the Qtablewidget.
    화면 캡처 2022-12-12 112803.png

    This is the value of the data on the oracle DB.
    화면 캡처 2022-12-12 112908.png

    This is the value displayed in the qt internal Debug. As you can see, the value is normally printed.

    However, the QTablewidget does not display two values, and only one is output.
    화면 캡처 2022-12-12 113002.png

    1 Reply Last reply
    0
    • C Offline
      C Offline
      ChrisW67
      wrote on last edited by
      #2

      The logic you are using to fill the table appears to be broken.

      sql = QString("select max(case when t_block='JB001' then T_FlOW1 end) as JB001, max(case when t_block='JB002' then T_FlOW1 end) as JB002 from ulsansstbl WHERE T_DATE BETWEEN '%1 00:00:00' AND '%2 23:59:59' AND t_block like '%3%' group by t_date order by t_date").arg(TIME).arg(CLOCK).arg(box);
      

      Returns as many rows as there are with distinct t_date values between TIME and CLOCK. This might be zero or more rows.
      BTW: You really should use bind variables not string substitution.

              if(!query.exec(sql)){
                 qDebug () <<"query error:" << query.lastError();
                 } else {
                    qDebug () <<"query access:" << query.lastQuery();
      
                    int li = 7;
      
                    QString str;
      

      How many rows does the table widget have at this point? You set it to li later, but it might be anything here.
      The str variable is a null string here and you use it a few lines down without ever setting it.

                    while(query.next()) {
                        for(int i = 0; i != li; i+=1){
                            ui->tableWidget->setItem(i+5,1,new QTableWidgetItem(str));
                        }
      

      You put the same value into column 1 of rows 5 to 11 ( i.e. 0+5 to 6+5) of the table widget. This seems unusual, but only you know what your table looks like.

      The first time through the while loop str will be a null string.
      The second and later loops, str will contain the value from the previous result record.
      You will never insert the value from the last result record.

                        ui->tableWidget->setRowCount(li);
      

      Now you set the table to have li (7) rows. Even if there were previously rows 7 to 11, they are gone now.
      Why set this fixed row count on every result record?

                        str = query.value(0).toString();
      

      Only now do you fetch the value from the result record into str. This should happen earlier.

                        qDebug() << "value:" << str;
                    }  // end of while
      
      1 Reply Last reply
      2
      • mrjjM Offline
        mrjjM Offline
        mrjj
        Lifetime Qt Champion
        wrote on last edited by mrjj
        #3

        Hi
        You seem add all data to col 1
        Have you seen the docs ?
        https://doc.qt.io/qt-6/qsqlquery.html

        You can use
        query.value(0).toString();
        to get data for col 1 and so on.

        But if all you wish is to load the data into a table as 1:1 mapping, you can get this "code free" if you use

        https://doc.qt.io/qt-6/qsqlquerymodel.html
        and a QTableView.
        You simply use the query of the model and the view will just display the data.

        ahh. ChrisW67 was faster :)

        M 1 Reply Last reply
        0
        • mrjjM mrjj

          Hi
          You seem add all data to col 1
          Have you seen the docs ?
          https://doc.qt.io/qt-6/qsqlquery.html

          You can use
          query.value(0).toString();
          to get data for col 1 and so on.

          But if all you wish is to load the data into a table as 1:1 mapping, you can get this "code free" if you use

          https://doc.qt.io/qt-6/qsqlquerymodel.html
          and a QTableView.
          You simply use the query of the model and the view will just display the data.

          ahh. ChrisW67 was faster :)

          M Offline
          M Offline
          meria0503
          wrote on last edited by
          #4

          @mrjj Thank you for your answer. As you said, I used QTableView at first, but I inevitably use QTableWidget because I have to use Excel library. Therefore, you cannot use the sqlquery model.

          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