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. QSqlTableModel insertRecord() returns true but nothing is saved in database
Forum Updated to NodeBB v4.3 + New Features

QSqlTableModel insertRecord() returns true but nothing is saved in database

Scheduled Pinned Locked Moved General and Desktop
16 Posts 2 Posters 4.4k 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.
  • S Offline
    S Offline
    snow45
    wrote on last edited by
    #3

    Actually, I do that. Just didn't copy that part over.

    @
    bool MyStuffDb::open(const QString& path)
    {
    mPath = path;

    //create connection to database
    myDatabase = new QSqlDatabase();
    *myDatabase = QSqlDatabase::addDatabase( "QSQLITE", mConnectionName );
    myDatabase->setDatabaseName( path + "/myDatabase.db" );
    myDatabase->setConnectOptions("QSQLITE_ENABLE_SHARED_CACHE");
    
    if( !myDatabase->open() )
    {
        qCritical() << Q_FUNC_INFO << "myDatabase->open:" << myDatabase->lastError();
        return false;
    }
    
    return true;
    

    }
    @

    As for using QSqlRecord being passed as pointer, that's just how the original guy wrote it (I think he liked to pass as pointer if he modified it and by reference if he didn't).

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

      Then you have a memory leak since you overwrite myDataBase right after you created it. There's no need for a pointer here.

      Also, how is the QSqlRecord created ?

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

        Fixed the memory leak, but didn't make a difference. QSqlRecord comes from:

        @
        bool MyStuffDb::addStuff(const MyObject& myobject)
        {
        QSqlTableModel table(NULL, myDatabase);
        table.setTable("myobjects");
        QSqlRecord record = myDatabase.record("myobjects");

        // Call to code in first post
        return addStuff(myobject, &record, &table);
        

        }
        @

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

          Did you check what edit strategy is used for the model ? If it's OnManualSubmit then you have to submit your changes

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

            I've checked that and tried them all without success. After calling insertRecord() I can see the row was inserted in memory but just not saved to disk, ever. Is there anyway to flush it to disk?

            I even switched over to QSqlQuery instead of using QSqlRecord and QSqlTableModel and had the same result. Could this be a linux issue somehow? As I mentioned before I only see this behavior on the first boot off a drive. Works like a charm after that.

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

              And if you remove the QSQLITE_ENABLE_SHARED_CACHE option ?

              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
              • S Offline
                S Offline
                snow45
                wrote on last edited by
                #9

                Problem exists with or without that option.

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

                  Can you create a minimal compilable example that shows this behavior ?

                  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
                  • S Offline
                    S Offline
                    snow45
                    wrote on last edited by
                    #11

                    I will try and put one together. Been super busy with other things that need done asap.

                    1 Reply Last reply
                    0
                    • S Offline
                      S Offline
                      snow45
                      wrote on last edited by
                      #12

                      What's the best way to post this? Can't add attachments and it's a lot of code to post in a reply.

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

                        You can use e.g. "pastebin":http://pastebin.com

                        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
                        • S Offline
                          S Offline
                          snow45
                          wrote on last edited by
                          #14

                          In that case check "here":https://gist.github.com/snow45/61cea39423b79c61e10f

                          Remember I only see this behavior with the setup initially described.

                          1 Reply Last reply
                          0
                          • S Offline
                            S Offline
                            snow45
                            wrote on last edited by
                            #15

                            I found the problem. I used the command lsattr to look at file attributes on my database files and found that when I opened App 2 for the first time I was deleting the shared database out from under App 1 and recreating it. Just had to fix that and everything works like I would expect.

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

                              Out of curiosity, how was it doing it ?

                              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