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

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

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

    I have two SSDs each with a 2GB partition containing a Debian Wheezy squashfs with Qt 4.8.2. The rest of the drive space are a RAID 1 with ext4. On boot up I launch 2 X screens. On the left screen one Qt application (say App1) is launched that connects to 2 databases, one of which is shared with another application (say App2) launched on the right X screen. Each Qt application has a database viewer built in (the shared database is only viewable in App2.

    On a separate computer these drives are wiped clean (squashfs removed, data on raid removed) and initialized with a new squashfs and new data on the raid.

    My problem is as follows:
    After inital boot up (1st boot after initialization mentioned above) App1 is launched. App1 saves stuff to it's database followed by saving a png file in the same directory as the database. Then it saves the same thing to the database shared with App2 with the png file in the same directory. App2 is then launched and the you can see that its shared database holds the same info as the App1 database.

    Now, close App2. App1 saves more stuff to it's database followed by a png file. Then it saves the exact same thing to the shared database with App2. Reopen App2. None of this new stuff is saved in App2's shared database except that the png files saved.

    The QSqlTableModel insertRecord() returns true even though nothing was saved in the shared database. QSqlTableModel lastError() prints out as QSqlError(-1, "", "" ). This only happens on the first initial boot. Doing the following make no difference either:
    @
    myDatabase = new QSqlDatabase();
    myDatabase->setConnectOptions("QSQLITE_ENABLE_SHARED_CACHE");
    @

    Code in question is as follows:

    @
    bool MyStuffDb::addStuff(const MyObject& myobject, QSqlRecord* record, QSqlTableModel* table)
    {
    record->setValue("id", myobject.id);
    record->setValue("timestamp", myobject.timestamp.toString(SQLITE_DATE_FORMAT));
    record->setValue("width", myobject.width);
    record->setValue("height", myobject.height);

    if (!table->insertRecord(-1, *record))
    {
        qCritical() << Q_FUNC_INFO << "mTable.insertRecord:" << table->lastError();
        return false;
    }
    
    // Save QImage
    myobject.snippet.save(getPathToSnippet(target.id), "PNG");
    
    return true;
    

    }
    @

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

      Hi and welcome to devnet,

      The first thing that strikes is that you are not preparing your database following the recommended way:

      @
      QSqlDatabase db = QSqlDataBase::addDatabase("QSQLITE");
      db.setconnectionOptions("QSQLITE_ENABLE_SHARED_CACHE");
      @

      Out of curiosity, why pass QSqlRecord as pointer ?

      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
        #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