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.
  • A Offline
    A Offline
    Aioria
    wrote on 23 Oct 2018, 17:32 last edited by Aioria
    #10

    @JonB
    Thank you for the answer. I have another question. I tried that but not made changes. I have to reescribe the entire code about CREATE the table? I do that with QString. Maybe i need to do it with QSqlQuery. Here is the code:

    void MainWindow::CreateStudentTable()
    {
        QString consult;
        consult.append("CREATE TABLE IF NOT EXISTS students("
                        "id INTEGER PRIMARY KEY AUTOINCREMENT,"
                       "name VARCHAR(100),"
                        "lastName VARCHAR(100), "
                        "faults INTEGER NOT NULL"
                       ");");
    
        QSqlQuery create;
        create.prepare(consult);
    
        if(create.exec()){
            qDebug()<<"The table STUDENT exists.";
        }else{
            qDebug()<<"The table STUDENTS doesnt exists";
            qDebug()<<"ERROR! " << create.lastError();
        }
    }
    
    J 1 Reply Last reply 24 Oct 2018, 05:38
    0
    • A Aioria
      23 Oct 2018, 17:32

      @JonB
      Thank you for the answer. I have another question. I tried that but not made changes. I have to reescribe the entire code about CREATE the table? I do that with QString. Maybe i need to do it with QSqlQuery. Here is the code:

      void MainWindow::CreateStudentTable()
      {
          QString consult;
          consult.append("CREATE TABLE IF NOT EXISTS students("
                          "id INTEGER PRIMARY KEY AUTOINCREMENT,"
                         "name VARCHAR(100),"
                          "lastName VARCHAR(100), "
                          "faults INTEGER NOT NULL"
                         ");");
      
          QSqlQuery create;
          create.prepare(consult);
      
          if(create.exec()){
              qDebug()<<"The table STUDENT exists.";
          }else{
              qDebug()<<"The table STUDENTS doesnt exists";
              qDebug()<<"ERROR! " << create.lastError();
          }
      }
      
      J Offline
      J Offline
      jsulm
      Lifetime Qt Champion
      wrote on 24 Oct 2018, 05:38 last edited by
      #11

      @Aioria said in Sqlite change Row Value:

      I tried that but not made changes

      What do you mean? Was the table not created? What does create.lastError() return?

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

      A 1 Reply Last reply 24 Oct 2018, 13:25
      1
      • J jsulm
        24 Oct 2018, 05:38

        @Aioria said in Sqlite change Row Value:

        I tried that but not made changes

        What do you mean? Was the table not created? What does create.lastError() return?

        A Offline
        A Offline
        Aioria
        wrote on 24 Oct 2018, 13:25 last edited by
        #12

        @jsulm

        I mean that i do this

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

        And when i want to save the state, don't increment the faults. I think that the problem is in ID and Faults but i can't make it work

        J 1 Reply Last reply 24 Oct 2018, 13:32
        0
        • A Aioria
          24 Oct 2018, 13:25

          @jsulm

          I mean that i do this

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

          And when i want to save the state, don't increment the faults. I think that the problem is in ID and Faults but i can't make it work

          J Offline
          J Offline
          JonB
          wrote on 24 Oct 2018, 13:32 last edited by
          #13

          @Aioria
          In principle this code looks correct/reasonable. Don't understand what you say is the problem? Does the above not increment correctly. You should check the return result of exec() at least in case there is an error? Print out the value of i and make sure that is the desired id in the table. Otherwise do you have anything like SQL Workbench for SQLite where you can test these statements?

          A 1 Reply Last reply 25 Oct 2018, 00:36
          4
          • J JonB
            24 Oct 2018, 13:32

            @Aioria
            In principle this code looks correct/reasonable. Don't understand what you say is the problem? Does the above not increment correctly. You should check the return result of exec() at least in case there is an error? Print out the value of i and make sure that is the desired id in the table. Otherwise do you have anything like SQL Workbench for SQLite where you can test these statements?

            A Offline
            A Offline
            Aioria
            wrote on 25 Oct 2018, 00:36 last edited by
            #14

            @JonB
            Yes, i don't understand why. I do the test and there is no error. I can copy the code and see if can get the error. I don't have Workbench, you say that if i try the statements in the workbench maybe can see the error?

            Here is the code:

            MainWindow::MainWindow(QWidget *parent) :
                QMainWindow(parent),
                ui(new Ui::MainWindow)
            {
                ui->setupUi(this);
            
            
                qDebug()<<"init application...";
            
                QString name;
                name.append("Datbase1.sqlite");
            
                db = QSqlDatabase::addDatabase("QSQLITE");
                db.setDatabaseName(name);
            
                if (!QSqlDatabase::isDriverAvailable("QSQLITE")){
                    qDebug()<< "Error: QSQLITE is not available";
                }
            
                if(db.open()) {
                    qDebug()<<"Successfull Conected .";
                }else{
                        qDebug()<<"ERROR! NOT connected to the database.";
                    }
            
                CreateStudentTable();
                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);
                }
            
                ShowData();
            }
            
            
            void MainWindow::insertStudent()
            {
            
                QSqlQuery consult;
                consult.prepare("INSERT INTO students("
                                "name,"
                                 "lastName, "
                                 "faults)"
                                 "VALUES("
                                  "'"+ui->lineEditName->text()+"',"
                                  "'"+ui->lineEditLastName->text()+"',"
                                  "'"+ui->lineEditFaults->text()+"'"
                                 ");");
            
            
               if(consult.exec()){
                   qDebug()<<"The Student is added.";
               }else{
                   qDebug()<<"The Student IS NOT added";
                   qDebug()<<"ERROR! " << consult.lastError();
               }
            }
            
            void MainWindow::deleteStudent()
            {
                QString consult;
                consult.append("DELETE FROM students WHERE name= '"+ui->lineEditName->text()+"'and lastName= '"+ui->lineEditLastName->text()+"'");
            
            
                QSqlQuery deleteS;
                deleteS.prepare(consult);
            
                if(deleteS.exec()){
                    qDebug()<<"The student is successfull deleted.";
                }else{
                    qDebug()<<"The student IS NOT successfull deleted";
                    qDebug()<<"ERROR! " << deleteS.lastError();
                }
            
            }
            
            int MainWindow::ShowData()
            {
                QString consult;
                consult.append("SELECT * FROM students");
            
                QSqlQuery show;
                show.prepare(consult);
            
                if(show.exec()){
                    qDebug()<<"The STUDENT has consulted correctly.";
                }else{
                    qDebug()<<"The STUDENT HAS NOT consulted correctly";
                    qDebug()<<"ERROR! " << show.lastError();
                }
            
                int row = 0;
                ui->tableWidgetData->setRowCount(0);
            
            
                while(show.next()){
                    ui->tableWidgetData->insertRow(row);
                    ui->tableWidgetData->setItem(row,0,new QTableWidgetItem (show.value(1).toByteArray().constData()));
                    ui->tableWidgetData->setItem(row,1,new QTableWidgetItem (show.value(2).toByteArray().constData()));
                    ui->tableWidgetData->setItem(row,2,new QTableWidgetItem (show.value(3).toByteArray().constData()));
                    row++;
                }
                ui->tableWidgetData->horizontalHeader()->setStretchLastSection(true);
                return row;
            }
            
            
            void MainWindow::chequedState(){
            
                int rowCount = ui->listWidget->count();
            
                for (int i = 0; i < rowCount; i++)
                {
                    if (ui->listWidget->item(i)->checkState() == Qt::Unchecked)
                    {
                        QSqlQuery update;
                        update.prepare("UPDATE students SET faults = faults + 1 WHERE id = :id");
                        update.bindValue(":id", i);
                        update.exec();
                    }
                }
            
            }
            
            
            void MainWindow::on_pushButtonAddStudent_clicked()
            {
                insertStudent();
                ShowData();
            }
            
            
            
            void MainWindow::on_pushButtonDeleteStudent_clicked()
            {
                deleteStudent();
                ShowData();
            }
            
            
            
            void MainWindow::on_pushButton_clicked()
            {
            
                chequedState();
                ShowData();
            }
            
            

            Maybe the error is in the Buttons or in the list of the check boxes that are separate from the table.

            J J 2 Replies Last reply 25 Oct 2018, 04:59
            0
            • A Aioria
              25 Oct 2018, 00:36

              @JonB
              Yes, i don't understand why. I do the test and there is no error. I can copy the code and see if can get the error. I don't have Workbench, you say that if i try the statements in the workbench maybe can see the error?

              Here is the code:

              MainWindow::MainWindow(QWidget *parent) :
                  QMainWindow(parent),
                  ui(new Ui::MainWindow)
              {
                  ui->setupUi(this);
              
              
                  qDebug()<<"init application...";
              
                  QString name;
                  name.append("Datbase1.sqlite");
              
                  db = QSqlDatabase::addDatabase("QSQLITE");
                  db.setDatabaseName(name);
              
                  if (!QSqlDatabase::isDriverAvailable("QSQLITE")){
                      qDebug()<< "Error: QSQLITE is not available";
                  }
              
                  if(db.open()) {
                      qDebug()<<"Successfull Conected .";
                  }else{
                          qDebug()<<"ERROR! NOT connected to the database.";
                      }
              
                  CreateStudentTable();
                  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);
                  }
              
                  ShowData();
              }
              
              
              void MainWindow::insertStudent()
              {
              
                  QSqlQuery consult;
                  consult.prepare("INSERT INTO students("
                                  "name,"
                                   "lastName, "
                                   "faults)"
                                   "VALUES("
                                    "'"+ui->lineEditName->text()+"',"
                                    "'"+ui->lineEditLastName->text()+"',"
                                    "'"+ui->lineEditFaults->text()+"'"
                                   ");");
              
              
                 if(consult.exec()){
                     qDebug()<<"The Student is added.";
                 }else{
                     qDebug()<<"The Student IS NOT added";
                     qDebug()<<"ERROR! " << consult.lastError();
                 }
              }
              
              void MainWindow::deleteStudent()
              {
                  QString consult;
                  consult.append("DELETE FROM students WHERE name= '"+ui->lineEditName->text()+"'and lastName= '"+ui->lineEditLastName->text()+"'");
              
              
                  QSqlQuery deleteS;
                  deleteS.prepare(consult);
              
                  if(deleteS.exec()){
                      qDebug()<<"The student is successfull deleted.";
                  }else{
                      qDebug()<<"The student IS NOT successfull deleted";
                      qDebug()<<"ERROR! " << deleteS.lastError();
                  }
              
              }
              
              int MainWindow::ShowData()
              {
                  QString consult;
                  consult.append("SELECT * FROM students");
              
                  QSqlQuery show;
                  show.prepare(consult);
              
                  if(show.exec()){
                      qDebug()<<"The STUDENT has consulted correctly.";
                  }else{
                      qDebug()<<"The STUDENT HAS NOT consulted correctly";
                      qDebug()<<"ERROR! " << show.lastError();
                  }
              
                  int row = 0;
                  ui->tableWidgetData->setRowCount(0);
              
              
                  while(show.next()){
                      ui->tableWidgetData->insertRow(row);
                      ui->tableWidgetData->setItem(row,0,new QTableWidgetItem (show.value(1).toByteArray().constData()));
                      ui->tableWidgetData->setItem(row,1,new QTableWidgetItem (show.value(2).toByteArray().constData()));
                      ui->tableWidgetData->setItem(row,2,new QTableWidgetItem (show.value(3).toByteArray().constData()));
                      row++;
                  }
                  ui->tableWidgetData->horizontalHeader()->setStretchLastSection(true);
                  return row;
              }
              
              
              void MainWindow::chequedState(){
              
                  int rowCount = ui->listWidget->count();
              
                  for (int i = 0; i < rowCount; i++)
                  {
                      if (ui->listWidget->item(i)->checkState() == Qt::Unchecked)
                      {
                          QSqlQuery update;
                          update.prepare("UPDATE students SET faults = faults + 1 WHERE id = :id");
                          update.bindValue(":id", i);
                          update.exec();
                      }
                  }
              
              }
              
              
              void MainWindow::on_pushButtonAddStudent_clicked()
              {
                  insertStudent();
                  ShowData();
              }
              
              
              
              void MainWindow::on_pushButtonDeleteStudent_clicked()
              {
                  deleteStudent();
                  ShowData();
              }
              
              
              
              void MainWindow::on_pushButton_clicked()
              {
              
                  chequedState();
                  ShowData();
              }
              
              

              Maybe the error is in the Buttons or in the list of the check boxes that are separate from the table.

              J Offline
              J Offline
              jsulm
              Lifetime Qt Champion
              wrote on 25 Oct 2018, 04:59 last edited by
              #15

              @Aioria said in Sqlite change Row Value:

              I don't have Workbench

              You can simply use the command line tool delivered together with SQLite.

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

              1 Reply Last reply
              3
              • mrjjM Offline
                mrjjM Offline
                mrjj
                Lifetime Qt Champion
                wrote on 25 Oct 2018, 06:45 last edited by
                #16

                Hi
                There is also
                https://sqlitebrowser.org/

                1 Reply Last reply
                3
                • A Aioria
                  25 Oct 2018, 00:36

                  @JonB
                  Yes, i don't understand why. I do the test and there is no error. I can copy the code and see if can get the error. I don't have Workbench, you say that if i try the statements in the workbench maybe can see the error?

                  Here is the code:

                  MainWindow::MainWindow(QWidget *parent) :
                      QMainWindow(parent),
                      ui(new Ui::MainWindow)
                  {
                      ui->setupUi(this);
                  
                  
                      qDebug()<<"init application...";
                  
                      QString name;
                      name.append("Datbase1.sqlite");
                  
                      db = QSqlDatabase::addDatabase("QSQLITE");
                      db.setDatabaseName(name);
                  
                      if (!QSqlDatabase::isDriverAvailable("QSQLITE")){
                          qDebug()<< "Error: QSQLITE is not available";
                      }
                  
                      if(db.open()) {
                          qDebug()<<"Successfull Conected .";
                      }else{
                              qDebug()<<"ERROR! NOT connected to the database.";
                          }
                  
                      CreateStudentTable();
                      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);
                      }
                  
                      ShowData();
                  }
                  
                  
                  void MainWindow::insertStudent()
                  {
                  
                      QSqlQuery consult;
                      consult.prepare("INSERT INTO students("
                                      "name,"
                                       "lastName, "
                                       "faults)"
                                       "VALUES("
                                        "'"+ui->lineEditName->text()+"',"
                                        "'"+ui->lineEditLastName->text()+"',"
                                        "'"+ui->lineEditFaults->text()+"'"
                                       ");");
                  
                  
                     if(consult.exec()){
                         qDebug()<<"The Student is added.";
                     }else{
                         qDebug()<<"The Student IS NOT added";
                         qDebug()<<"ERROR! " << consult.lastError();
                     }
                  }
                  
                  void MainWindow::deleteStudent()
                  {
                      QString consult;
                      consult.append("DELETE FROM students WHERE name= '"+ui->lineEditName->text()+"'and lastName= '"+ui->lineEditLastName->text()+"'");
                  
                  
                      QSqlQuery deleteS;
                      deleteS.prepare(consult);
                  
                      if(deleteS.exec()){
                          qDebug()<<"The student is successfull deleted.";
                      }else{
                          qDebug()<<"The student IS NOT successfull deleted";
                          qDebug()<<"ERROR! " << deleteS.lastError();
                      }
                  
                  }
                  
                  int MainWindow::ShowData()
                  {
                      QString consult;
                      consult.append("SELECT * FROM students");
                  
                      QSqlQuery show;
                      show.prepare(consult);
                  
                      if(show.exec()){
                          qDebug()<<"The STUDENT has consulted correctly.";
                      }else{
                          qDebug()<<"The STUDENT HAS NOT consulted correctly";
                          qDebug()<<"ERROR! " << show.lastError();
                      }
                  
                      int row = 0;
                      ui->tableWidgetData->setRowCount(0);
                  
                  
                      while(show.next()){
                          ui->tableWidgetData->insertRow(row);
                          ui->tableWidgetData->setItem(row,0,new QTableWidgetItem (show.value(1).toByteArray().constData()));
                          ui->tableWidgetData->setItem(row,1,new QTableWidgetItem (show.value(2).toByteArray().constData()));
                          ui->tableWidgetData->setItem(row,2,new QTableWidgetItem (show.value(3).toByteArray().constData()));
                          row++;
                      }
                      ui->tableWidgetData->horizontalHeader()->setStretchLastSection(true);
                      return row;
                  }
                  
                  
                  void MainWindow::chequedState(){
                  
                      int rowCount = ui->listWidget->count();
                  
                      for (int i = 0; i < rowCount; i++)
                      {
                          if (ui->listWidget->item(i)->checkState() == Qt::Unchecked)
                          {
                              QSqlQuery update;
                              update.prepare("UPDATE students SET faults = faults + 1 WHERE id = :id");
                              update.bindValue(":id", i);
                              update.exec();
                          }
                      }
                  
                  }
                  
                  
                  void MainWindow::on_pushButtonAddStudent_clicked()
                  {
                      insertStudent();
                      ShowData();
                  }
                  
                  
                  
                  void MainWindow::on_pushButtonDeleteStudent_clicked()
                  {
                      deleteStudent();
                      ShowData();
                  }
                  
                  
                  
                  void MainWindow::on_pushButton_clicked()
                  {
                  
                      chequedState();
                      ShowData();
                  }
                  
                  

                  Maybe the error is in the Buttons or in the list of the check boxes that are separate from the table.

                  J Offline
                  J Offline
                  JonB
                  wrote on 25 Oct 2018, 07:06 last edited by JonB
                  #17

                  @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 2 Replies Last reply 25 Oct 2018, 19:04
                  2
                  • VRoninV Offline
                    VRoninV Offline
                    VRonin
                    wrote on 25 Oct 2018, 08:03 last edited by
                    #18

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

                    "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

                    A 1 Reply Last reply 25 Oct 2018, 19:15
                    2
                    • J JonB
                      25 Oct 2018, 07:06

                      @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 25 Oct 2018, 19:04 last edited by
                      #19

                      @JonB
                      Yes i did that and dont throw a error and i delete it from the code. I will try to do that in the page of sqlite

                      1 Reply Last reply
                      0
                      • VRoninV VRonin
                        25 Oct 2018, 08:03

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

                        A Offline
                        A Offline
                        Aioria
                        wrote on 25 Oct 2018, 19:15 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
                        • J JonB
                          25 Oct 2018, 07:06

                          @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 26 Oct 2018, 00:37 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 5 Nov 2018, 21:10 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!

                            J 1 Reply Last reply 6 Nov 2018, 05:56
                            0
                            • A Aioria
                              5 Nov 2018, 21:10

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

                              J Offline
                              J Offline
                              jsulm
                              Lifetime Qt Champion
                              wrote on 6 Nov 2018, 05:56 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 6 Nov 2018, 11:56
                              2
                              • J jsulm
                                6 Nov 2018, 05:56

                                @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 6 Nov 2018, 11:56 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...

                                J 1 Reply Last reply 6 Nov 2018, 12:11
                                0
                                • A Aioria
                                  6 Nov 2018, 11:56

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

                                  J Offline
                                  J Offline
                                  jsulm
                                  Lifetime Qt Champion
                                  wrote on 6 Nov 2018, 12:11 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 7 Nov 2018, 17:57
                                  0
                                  • J jsulm
                                    6 Nov 2018, 12:11

                                    @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 7 Nov 2018, 17:57 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 J 2 Replies Last reply 7 Nov 2018, 18:36
                                    0
                                    • A Aioria
                                      7 Nov 2018, 17:57

                                      @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 7 Nov 2018, 18:36 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 7 Nov 2018, 20:53 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 7 Nov 2018, 20:55
                                        1
                                        • SGaistS SGaist
                                          7 Nov 2018, 20:53

                                          @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 7 Nov 2018, 20:55 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

                                          • Login

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