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 change Row Value
Forum Updated to NodeBB v4.3 + New Features

Sqlite change Row Value

Scheduled Pinned Locked Moved Unsolved General and Desktop
31 Posts 7 Posters 6.4k Views 3 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.
  • VRoninV VRonin

    Quick question: how do you add the checkboxes to ui->listWidget?

    A Offline
    A Offline
    Aioria
    wrote on last edited by
    #20

    @VRonin @JonB
    Yes, here i add the checkboxes

    int row = ShowData();
        setWindowTitle(QString ("Assistance"));
        QList<QString> items;
        for (int i = 0; i<row;i++)
        {
            items.append("" + QString::number(i));
        }
        QListIterator <QString> itr(items);
        while(itr.hasNext())
        {
            QListWidgetItem *item = new QListWidgetItem(itr.next());
            item->setCheckState(Qt::Unchecked);
            ui->listWidget->addItem(item);
        }
    
    

    Maybe the error ocurred here in the creation of the checkboxes and i need to do it in other function and other way

    1 Reply Last reply
    0
    • JonBJ JonB

      @Aioria

      • If you are saying you are not sure whether your chequedState() is even being hit, you should put in a debug statement to verify you are getting there.

      • You still have not checked the return result of the update.prepare("UPDATE students SET faults = faults + 1 WHERE id = :id"); which you say is not working. [EDIT Sorry, I meant the update.exec() which follows that line.]

      • You can check your statement by typing

      UPDATE students SET faults = faults + 1 WHERE id = 1
      

      into a SQLite command-line/client tool. You can & should get this set up. Once you have done so you can test your proposed SQL statements directly, before you put them into your program. This makes it easier to develop correctly.

      A Offline
      A Offline
      Aioria
      wrote on last edited by Aioria
      #21

      @JonB I test it and if i do this:

      UPDATE students SET faults = faults + 1 WHERE id = id
      

      then increment by 3 all the faults and if i uncheck one, increment all by 1. So now the thing is in the for loop. im trying to change it

      1 Reply Last reply
      0
      • A Offline
        A Offline
        Aioria
        wrote on last edited by
        #22

        @JonB @jsulm
        Can anyone help me with this last thing?

        void MainWindow::chequedState(int i){
        
            if (ui->listWidget->item(i)->checkState() == Qt::Unchecked)
            {
                QSqlQuery update;
                update.bindValue(":id", i);
                update.prepare("UPDATE students SET faults = faults + 1 WHERE id");
                update.exec();
            }
        
        
        }
        /void MainWindow::saveAssistence()
        {
            int rowCount = ui->listWidget->count();
        
            for (int i = 0; i < rowCount; i++)
            {
                chequedState(i);
            }
        
        
        }
        

        I have this 2 functions and increments in 3 instead in 1 each row.

        Thank you!

        jsulmJ 1 Reply Last reply
        0
        • A Aioria

          @JonB @jsulm
          Can anyone help me with this last thing?

          void MainWindow::chequedState(int i){
          
              if (ui->listWidget->item(i)->checkState() == Qt::Unchecked)
              {
                  QSqlQuery update;
                  update.bindValue(":id", i);
                  update.prepare("UPDATE students SET faults = faults + 1 WHERE id");
                  update.exec();
              }
          
          
          }
          /void MainWindow::saveAssistence()
          {
              int rowCount = ui->listWidget->count();
          
              for (int i = 0; i < rowCount; i++)
              {
                  chequedState(i);
              }
          
          
          }
          

          I have this 2 functions and increments in 3 instead in 1 each row.

          Thank you!

          jsulmJ Offline
          jsulmJ Offline
          jsulm
          Lifetime Qt Champion
          wrote on last edited by
          #23

          @Aioria said in Sqlite change Row Value:

          UPDATE students SET faults = faults + 1 WHERE id

          please take a closer look at the WHERE part of your update query...

          https://forum.qt.io/topic/113070/qt-code-of-conduct

          A 1 Reply Last reply
          2
          • jsulmJ jsulm

            @Aioria said in Sqlite change Row Value:

            UPDATE students SET faults = faults + 1 WHERE id

            please take a closer look at the WHERE part of your update query...

            A Offline
            A Offline
            Aioria
            wrote on last edited by
            #24

            @jsulm
            I tried to put =: id but don't do nothing. I think that the problem is in the for. Maybe another way to do it. This increment all the rows in the quantity of rows that the table have...

            jsulmJ 1 Reply Last reply
            0
            • A Aioria

              @jsulm
              I tried to put =: id but don't do nothing. I think that the problem is in the for. Maybe another way to do it. This increment all the rows in the quantity of rows that the table have...

              jsulmJ Offline
              jsulmJ Offline
              jsulm
              Lifetime Qt Champion
              wrote on last edited by
              #25

              @Aioria Are you sure the row number in the list widget is the correct ID in the table?!

              https://forum.qt.io/topic/113070/qt-code-of-conduct

              A 1 Reply Last reply
              0
              • jsulmJ jsulm

                @Aioria Are you sure the row number in the list widget is the correct ID in the table?!

                A Offline
                A Offline
                Aioria
                wrote on last edited by
                #26

                @jsulm I think with the :id, i , i put the value of i in the id.
                How can i check that and how can i do to make an union between the list and the table?

                CreateStudentTable();
                    int row = ShowData();
                    setWindowTitle(QString ("Asistencia"));
                    QList<QString> items;
                    for (int i = 0; i<row;i++)
                    {
                        items.append("" + QString::number(i));
                    }
                    QListIterator <QString> itr(items);
                    while(itr.hasNext())
                    {
                        QListWidgetItem *item = new QListWidgetItem(itr.next());
                        item->setCheckState(Qt::Unchecked);
                        ui->listWidget->addItem(item);
                    }
                
                    ShowData();
                

                This is the code that creates the list with the checkbox

                Pablo J. RoginaP jsulmJ 2 Replies Last reply
                0
                • A Aioria

                  @jsulm I think with the :id, i , i put the value of i in the id.
                  How can i check that and how can i do to make an union between the list and the table?

                  CreateStudentTable();
                      int row = ShowData();
                      setWindowTitle(QString ("Asistencia"));
                      QList<QString> items;
                      for (int i = 0; i<row;i++)
                      {
                          items.append("" + QString::number(i));
                      }
                      QListIterator <QString> itr(items);
                      while(itr.hasNext())
                      {
                          QListWidgetItem *item = new QListWidgetItem(itr.next());
                          item->setCheckState(Qt::Unchecked);
                          ui->listWidget->addItem(item);
                      }
                  
                      ShowData();
                  

                  This is the code that creates the list with the checkbox

                  Pablo J. RoginaP Offline
                  Pablo J. RoginaP Offline
                  Pablo J. Rogina
                  wrote on last edited by
                  #27

                  @Aioria please try to not double post. I know, that post is Spanish but you're trying to solve the same issue :-)

                  Upvote the answer(s) that helped you solve the issue
                  Use "Topic Tools" button to mark your post as Solved
                  Add screenshots via postimage.org
                  Don't ask support requests via chat/PM. Please use the forum so others can benefit from the solution in the future

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

                    @Pablo-J-Rogina it's usually the only case where double posting is allowed since it's in the poster native language and therefor might get an answer that's easier to understand.

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

                    Pablo J. RoginaP 1 Reply Last reply
                    1
                    • SGaistS SGaist

                      @Pablo-J-Rogina it's usually the only case where double posting is allowed since it's in the poster native language and therefor might get an answer that's easier to understand.

                      Pablo J. RoginaP Offline
                      Pablo J. RoginaP Offline
                      Pablo J. Rogina
                      wrote on last edited by
                      #29

                      @SGaist got it, it's learnt since now on forward. Thank you for the clarification.

                      Upvote the answer(s) that helped you solve the issue
                      Use "Topic Tools" button to mark your post as Solved
                      Add screenshots via postimage.org
                      Don't ask support requests via chat/PM. Please use the forum so others can benefit from the solution in the future

                      1 Reply Last reply
                      0
                      • A Aioria

                        @jsulm I think with the :id, i , i put the value of i in the id.
                        How can i check that and how can i do to make an union between the list and the table?

                        CreateStudentTable();
                            int row = ShowData();
                            setWindowTitle(QString ("Asistencia"));
                            QList<QString> items;
                            for (int i = 0; i<row;i++)
                            {
                                items.append("" + QString::number(i));
                            }
                            QListIterator <QString> itr(items);
                            while(itr.hasNext())
                            {
                                QListWidgetItem *item = new QListWidgetItem(itr.next());
                                item->setCheckState(Qt::Unchecked);
                                ui->listWidget->addItem(item);
                            }
                        
                            ShowData();
                        

                        This is the code that creates the list with the checkbox

                        jsulmJ Offline
                        jsulmJ Offline
                        jsulm
                        Lifetime Qt Champion
                        wrote on last edited by jsulm
                        #30

                        @Aioria said in Sqlite change Row Value:

                        I think with the :id, i , i put the value of i in the id

                        Yes, you do and this is a bad idea. The id of an item in the database should not be its row number in some widget in your UI! What happens, for example, if you order/sort the items differently? Then suddenly all your IDs are wrong.
                        You can use http://doc.qt.io/qt-5/qlistwidgetitem.html#setData to set the database ID in your items and then get it using http://doc.qt.io/qt-5/qlistwidgetitem.html#data

                        https://forum.qt.io/topic/113070/qt-code-of-conduct

                        A 1 Reply Last reply
                        1
                        • jsulmJ jsulm

                          @Aioria said in Sqlite change Row Value:

                          I think with the :id, i , i put the value of i in the id

                          Yes, you do and this is a bad idea. The id of an item in the database should not be its row number in some widget in your UI! What happens, for example, if you order/sort the items differently? Then suddenly all your IDs are wrong.
                          You can use http://doc.qt.io/qt-5/qlistwidgetitem.html#setData to set the database ID in your items and then get it using http://doc.qt.io/qt-5/qlistwidgetitem.html#data

                          A Offline
                          A Offline
                          Aioria
                          wrote on last edited by
                          #31

                          @jsulm
                          Thank you! I will try to do that instead the bindvalue!

                          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