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. How to use setData on inserted row in QSqlTableModel
Forum Updated to NodeBB v4.3 + New Features

How to use setData on inserted row in QSqlTableModel

Scheduled Pinned Locked Moved Unsolved General and Desktop
13 Posts 3 Posters 1.5k 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.
  • I Infinity

    @JonB Thanks

    How can I achieve that the new inserted data re written to the database?

    m_accountModel->setEditStrategy(QSqlTableModel::OnManualSubmit);
    
    m_accountModel->insertRow(0);
    QModelIndex *index = m_accountModel->index(0, 0);
    m_accountModel->setData(index, "Here");
    m_accountModel->submitAll();
    

    This doesn't write the data to the database. What am I doing wrong here?

    JonBJ Online
    JonBJ Online
    JonB
    wrote on last edited by
    #4

    @Infinity
    In principle outline looks fine. My guess: check the return from the setData(), I only gave example, maybe yours does not accept anything/a string in that column, and (most likely) check submitAll(), if the newly inserted row (which here has only one column initialized if at all) is not acceptable for a row in the database it will get rejected.

    1 Reply Last reply
    1
    • JonBJ JonB

      @Infinity

      m_accountModel->insertRow(0);
      QModelIndex *index = m_accountModel->index(0, 0);
      m_accountModel->setData(index, "Here");
      
      I Offline
      I Offline
      Infinity
      wrote on last edited by
      #5

      @JonB I found the reason why submitAll() did not write the data to the database table. It was because I didn't set a value for the primary key in the record. Now it works fine.

      Thank you very much for your help.

      JonBJ 1 Reply Last reply
      0
      • I Infinity

        @JonB I found the reason why submitAll() did not write the data to the database table. It was because I didn't set a value for the primary key in the record. Now it works fine.

        Thank you very much for your help.

        JonBJ Online
        JonBJ Online
        JonB
        wrote on last edited by JonB
        #6

        @Infinity
        Yes, QSqlTableModel will let you insert into it an incomplete row with columns unset or set to bad value. But then when you submit() the database will barf on the INSERT statement, which is what you are seeing. It is a good idea to check/listen for all warning output from the db so that you know what is going on, I subclass/set handlers on all QSql... classes I use for that.

        1 Reply Last reply
        1
        • artwawA Offline
          artwawA Offline
          artwaw
          wrote on last edited by
          #7

          To add to the previous, I practically dropped use of setData() in favour of insertRecord().
          So what I usually do (but my use is never performance oriented):

          QSqlRecord rec=record(); //returns me empty, well formatted record
          //here is filling the record with data
          insertRecord(row,rec); // row is position at which I insert
          

          There is also setRecord() method available. Both took lots of headache from me.

          For more information please re-read.

          Kind Regards,
          Artur

          JonBJ 1 Reply Last reply
          1
          • artwawA artwaw

            To add to the previous, I practically dropped use of setData() in favour of insertRecord().
            So what I usually do (but my use is never performance oriented):

            QSqlRecord rec=record(); //returns me empty, well formatted record
            //here is filling the record with data
            insertRecord(row,rec); // row is position at which I insert
            

            There is also setRecord() method available. Both took lots of headache from me.

            JonBJ Online
            JonBJ Online
            JonB
            wrote on last edited by
            #8

            @artwaw
            +1 for using the QSqlRecord-level stuff. Nicer than setData(row, column)-level.
            But the "empty record" is not necessarily acceptable for database INSERT, you still have to fill in any compulsory columns don't you?

            artwawA 1 Reply Last reply
            0
            • JonBJ JonB

              @artwaw
              +1 for using the QSqlRecord-level stuff. Nicer than setData(row, column)-level.
              But the "empty record" is not necessarily acceptable for database INSERT, you still have to fill in any compulsory columns don't you?

              artwawA Offline
              artwawA Offline
              artwaw
              wrote on last edited by
              #9

              @JonB Depends. In my use cases (again, what works for me doesn't have to work for the others) all I have to take care of are the fields that explicitly can't be null. Indexes are autofilled. As I said, my use cases are quite simple - but then again, QSqlRecord-related stuff has a lot of capabilities I simply don't use.

              For more information please re-read.

              Kind Regards,
              Artur

              JonBJ 1 Reply Last reply
              0
              • artwawA artwaw

                @JonB Depends. In my use cases (again, what works for me doesn't have to work for the others) all I have to take care of are the fields that explicitly can't be null. Indexes are autofilled. As I said, my use cases are quite simple - but then again, QSqlRecord-related stuff has a lot of capabilities I simply don't use.

                JonBJ Online
                JonBJ Online
                JonB
                wrote on last edited by
                #10

                @artwaw said in How to use setData on inserted row in QSqlTableModel:

                all I have to take care of are the fields that explicitly can't be null.

                Yes. But I think (correct me if wrong) that the QSqlRecord-level does not check this for you, it lets you leave and then the INSERT/UPDATE db statement errors. That is what I was referring to.

                artwawA 1 Reply Last reply
                0
                • JonBJ JonB

                  @artwaw said in How to use setData on inserted row in QSqlTableModel:

                  all I have to take care of are the fields that explicitly can't be null.

                  Yes. But I think (correct me if wrong) that the QSqlRecord-level does not check this for you, it lets you leave and then the INSERT/UPDATE db statement errors. That is what I was referring to.

                  artwawA Offline
                  artwawA Offline
                  artwaw
                  wrote on last edited by
                  #11

                  @JonB No, you are correct. As far as I can tell (I probably did a mistake like this in the past just can't recall) attempt to insert a malformed record, be it without necessary fields filled or with a wrong structure will fail. (it is probably also in the docs but can't dive there at this moment to check)

                  For more information please re-read.

                  Kind Regards,
                  Artur

                  JonBJ 1 Reply Last reply
                  1
                  • artwawA artwaw

                    @JonB No, you are correct. As far as I can tell (I probably did a mistake like this in the past just can't recall) attempt to insert a malformed record, be it without necessary fields filled or with a wrong structure will fail. (it is probably also in the docs but can't dive there at this moment to check)

                    JonBJ Online
                    JonBJ Online
                    JonB
                    wrote on last edited by
                    #12

                    @artwaw
                    Yes, IIRC in (my beloved) C# data/database handling classes, it stopped you putting an invalid-type-value into its QRecord internally before db involved, so this sort of issue was picked up before sending to db, which is nice. Obviously one could write one's own subclass of QRecord to achieve this, I think.

                    1 Reply Last reply
                    1
                    • I Offline
                      I Offline
                      Infinity
                      wrote on last edited by
                      #13

                      I'm working now with setData instead of QSqlRecord. I found it to be cleaner especially when updating values.

                      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