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

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
  • 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.
  • mrjjM mrjj

    @landslyde
    Red text

    `test`
    

    test

    ? Offline
    ? Offline
    A Former User
    wrote on last edited by
    #6

    @mrjj - Thank you.

    mrjjM 1 Reply Last reply
    0
    • ? A Former User

      @mrjj - Thank you.

      mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on 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
      3
      • ? A Former User

        @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

        JonBJ Offline
        JonBJ Offline
        JonB
        wrote on last edited by JonB
        #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
        1
        • mrjjM mrjj

          @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 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
          • JonBJ JonB

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

            mrjjM 1 Reply Last reply
            1
            • ? A Former User

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

              mrjjM Offline
              mrjjM Offline
              mrjj
              Lifetime Qt Champion
              wrote on 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
              1
              • mrjjM mrjj

                @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 last edited by A Former User
                #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
                • mrjjM Offline
                  mrjjM Offline
                  mrjj
                  Lifetime Qt Champion
                  wrote on last edited by mrjj
                  #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
                  0
                  • mrjjM mrjj

                    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 last edited by A Former User
                    #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;
                    }
                    
                    JonBJ 1 Reply Last reply
                    1
                    • mrjjM Offline
                      mrjjM Offline
                      mrjj
                      Lifetime Qt Champion
                      wrote on 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
                      0
                      • ? A Former User

                        @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;
                        }
                        
                        JonBJ Offline
                        JonBJ Offline
                        JonB
                        wrote on last edited by JonB
                        #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
                        0
                        • JonBJ JonB

                          @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 last edited by A Former User
                          #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
                          • mrjjM mrjj

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

                            • Login

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