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. SQLite3 Master/Detail | Can't get data in the Detail table
Forum Updated to NodeBB v4.3 + New Features

SQLite3 Master/Detail | Can't get data in the Detail table

Scheduled Pinned Locked Moved Solved General and Desktop
18 Posts 3 Posters 2.2k 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.
  • J JonB
    11 Aug 2018, 11:44

    @landslyde
    I assume you have checked that on_tw_recipes_cellActivated() is actually called correctly, and is issuing the SQL statements, and gets back the right numbers of rows & columns....?

    Try ensuring that tw_recipeIngredients has the correct number of columns via setColumnCount(), in case....?

    It won't solve whatever your problem is, but in the long run I wouldn't be issuing a separate, initial SELECT COUNT(*) query to get the number of rows to expect each time. This is not good for a variety of reasons. Just do the second SELECT to get the actual rows, read them while (q.isValid()), don't do the QTableWidget::setRowCount(), just append the rows as needed via http://doc.qt.io/qt-5/qtablewidget.html#insertRow.

    ? Offline
    ? Offline
    A Former User
    wrote on 11 Aug 2018, 12:50 last edited by
    #3

    @JonB - I appreciate your input. I removed the SELECT COUNT(*) and changed to while (q.isValid()). That's a lot cleaner. And the change messed up the recipe names from printing out to the first table, I'll take the time to dig in deeper and see what's wrong with it now and work toward having a solid understanding of what I'm doing. Like I said, this is all new to me. The code I showed was some I got from a youtube vid. So I seriously appreciate you taking the time to send in a better direction. Thanks.

    On another note: How do you get the highlighted text? Red on the light red background.

    M 1 Reply Last reply 11 Aug 2018, 14:53
    1
    • ? A Former User
      11 Aug 2018, 12:50

      @JonB - I appreciate your input. I removed the SELECT COUNT(*) and changed to while (q.isValid()). That's a lot cleaner. And the change messed up the recipe names from printing out to the first table, I'll take the time to dig in deeper and see what's wrong with it now and work toward having a solid understanding of what I'm doing. Like I said, this is all new to me. The code I showed was some I got from a youtube vid. So I seriously appreciate you taking the time to send in a better direction. Thanks.

      On another note: How do you get the highlighted text? Red on the light red background.

      M Offline
      M Offline
      mrjj
      Lifetime Qt Champion
      wrote on 11 Aug 2018, 14:53 last edited by
      #4

      @landslyde
      Red text

      `test`
      

      test

      ? 1 Reply Last reply 11 Aug 2018, 15:02
      0
      • J JonB
        11 Aug 2018, 11:44

        @landslyde
        I assume you have checked that on_tw_recipes_cellActivated() is actually called correctly, and is issuing the SQL statements, and gets back the right numbers of rows & columns....?

        Try ensuring that tw_recipeIngredients has the correct number of columns via setColumnCount(), in case....?

        It won't solve whatever your problem is, but in the long run I wouldn't be issuing a separate, initial SELECT COUNT(*) query to get the number of rows to expect each time. This is not good for a variety of reasons. Just do the second SELECT to get the actual rows, read them while (q.isValid()), don't do the QTableWidget::setRowCount(), just append the rows as needed via http://doc.qt.io/qt-5/qtablewidget.html#insertRow.

        ? Offline
        ? Offline
        A Former User
        wrote on 11 Aug 2018, 14:56 last edited by A Former User 8 Nov 2018, 15:00
        #5

        @JonB - I changed the first function to this:

        void MainWindow::FillTable()
        {
            loading = true;
            QSqlQuery q(db);
        
            int r = 0, c = 0;
            
            qDebug()<< "Before SELECT";
            if (!q.exec("SELECT recipe_id, name FROM recipes "
                        "ORDER BY name")) db.lastError().text();
            qDebug()<< "After SELECT";
            qDebug() << r;
            ++r;
            qDebug() << r;
            qDebug() << q.isValid();
            while (q.isValid())  // RETURNS FALSE EACH TIME NOW. THIS TABLE HAS 4 ENTRIES IN IT.
            {
                qDebug() << "Inside WHILE";
               // ++r;
                ui->tw_recipes->insertRow(r);
                for (c = 0; c < 2; ++c)
                {
                    qDebug() << c;
                    qDebug()<< "Inside FOR";
                    ui->tw_recipes->setItem(r, c, new QTableWidgetItem(q.value(c).toString()));
                }
            }
            loading = false;
        }
        

        My output is:

        09:46:25: Starting /home/slyde/Desktop/Qt Apps/build-Crumbs-Desktop_Qt_5_11_0_GCC_64bit-Debug/Crumbs...
        Before SELECT
        After SELECT
        0
        1
        false
        09:46:34: /home/slyde/Desktop/Qt Apps/build-Crumbs-Desktop_Qt_5_11_0_GCC_64bit-Debug/Crumbs exited with code 0
        

        Can anyone tell me why

        while (q.isValid())
        

        is failing? I see nothing wrong with my query. But obviously something's fouled up. Anyone?
        alt text
        Not sure how to post a pic here. So here's a link to show the table's valid entries: https://imgur.com/L29lGbb

        J 1 Reply Last reply 11 Aug 2018, 15:08
        0
        • M mrjj
          11 Aug 2018, 14:53

          @landslyde
          Red text

          `test`
          

          test

          ? Offline
          ? Offline
          A Former User
          wrote on 11 Aug 2018, 15:02 last edited by
          #6

          @mrjj - Thank you.

          M 1 Reply Last reply 11 Aug 2018, 15:06
          0
          • ? A Former User
            11 Aug 2018, 15:02

            @mrjj - Thank you.

            M Offline
            M Offline
            mrjj
            Lifetime Qt Champion
            wrote on 11 Aug 2018, 15:06 last edited by
            #7

            @landslyde
            Hi
            Why are you using q.isValid() and not
            http://doc.qt.io/qt-5/qsqlquery.html#next

            Normally to loop the result is like

               QSqlQuery query("SELECT country FROM artist");
                while (query.next()) {
                    QString country = query.value(0).toString();
                    ...      
                }
            

            Did i miss something ?

            ? 1 Reply Last reply 11 Aug 2018, 15:19
            3
            • ? A Former User
              11 Aug 2018, 14:56

              @JonB - I changed the first function to this:

              void MainWindow::FillTable()
              {
                  loading = true;
                  QSqlQuery q(db);
              
                  int r = 0, c = 0;
                  
                  qDebug()<< "Before SELECT";
                  if (!q.exec("SELECT recipe_id, name FROM recipes "
                              "ORDER BY name")) db.lastError().text();
                  qDebug()<< "After SELECT";
                  qDebug() << r;
                  ++r;
                  qDebug() << r;
                  qDebug() << q.isValid();
                  while (q.isValid())  // RETURNS FALSE EACH TIME NOW. THIS TABLE HAS 4 ENTRIES IN IT.
                  {
                      qDebug() << "Inside WHILE";
                     // ++r;
                      ui->tw_recipes->insertRow(r);
                      for (c = 0; c < 2; ++c)
                      {
                          qDebug() << c;
                          qDebug()<< "Inside FOR";
                          ui->tw_recipes->setItem(r, c, new QTableWidgetItem(q.value(c).toString()));
                      }
                  }
                  loading = false;
              }
              

              My output is:

              09:46:25: Starting /home/slyde/Desktop/Qt Apps/build-Crumbs-Desktop_Qt_5_11_0_GCC_64bit-Debug/Crumbs...
              Before SELECT
              After SELECT
              0
              1
              false
              09:46:34: /home/slyde/Desktop/Qt Apps/build-Crumbs-Desktop_Qt_5_11_0_GCC_64bit-Debug/Crumbs exited with code 0
              

              Can anyone tell me why

              while (q.isValid())
              

              is failing? I see nothing wrong with my query. But obviously something's fouled up. Anyone?
              alt text
              Not sure how to post a pic here. So here's a link to show the table's valid entries: https://imgur.com/L29lGbb

              J Offline
              J Offline
              JonB
              wrote on 11 Aug 2018, 15:08 last edited by JonB 8 Nov 2018, 15:10
              #8

              @landslyde
              I copied your use of q.isValid() from your code, I didn't look it up. @mrjj's code is the correct way. Also that might explain your problems...!

              ? 1 Reply Last reply 11 Aug 2018, 15:23
              1
              • M mrjj
                11 Aug 2018, 15:06

                @landslyde
                Hi
                Why are you using q.isValid() and not
                http://doc.qt.io/qt-5/qsqlquery.html#next

                Normally to loop the result is like

                   QSqlQuery query("SELECT country FROM artist");
                    while (query.next()) {
                        QString country = query.value(0).toString();
                        ...      
                    }
                

                Did i miss something ?

                ? Offline
                ? Offline
                A Former User
                wrote on 11 Aug 2018, 15:19 last edited by
                #9

                @mrjj - That was the key to the door. I'm learning. I'm not here to roach code, just to learn. I just didn't know enough to sort that out and get it headed in the right direction. And thank you for the link. Pure gold!

                1 Reply Last reply
                0
                • J JonB
                  11 Aug 2018, 15:08

                  @landslyde
                  I copied your use of q.isValid() from your code, I didn't look it up. @mrjj's code is the correct way. Also that might explain your problems...!

                  ? Offline
                  ? Offline
                  A Former User
                  wrote on 11 Aug 2018, 15:23 last edited by
                  #10

                  @JonB - Your input was appreciated nonetheless. My initial code up there was a hack from a video I watched. While it worked, it wasn't even close to what I know Qt can offer. I hope you guys don't mind my questions. I'm full of them. Really enjoying Qt. Hands down, it's the best!

                  M 1 Reply Last reply 11 Aug 2018, 15:24
                  1
                  • ? A Former User
                    11 Aug 2018, 15:23

                    @JonB - Your input was appreciated nonetheless. My initial code up there was a hack from a video I watched. While it worked, it wasn't even close to what I know Qt can offer. I hope you guys don't mind my questions. I'm full of them. Really enjoying Qt. Hands down, it's the best!

                    M Offline
                    M Offline
                    mrjj
                    Lifetime Qt Champion
                    wrote on 11 Aug 2018, 15:24 last edited by
                    #11

                    @landslyde
                    We basically hang around here for questions. ;)
                    So if you show code,
                    clearly state what you tried,
                    what you got and what you expected you will find us
                    very willing to try to help.

                    Also the online Docs are excellent.

                    ? 1 Reply Last reply 11 Aug 2018, 16:04
                    1
                    • M mrjj
                      11 Aug 2018, 15:24

                      @landslyde
                      We basically hang around here for questions. ;)
                      So if you show code,
                      clearly state what you tried,
                      what you got and what you expected you will find us
                      very willing to try to help.

                      Also the online Docs are excellent.

                      ? Offline
                      ? Offline
                      A Former User
                      wrote on 11 Aug 2018, 16:04 last edited by A Former User 8 Nov 2018, 16:23
                      #12

                      @mrjj - Thank you very much for the welcome. I do have one more question regarding all we've talked abt here. Although my initial post on this was sloppy code, it did fill the TableWidget properly. Now it won't load the four names I have in it.

                      void MainWindow::FillTable()
                      {
                          loading = true;
                          QSqlQuery q(db);
                      
                          int r = 0, c = 0;
                      
                          if (!q.exec("SELECT recipe_id, name FROM recipes "
                                      "ORDER BY name")) db.lastError().text();
                          while (q.next())
                          {
                              ++r;
                              ui->tw_recipes->insertRow(r);
                              for (c = 0; c < 2; ++c)
                              {
                                  qDebug() << r;
                                  qDebug() << c;
                                  qDebug() << q.value(c);
                                  ui->tw_recipes->setItem(r, c, new QTableWidgetItem(q.value(c).toString()));
                              }
                          }
                          loading = false;
                      }
                      

                      Output is:

                      10:55:47: Starting /home/slyde/Desktop/Qt Apps/build-Crumbs-Desktop_Qt_5_11_0_GCC_64bit-Debug/Crumbs...
                      1
                      0
                      QVariant(qlonglong, 3)
                      1
                      1
                      QVariant(QString, "Cat Finger Licks")
                      2
                      0
                      QVariant(qlonglong, 4)
                      2
                      1
                      QVariant(QString, "Chocolate Chip Cookies")
                      3
                      0
                      QVariant(qlonglong, 2)
                      3
                      1
                      QVariant(QString, "Lemon Cookies")
                      4
                      0
                      QVariant(qlonglong, 1)
                      4
                      1
                      QVariant(QString, "Oatmeal Pecan Chewy")
                      

                      This line of code worked before, but now it doesn't:

                      ui->tw_recipes->setItem(r, c, new QTableWidgetItem(q.value(c).toString()));
                      

                      Why wld it not work? Is there something wrong with it? Please advise.

                      Pics of this: https://imgur.com/MsJ9xac and https://imgur.com/iQ4mGoI

                      UPDATE
                      I added a line to it and see that when I insert a row, nothing happens. Look:

                      void MainWindow::FillTable()
                      {
                          loading = true;
                          QSqlQuery q(db);
                      
                          int r = 0, c = 0;
                      
                          if (!q.exec("SELECT recipe_id, name FROM recipes "
                                      "ORDER BY name")) db.lastError().text();
                          while (q.next())
                          {
                              ++r;
                              ui->tw_recipes->insertRow(r);  // NOTHING HAPPENS HERE
                              for (c = 0; c < 2; ++c)
                              {
                                  qDebug() << r;
                                  qDebug() << c;
                                  qDebug() << ui->tw_recipes->currentRow();  // I ADDED THIS LINE TO IT
                                  qDebug() << q.value(c);
                                  ui->tw_recipes->setItem(r, c, new QTableWidgetItem(q.value(c).toString()));
                              }
                          }
                          loading = false;
                      }
                      

                      And the output:

                      11:18:16: Starting /home/slyde/Desktop/Qt Apps/build-Crumbs-Desktop_Qt_5_11_0_GCC_64bit-Debug/Crumbs...
                      1
                      0
                      -1
                      QVariant(qlonglong, 3)
                      1
                      1
                      -1
                      QVariant(QString, "Cat Finger Licks")
                      2
                      0
                      -1
                      QVariant(qlonglong, 4)
                      2
                      1
                      -1
                      QVariant(QString, "Chocolate Chip Cookies")
                      3
                      0
                      -1
                      QVariant(qlonglong, 2)
                      3
                      1
                      -1
                      QVariant(QString, "Lemon Cookies")
                      4
                      0
                      -1
                      QVariant(qlonglong, 1)
                      4
                      1
                      -1
                      QVariant(QString, "Oatmeal Pecan Chewy")
                      

                      What's making it be -1?

                      1 Reply Last reply
                      0
                      • M Offline
                        M Offline
                        mrjj
                        Lifetime Qt Champion
                        wrote on 11 Aug 2018, 17:06 last edited by mrjj 8 Nov 2018, 17:09
                        #13

                        Hi
                        I assume TW is tableWidget ?
                        Please read this. it shows how its expected to be used.
                        https://wiki.qt.io/How_to_Use_QTableWidget

                        You might need to tell it how many to expect/have
                        m_pTableWidget->setRowCount(10);
                        m_pTableWidget->setColumnCount(3);

                        normally to add one new row, you can do
                        tableWidget->insertRow( tableWidget->rowCount() );

                        The -1 is most likely
                        qDebug() << ui->tw_recipes->currentRow();
                        which means None selected. (since nothing been clicked on)

                        also
                        ++r; // is 1
                        ui->tw_recipes->insertRow(r); // ask it to add row at 1 but it dont have row zero so it cant.

                        ? 1 Reply Last reply 11 Aug 2018, 18:41
                        0
                        • M mrjj
                          11 Aug 2018, 17:06

                          Hi
                          I assume TW is tableWidget ?
                          Please read this. it shows how its expected to be used.
                          https://wiki.qt.io/How_to_Use_QTableWidget

                          You might need to tell it how many to expect/have
                          m_pTableWidget->setRowCount(10);
                          m_pTableWidget->setColumnCount(3);

                          normally to add one new row, you can do
                          tableWidget->insertRow( tableWidget->rowCount() );

                          The -1 is most likely
                          qDebug() << ui->tw_recipes->currentRow();
                          which means None selected. (since nothing been clicked on)

                          also
                          ++r; // is 1
                          ui->tw_recipes->insertRow(r); // ask it to add row at 1 but it dont have row zero so it cant.

                          ? Offline
                          ? Offline
                          A Former User
                          wrote on 11 Aug 2018, 18:41 last edited by A Former User 8 Nov 2018, 18:42
                          #14

                          @mrjj - Finally got it! Thanks for all your help.

                          void MainWindow::FillTable()
                          {
                              loading = true;
                              QSqlQuery q(db);
                          
                              int r = 0, c = 0;
                              if (!q.exec("SELECT COUNT(*) FROM recipes")) db.lastError().text();
                              q.first();
                              int rCount = q.value(0).toInt();
                          
                              if (!q.exec("SELECT recipe_id, name FROM recipes "
                                          "ORDER BY name")) db.lastError().text();
                              while (q.next())
                              {
                                  ui->tw_recipes->setRowCount(rCount);
                                  for (c = 0; c < 2; ++c)
                                  {
                                      ui->tw_recipes->setItem(r, c, new QTableWidgetItem(q.value(c).toString()));
                                  }
                                  ++r;
                              }
                              loading = false;
                          }
                          
                          J 1 Reply Last reply 11 Aug 2018, 19:22
                          1
                          • M Offline
                            M Offline
                            mrjj
                            Lifetime Qt Champion
                            wrote on 11 Aug 2018, 19:02 last edited by
                            #15

                            Hi
                            super.
                            One note.
                            should it not be
                            int rCount = q.value(0).toInt();
                            ui->tw_recipes->setRowCount(rCount);

                            instead of inside the while loop ?

                            ? 1 Reply Last reply 11 Aug 2018, 19:45
                            0
                            • ? A Former User
                              11 Aug 2018, 18:41

                              @mrjj - Finally got it! Thanks for all your help.

                              void MainWindow::FillTable()
                              {
                                  loading = true;
                                  QSqlQuery q(db);
                              
                                  int r = 0, c = 0;
                                  if (!q.exec("SELECT COUNT(*) FROM recipes")) db.lastError().text();
                                  q.first();
                                  int rCount = q.value(0).toInt();
                              
                                  if (!q.exec("SELECT recipe_id, name FROM recipes "
                                              "ORDER BY name")) db.lastError().text();
                                  while (q.next())
                                  {
                                      ui->tw_recipes->setRowCount(rCount);
                                      for (c = 0; c < 2; ++c)
                                      {
                                          ui->tw_recipes->setItem(r, c, new QTableWidgetItem(q.value(c).toString()));
                                      }
                                      ++r;
                                  }
                                  loading = false;
                              }
                              
                              J Offline
                              J Offline
                              JonB
                              wrote on 11 Aug 2018, 19:22 last edited by JonB 8 Nov 2018, 19:32
                              #16

                              @landslyde
                              Now be brave and remove the whole SELECT COUNT(*) and the setRowCount(), do it with insertRow() instead. Do it now! You know it makes sense ;-)

                              Separately, your db.lastError().text(); just returns the text, you won't see anything when your exec()s fail. You will want more like:

                              qDebug << db.lastError().text();
                              return ...;
                              
                              ? 1 Reply Last reply 11 Aug 2018, 19:40
                              0
                              • J JonB
                                11 Aug 2018, 19:22

                                @landslyde
                                Now be brave and remove the whole SELECT COUNT(*) and the setRowCount(), do it with insertRow() instead. Do it now! You know it makes sense ;-)

                                Separately, your db.lastError().text(); just returns the text, you won't see anything when your exec()s fail. You will want more like:

                                qDebug << db.lastError().text();
                                return ...;
                                
                                ? Offline
                                ? Offline
                                A Former User
                                wrote on 11 Aug 2018, 19:40 last edited by A Former User 8 Nov 2018, 20:38
                                #17

                                @JonB - I cldnt make inserRow() work. I'll mess around with it, but not just yet. Yes, it makes more sense doing it that way. I had the variable r stuck in it: insertRow(r), which seems right to me. But it was DOA each time I ran it. I'll work with it though. And I appreciate all of your help, including the addition that came in while I was responding here, regarding using qDebug for lastError(). Thanks.

                                UPDATE
                                Works like a charm! =)

                                void MainWindow::FillTable()
                                {
                                    loading = true;
                                    QSqlQuery q(db);
                                
                                    int r = 0, c = 0;
                                
                                    if (!q.exec("SELECT recipe_id, name FROM recipes "
                                                "ORDER BY name")) 
                                        qDebug() << db.lastError().text();
                                    while (q.next())
                                    {
                                        ui->tw_recipes->insertRow(r);
                                        for (c = 0; c < 2; ++c)
                                        {
                                            ui->tw_recipes->setItem(r, c, new QTableWidgetItem(q.value(c).toString()));
                                        }
                                        ++r;
                                    }
                                    loading = false;
                                }
                                
                                1 Reply Last reply
                                0
                                • M mrjj
                                  11 Aug 2018, 19:02

                                  Hi
                                  super.
                                  One note.
                                  should it not be
                                  int rCount = q.value(0).toInt();
                                  ui->tw_recipes->setRowCount(rCount);

                                  instead of inside the while loop ?

                                  ? Offline
                                  ? Offline
                                  A Former User
                                  wrote on 11 Aug 2018, 19:45 last edited by
                                  #18

                                  @mrjj - Both ways work. But one looks cleaner than the other: yours =) Anyway, the ribs are ready to be pulled out of the smoker, and that means I'm out of here! =)

                                  Thank you for all your help. I'll get there...in time.

                                  1 Reply Last reply
                                  0

                                  12/18

                                  11 Aug 2018, 16:04

                                  • Login

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