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 Aioria

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

    jsulmJ Offline
    jsulmJ Offline
    jsulm
    Lifetime Qt Champion
    wrote on 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 last edited by
      #16

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

      1 Reply Last reply
      3
      • A Aioria

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

        JonBJ Offline
        JonBJ Offline
        JonB
        wrote on 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
        2
        • VRoninV Offline
          VRoninV Offline
          VRonin
          wrote on 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
          2
          • 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
            #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

              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