QSqlTableModel insertRecord() returns true but nothing is saved in database
-
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).
-
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 ?
-
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);
}
@ -
Did you check what edit strategy is used for the model ? If it's OnManualSubmit then you have to submit your changes
-
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.
-
And if you remove the QSQLITE_ENABLE_SHARED_CACHE option ?
-
Can you create a minimal compilable example that shows this behavior ?
-
You can use e.g. "pastebin":http://pastebin.com
-
In that case check "here":https://gist.github.com/snow45/61cea39423b79c61e10f
Remember I only see this behavior with the setup initially described.
-
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.
-
Out of curiosity, how was it doing it ?