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. update qsqlite and qtableview

update qsqlite and qtableview

Scheduled Pinned Locked Moved Solved General and Desktop
17 Posts 4 Posters 2.2k Views 2 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.
  • M Offline
    M Offline
    Mogli123
    wrote on last edited by Mogli123
    #1

    Hi,

    I'm creating a database (qsqlite) and i want to show the data with a qtableview.
    creating the database and to show them via qtableview works so far, but at the point to update it I have problems.
    It does not work like I want. The new data are at the wrong place or some data are missing.
    The sice of the database remains constant, only the data changes incomming from a tcpsocket.
    I hope someone can help me
    If I have some bad code please tell me.
    I'm a beginner and want to learn how to do it corectly.

    creating the database

     db = QSqlDatabase::addDatabase("QSQLITE");
        db.setDatabaseName(":memory:");
        if (!db.open())
        {
            QMessageBox::critical(nullptr, QObject::tr("Cannot open database"),
                QObject::tr("Unable to establish a database connection.\n"
                            "This example needs SQLite support. Please read "
                            "the Qt SQL driver documentation for information how "
                            "to build it.\n\n"
                            "Click Cancel to exit."), QMessageBox::Cancel);
            return;
        }
    
     queryToolChangeWarnings = new QSqlQuery;
        queryToolChangeWarnings->exec("CREATE TABLE ToolChangeWarnings (COLOR varchar(20),"
                    "TOOLNR int, TIME int, POCKETSTATUS int)");
        queryToolChangeWarnings->prepare("INSERT INTO ToolChangeWarnings (COLOR, TOOLNR, TIME, POCKETSTATUS) "
                       "VALUES (:COLOR, :TOOLNR, :TIME, :POCKETSTATUS)");
        queryToolChangeWarnings->bindValue(":COLOR", "");
    
        this->close();
    
        myTableToolChangeWarnings = new TableToolChangeWarnings(this);
        myTableToolChangeWarnings->move(spaceBetweenTables, spaceBetweenTables);
        myTableToolChangeWarnings->setMinimumHeight(tableHeight);
        myTableToolChangeWarnings->setMinimumWidth(tableWidth);
        myTableToolChangeWarnings->show();
    
    
        queryManipulationToolChangeWarnings = new QSqlTableModel(this);
        queryManipulationToolChangeWarnings->setTable("ToolChangeWarnings");
        queryManipulationToolChangeWarnings->select();
        queryManipulationToolChangeWarnings->setEditStrategy(QSqlTableModel::OnFieldChange);
    

    the new data for the database

    void detailScreen::receiveVectorTool_W(QVector<int> vector)
    {
        if (vector.length() == 30)
        {
            for(qint8 i = 0; i<30; i++)
            {
                arrayToolChangeWarnings[i] = vector[i];
            }
            if (firstFill == false)
            {
        
                firstFillDatabaseToolChangeWarnings();
                emit fillTableNewToolChangeWarnings();
        
                firstFill = true;
            }
            else
            {
                emit fillTableNewToolChangeWarnings();
            }
    
        }
    }
    

    first fill of the database

    void detailScreen::firstFillDatabaseToolChangeWarnings()
    {
        for (int i = 0; i < 10; i++)
        {
            queryToolChangeWarnings->bindValue(":TOOLNR", arrayToolChangeWarnings[i * 3 + 0]);
            queryToolChangeWarnings->bindValue(":TIME", arrayToolChangeWarnings[i * 3 + 1]);
            queryToolChangeWarnings->bindValue(":POCKETSTATUS", arrayToolChangeWarnings[i * 3 + 2]);
            queryToolChangeWarnings->exec();
        }
    }
    

    update the data

    void detailScreen::fillDatabaseToolChangeWarnings()
    {
    
        qint8 i = 0;
        for (qint8 indexRow = 0; indexRow < 10; indexRow ++)
        {
    
            for (qint8 indexColumn = 1; indexColumn < 4; indexColumn ++)
            {
                queryManipulationToolChangeWarnings->setData(queryManipulationToolChangeWarnings->
                index(indexRow,indexColumn), arrayToolChangeWarnings[i]);
                queryManipulationToolChangeWarnings->submit();
                i++;
            }
    
        }
    
        emit refreshTableToolChangeWarnings();
    }
    

    in my table clase I try to update the data via signal & slot

    void TableToolChangeWarnings::updateTable()
    {
        myModelToolChangeWarnings->setQuery("SELECT * FROM ToolChangeWarnings");
    };
    

    If you need more informations please ask

    Thank you in advance
    regards

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

      Hi,

      @Mogli123 said in update qsqlite and qtableview:

      myModelToolChangeWarnings

      What is that class ?

      Can you explain more precisely what you mean by "at the wrong place" ?

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

      1 Reply Last reply
      0
      • M Offline
        M Offline
        Mogli123
        wrote on last edited by Mogli123
        #3

        Hi, thank you for answering
        myModelToolChangeWarnings inherits from QSqlQueryModel

        SGaist:"Can you explain more precisely what you mean by "at the wrong place" ?"
        To say: "they are at the wrong place" is wrong. Sorry for that.
        I think I'm overiting the old data wrong in the database.
        To write the data the first time to the database works but updating doesn't work.
        I don't know how exactly to do that.

        Is that the write way to update the database or is there a better solution?

        jsulmJ 1 Reply Last reply
        0
        • M Mogli123

          Hi, thank you for answering
          myModelToolChangeWarnings inherits from QSqlQueryModel

          SGaist:"Can you explain more precisely what you mean by "at the wrong place" ?"
          To say: "they are at the wrong place" is wrong. Sorry for that.
          I think I'm overiting the old data wrong in the database.
          To write the data the first time to the database works but updating doesn't work.
          I don't know how exactly to do that.

          Is that the write way to update the database or is there a better solution?

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

          @Mogli123 To update existing data you use an UPDATE SQL query. How does your UPDATE query look like?

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

          M 1 Reply Last reply
          0
          • jsulmJ jsulm

            @Mogli123 To update existing data you use an UPDATE SQL query. How does your UPDATE query look like?

            M Offline
            M Offline
            Mogli123
            wrote on last edited by
            #5

            @jsulm
            I tried it like this way

            void detailScreen::fillDatabaseToolChangeWarnings()
            {
                qint8 i = 0;
                for (qint8 indexRow = 0; indexRow < 10; indexRow ++)
                {
            
                    for (qint8 indexColumn = 1; indexColumn < 4; indexColumn ++)
                    {
                        queryManipulationToolChangeWarnings->setData(queryManipulationToolChangeWarnings->
                        index(indexRow,indexColumn), arrayToolChangeWarnings[i]);
                        queryManipulationToolChangeWarnings->submit();
                        i++;
                    }
            
                }
            
                emit refreshTableToolChangeWarnings();
            }
            

            but it seems to be wrong

            Yesterday i tried it like this

            void detailScreen::fillDatabaseToolChangeWarnings()
            {
            
             queryToolChangeWarnings->prepare("UPDATE ToolChangeWarnings (COLOR, TOOLNR, TIME, POCKETSTATUS) "
                                                    "VALUES (:COLOR, :TOOLNR, :TIME, :POCKETSTATUS)");
                for (int i = 0; i < 10; i++)
                {
                    queryToolChangeWarnings->bindValue(":TOOLNR", arrayToolChangeWarnings[i * 3 + 0]);
                    queryToolChangeWarnings->bindValue(":TIME", arrayToolChangeWarnings[i * 3 + 1]);
                    queryToolChangeWarnings->bindValue(":POCKETSTATUS", arrayToolChangeWarnings[i * 3 + 2]);
                    queryToolChangeWarnings->exec();
                }
                emit refreshTableToolChangeWarnings();
            }
            

            but it does not work too.

            can you please tell me to do it corectly?

            jsulmJ 1 Reply Last reply
            0
            • M Mogli123

              @jsulm
              I tried it like this way

              void detailScreen::fillDatabaseToolChangeWarnings()
              {
                  qint8 i = 0;
                  for (qint8 indexRow = 0; indexRow < 10; indexRow ++)
                  {
              
                      for (qint8 indexColumn = 1; indexColumn < 4; indexColumn ++)
                      {
                          queryManipulationToolChangeWarnings->setData(queryManipulationToolChangeWarnings->
                          index(indexRow,indexColumn), arrayToolChangeWarnings[i]);
                          queryManipulationToolChangeWarnings->submit();
                          i++;
                      }
              
                  }
              
                  emit refreshTableToolChangeWarnings();
              }
              

              but it seems to be wrong

              Yesterday i tried it like this

              void detailScreen::fillDatabaseToolChangeWarnings()
              {
              
               queryToolChangeWarnings->prepare("UPDATE ToolChangeWarnings (COLOR, TOOLNR, TIME, POCKETSTATUS) "
                                                      "VALUES (:COLOR, :TOOLNR, :TIME, :POCKETSTATUS)");
                  for (int i = 0; i < 10; i++)
                  {
                      queryToolChangeWarnings->bindValue(":TOOLNR", arrayToolChangeWarnings[i * 3 + 0]);
                      queryToolChangeWarnings->bindValue(":TIME", arrayToolChangeWarnings[i * 3 + 1]);
                      queryToolChangeWarnings->bindValue(":POCKETSTATUS", arrayToolChangeWarnings[i * 3 + 2]);
                      queryToolChangeWarnings->exec();
                  }
                  emit refreshTableToolChangeWarnings();
              }
              

              but it does not work too.

              can you please tell me to do it corectly?

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

              @Mogli123 There is no WHERE in your query, so you're updating everything. If you, for example, want to update a data record with a specific ID you can do it like this:

              UPDATE ToolChangeWarnings (COLOR, TOOLNR, TIME, POCKETSTATUS) "
                                                      "VALUES (:COLOR, :TOOLNR, :TIME, :POCKETSTATUS)"
                                                      "WHERE ID = :ID"
              

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

              M 1 Reply Last reply
              1
              • jsulmJ jsulm

                @Mogli123 There is no WHERE in your query, so you're updating everything. If you, for example, want to update a data record with a specific ID you can do it like this:

                UPDATE ToolChangeWarnings (COLOR, TOOLNR, TIME, POCKETSTATUS) "
                                                        "VALUES (:COLOR, :TOOLNR, :TIME, :POCKETSTATUS)"
                                                        "WHERE ID = :ID"
                
                M Offline
                M Offline
                Mogli123
                wrote on last edited by
                #7

                @jsulm
                to updating everything it's ok, because the old data are not necessary after updating

                jsulmJ 1 Reply Last reply
                0
                • M Mogli123

                  @jsulm
                  to updating everything it's ok, because the old data are not necessary after updating

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

                  @Mogli123 But then I don't understand what the problem is.
                  Or maybe you misunderstood what I wrote? Without WHERE you're updating all data records in the table. So, if you have 10 records in that table all 10 will be updated.

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

                  M 1 Reply Last reply
                  0
                  • jsulmJ jsulm

                    @Mogli123 But then I don't understand what the problem is.
                    Or maybe you misunderstood what I wrote? Without WHERE you're updating all data records in the table. So, if you have 10 records in that table all 10 will be updated.

                    M Offline
                    M Offline
                    Mogli123
                    wrote on last edited by Mogli123
                    #9

                    @jsulm
                    Yes that is that wat I want because I don't know which data had changed therefore I want to override all data.
                    Or is this a bad solution?
                    My problem is that it does not work and I don't know why.
                    after writing the data at first time it works fine and the qtableview is displaying them.
                    If I try to update them the qtableview does nothing
                    Furthermore I don't know if updating the database works because the qtableview shows no new data

                    M 1 Reply Last reply
                    0
                    • M Mogli123

                      @jsulm
                      Yes that is that wat I want because I don't know which data had changed therefore I want to override all data.
                      Or is this a bad solution?
                      My problem is that it does not work and I don't know why.
                      after writing the data at first time it works fine and the qtableview is displaying them.
                      If I try to update them the qtableview does nothing
                      Furthermore I don't know if updating the database works because the qtableview shows no new data

                      M Offline
                      M Offline
                      Mogli123
                      wrote on last edited by
                      #10
                      This post is deleted!
                      1 Reply Last reply
                      0
                      • M Offline
                        M Offline
                        Mogli123
                        wrote on last edited by
                        #11

                        if I try something like this

                                queryToolChangeWarnings->prepare("UPDATE ToolChangeWarnings SET TOOLNR = :TOOLNR");
                                queryToolChangeWarnings->bindValue(":TOOLNR", arrayToolChangeWarnings[0]);
                        

                        The updating is working but complete column TOOLNR geht the int value from the arrayToolChangeWarnings[0]

                        How can I only update for example the first value from the column not the complete column
                        and so on

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

                          Because your query asks exactly for that. Use the WHERE keyword and state precisely where you want the update to happen.

                          See the documentation which also contain a big warning about missing the WHERE part.

                          [edit: added link to documentation SGaist]

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

                          M 1 Reply Last reply
                          2
                          • SGaistS SGaist

                            Because your query asks exactly for that. Use the WHERE keyword and state precisely where you want the update to happen.

                            See the documentation which also contain a big warning about missing the WHERE part.

                            [edit: added link to documentation SGaist]

                            M Offline
                            M Offline
                            Mogli123
                            wrote on last edited by Mogli123
                            #13

                            @SGaist
                            Sorry your right
                            If I try it lik this it works

                             queryToolChangeWarnings->prepare("UPDATE ToolChangeWarnings SET TOOLNR = :TOOLNR WHERE ID = 0");
                             queryToolChangeWarnings->bindValue(":TOOLNR", arrayToolChangeWarnings[0]);
                             queryToolChangeWarnings->exec();
                            

                            It looks like that it is very impractical to do this for all cells
                            Do you know a solution to do this in a more efficient way

                            JonBJ 1 Reply Last reply
                            0
                            • SGaistS Offline
                              SGaistS Offline
                              SGaist
                              Lifetime Qt Champion
                              wrote on last edited by
                              #14

                              Then use another bind value for ID and pass it also to your query. You can do that in a loop.

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

                              1 Reply Last reply
                              2
                              • M Mogli123

                                @SGaist
                                Sorry your right
                                If I try it lik this it works

                                 queryToolChangeWarnings->prepare("UPDATE ToolChangeWarnings SET TOOLNR = :TOOLNR WHERE ID = 0");
                                 queryToolChangeWarnings->bindValue(":TOOLNR", arrayToolChangeWarnings[0]);
                                 queryToolChangeWarnings->exec();
                                

                                It looks like that it is very impractical to do this for all cells
                                Do you know a solution to do this in a more efficient way

                                JonBJ Offline
                                JonBJ Offline
                                JonB
                                wrote on last edited by JonB
                                #15

                                @Mogli123
                                Provided I understand you right:

                                It looks like that it is very impractical to do this for all cells

                                I believe you're saying you have some array of data (from a TCP socket). You want to update the database to change the TOOLNR value of a bunch of rows to a variety of values in your input data,

                                It cannot be helped that this requires a distinct SQL statement to be sent to the database for each input value (array element). If you have a 100 rows of data you will be executing 100 SQL statements. That's how it is.

                                At the code side, as @SGaist says you do not have to write 100 lines of code, one for each row. You use a loop to produce the statements, like:

                                for (int i = 0; i < arrayToolChangeWarnings.size(); i++
                                {
                                    queryToolChangeWarnings->prepare("UPDATE ToolChangeWarnings SET TOOLNR = :TOOLNR WHERE ID = " + QString::number(i));
                                    queryToolChangeWarnings->bindValue(":TOOLNR", arrayToolChangeWarnings[i]);
                                    queryToolChangeWarnings->exec();
                                }
                                

                                There may be a way of sending the multiple statements as a single batch efficiently, I don't know (but then you'd have to deal with the bindings somehow).

                                M 1 Reply Last reply
                                1
                                • JonBJ JonB

                                  @Mogli123
                                  Provided I understand you right:

                                  It looks like that it is very impractical to do this for all cells

                                  I believe you're saying you have some array of data (from a TCP socket). You want to update the database to change the TOOLNR value of a bunch of rows to a variety of values in your input data,

                                  It cannot be helped that this requires a distinct SQL statement to be sent to the database for each input value (array element). If you have a 100 rows of data you will be executing 100 SQL statements. That's how it is.

                                  At the code side, as @SGaist says you do not have to write 100 lines of code, one for each row. You use a loop to produce the statements, like:

                                  for (int i = 0; i < arrayToolChangeWarnings.size(); i++
                                  {
                                      queryToolChangeWarnings->prepare("UPDATE ToolChangeWarnings SET TOOLNR = :TOOLNR WHERE ID = " + QString::number(i));
                                      queryToolChangeWarnings->bindValue(":TOOLNR", arrayToolChangeWarnings[i]);
                                      queryToolChangeWarnings->exec();
                                  }
                                  

                                  There may be a way of sending the multiple statements as a single batch efficiently, I don't know (but then you'd have to deal with the bindings somehow).

                                  M Offline
                                  M Offline
                                  Mogli123
                                  wrote on last edited by
                                  #16

                                  @JonB
                                  @SGaist
                                  @jsulm

                                  Thank you very much that's exactly what I want

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

                                    Great !

                                    Then please mark the thread as solved using the "Topic Tools" button so that other forum uses may know a solution has been found :)

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

                                    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