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. Updating/Inserting into table using QSqlTableModel
Forum Updated to NodeBB v4.3 + New Features

Updating/Inserting into table using QSqlTableModel

Scheduled Pinned Locked Moved Unsolved General and Desktop
4 Posts 2 Posters 334 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    Abdurahman_Gulamkadirov
    wrote on last edited by Abdurahman_Gulamkadirov
    #1

    Hi there. I'm not familiar with model/view, so I really need your help!
    I'm working with MySql, there's a table called goods. It's fields are name, barcode, amount, cost. In UI, there is a tableView. User can input data(good's name, barcode, amount, cost) to the table via modal window. The data user given should be inserted to the database if there's no good with the same barcode, else it's amount field should be updated to amount+user_input.amount

    I've written simple model that inherits QSqlTableModel, and implemented it's submitAll() method:

    bool SqlTableModelEx::submitAll()
    {
        QSqlQuery query(this->database());
    
        for(int row = 0; row < this->rowCount(); ++row)
        {
            QSqlRecord rec = this->record(row);
            //0 - id, 1 - name, 2 - barcode, 3 - amount, 4 - cost
            query.exec(tr("SELECT * FROM test WHERE barcode=%1").arg(rec.value(2).toString()));
            if(query.size())//if there's a product with the same barcode in the database
            {
                query.next();
                rec.value(3).setValue(rec.value(3).toInt() + query.value(3).toInt());
                rec.setGenerated(3, true);
                this->updateRowInTable(row, rec);
            }
        }
        return this->database().commit();
    }
    

    But no updating. I tested:

    qInfo() << this->updateRowInTable(row, rec);
    

    it showed false. Last error:

    " No Fields to update"
    
    1 Reply Last reply
    0
    • A Offline
      A Offline
      Abdurahman_Gulamkadirov
      wrote on last edited by Abdurahman_Gulamkadirov
      #2

      should I change "dirty" of QModelIndex? If yes, how?

      JonBJ 1 Reply Last reply
      0
      • A Abdurahman_Gulamkadirov

        should I change "dirty" of QModelIndex? If yes, how?

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

        @Abdurahman_Gulamkadirov
        I don't know what you're trying to do/why do it this way. I don't know why you are executing SELECT statements in the middle of submitting.

        The usual way of using a QSqlTableModel is:

        1. Allow it to fill by select()ing all the rows in the table. the rows are all in memory now.

        2. Allow the user to insert or update via your UI, or whatever means. You know whether a given row exists by looking at what you have fetched, so you know whether to issue insertRows()/insertRecord() or just setData()/setRecord() on an existing row.

        3. Call submitAll() when you are ready to commit to the database.

        You should not need to subclass. You should not need to play with updateRowInTable() or look at "dirtiness" etc. unless you have some need and know what you are doing.

        A 1 Reply Last reply
        2
        • JonBJ JonB

          @Abdurahman_Gulamkadirov
          I don't know what you're trying to do/why do it this way. I don't know why you are executing SELECT statements in the middle of submitting.

          The usual way of using a QSqlTableModel is:

          1. Allow it to fill by select()ing all the rows in the table. the rows are all in memory now.

          2. Allow the user to insert or update via your UI, or whatever means. You know whether a given row exists by looking at what you have fetched, so you know whether to issue insertRows()/insertRecord() or just setData()/setRecord() on an existing row.

          3. Call submitAll() when you are ready to commit to the database.

          You should not need to subclass. You should not need to play with updateRowInTable() or look at "dirtiness" etc. unless you have some need and know what you are doing.

          A Offline
          A Offline
          Abdurahman_Gulamkadirov
          wrote on last edited by Abdurahman_Gulamkadirov
          #4

          @JonB I should show just a table to user when importing products to store. So there should be product list that's just imported, not the list of whole products in the store. When the user presses "Import to store" button, If a product in the importing list is not in database, it should be inserted, otherwise it's amount should be updated to it's_amount_in_the_store + it's_amount_in_the_importing_list. I just tried to select product by it's barcode from database, using SELECT statement. Then I know if the product exists in db

          Sorry for my english, I couldn't describe my question clearly

          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