Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. insertRecord not saving the values
Qt 6.12 LTS is out! See what's new in the release blog

insertRecord not saving the values

Scheduled Pinned Locked Moved Solved General and Desktop
13 Posts 2 Posters 3.3k 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.
  • SGaistS Offline
    SGaistS Offline
    SGaist
    Lifetime Qt Champion
    wrote on last edited by
    #2

    Hi and welcome to devnet,

    Did you inspect the content of your database with an other tool to check if the insertion really did fail ?

    About your code: why don't you print the error you have on insertion failure ?

    Your data method look strange: for standard roles you are not calling the base classe implementation however for your custom roles you do. That does not look good.

    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
    • Bilal_KhanB Offline
      Bilal_KhanB Offline
      Bilal_Khan
      wrote on last edited by
      #3

      Thank you for the feedback.

      • I am just trying to add a row in the model, saving it to database is the next step [I think]

      • insertRecord actually adds a blank row without the values

      • in data method, I am building QModelIndex according to custom role [code taken from Qt wiki]

      thanks again.

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

        If you are at the experimenting level, do not start by sub classing QSqlTableModel. Just use it to check your insertion code. Once you have that working, then you can go further.

        In any case, always do success checks and at least print the error if something goes wrong.

        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
        • Bilal_KhanB Offline
          Bilal_KhanB Offline
          Bilal_Khan
          wrote on last edited by
          #5

          I tried as you suggested but ... same result
          [02 records are already there in database]

          QSqlTableModel contentModel(&app, db);
          contentModel.setTable("content");    //  (id, data, type)
          contentModel.select();
          contentModel.setEditStrategy(QSqlTableModel::OnManualSubmit);
          QSqlRecord rec;
          rec.setValue("id", contentModel.rowCount());
          rec.setValue("data","Weeping Monk");
          rec.setValue("type", "text");
          
          qDebug() << contentModel.insertRecord(-1, rec);   // true
          qDebug() << contentModel.submitAll();   // false
          qDebug() << contentModel.data(contentModel.index(2,1));  // QVariant(QString, "")
          qDebug() << "ERROR: " << contentModel.lastError();  // ERROR:  QSqlError("", "No Fields to update", "")
          
          1 Reply Last reply
          0
          • SGaistS Offline
            SGaistS Offline
            SGaist
            Lifetime Qt Champion
            wrote on last edited by
            #6

            Are you sure your database connection is working ?
            Are you sure your QSqlTableModel has no error ?

            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
            • Bilal_KhanB Offline
              Bilal_KhanB Offline
              Bilal_Khan
              wrote on last edited by
              #7

              yes, I am loading 02 rows in listview and once I was able to insert one record in database by manually calling setData [in my previous example of subclassing]

              1 Reply Last reply
              0
              • Bilal_KhanB Offline
                Bilal_KhanB Offline
                Bilal_Khan
                wrote on last edited by
                #8

                Minimal code for reproduction:
                show 02 rows in view but does not insert the third one

                #include <QApplication>
                #include <QSplitter>
                #include <QStringListModel>
                #include <QListView>
                #include <QSqlDatabase>
                #include <QtDebug>
                #include <QTableView>
                #include <QSqlTableModel>
                #include <QSqlRecord>

                int main(int argc, char *argv[])
                {
                QSqlDatabase db;

                db = QSqlDatabase::addDatabase("QSQLITE");
                db.setDatabaseName("path-to/db.db");
                db.open();
                if(db.isOpen()) {
                    qDebug("database is opened");
                }
                
                QApplication a(argc, argv);
                
                
                QTableView contentView;
                
                QSqlTableModel contentModel(&a, db);
                contentModel.setEditStrategy(QSqlTableModel::OnRowChange);
                contentModel.setTable("content");  // (id: int PK, data: text, type: text)
                contentModel.select();
                
                contentView.setModel(&contentModel);
                contentView.hideColumn(contentModel.fieldIndex("id"));
                contentView.hideColumn(contentModel.fieldIndex("type"));
                contentView.show();
                
                
                QSqlRecord rec;
                //  rec.setValue("id", contentModel.rowCount());
                rec.setValue("data","Weeping Monk");
                rec.setValue("type", "text");
                
                contentModel.insertRecord(-1, rec);
                
                return a.exec();
                

                }

                1 Reply Last reply
                0
                • Bilal_KhanB Offline
                  Bilal_KhanB Offline
                  Bilal_Khan
                  wrote on last edited by
                  #9

                  It is actually working with insertRows() and setData() but not with record ... very strange

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

                    First rule: QApplication shall be the first thing you create. It does some initialisation of internal stuff that are required. Here again, you do not do any checks nor print any error if things fail.

                    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
                    1
                    • Bilal_KhanB Offline
                      Bilal_KhanB Offline
                      Bilal_Khan
                      wrote on last edited by
                      #11

                      Qt does not throw exceptions .. we are left to rely on function's return value and the lastError() ... there is no insight how something is mechanized under the hood.

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

                        You have the full Qt sources at your disposal if you are curious about the gory details.
                        The documentation is pretty complete and contains a lot of examples.

                        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
                        • Bilal_KhanB Offline
                          Bilal_KhanB Offline
                          Bilal_Khan
                          wrote on last edited by
                          #13

                          Thanks a lot again for quick response, feedback and support.

                          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
                          • Unsolved