PDA

View Full Version : SQLite sometimes doens't INSERT into database



cevou
2nd October 2009, 18:18
Hi folks,

I have a weird problem with SQLite in Qt.

First, here is my code:

QSqlQuery query;
query.prepare("INSERT INTO details (parentid,value) VALUES(?,?)");
query.bindValue(0,parenid);
query.bindValue(1,value);
query.exec();

Sometimes this query doesn't save the entry in the database. The value is then available for an SELECT Statement while the program is running but if I close the program and start it again the value isn't there anymore. I'm not using a memory database and other statements are saved in the DB. The weird thing ist, that sometimes the query saves the entry and sometimes it doesn't.

So does anybody know how to solve this?

cydside
3rd October 2009, 04:09
Hi, I met the same problem. In a nutshell your INSERT go to journal file (read this (http://www.sqlite.org/tempfiles.html)) and you see the data until you are connected with the database(after a restart you probably won't see the data). In order to avoid that behavior you have to release all the resources like the QSqlQueryModel (http://doc.trolltech.com/4.5/qsqlquerymodel.html) using the clear() function before updating the DB and refresh the Model/View after that.

cevou
3rd October 2009, 08:49
Thank you for that little hint.
Everything works fine now.

ChrisW67
4th October 2009, 22:48
If you put the insert into a transaction and then commit it everything should be written to disc. Do you still get lost data?

pag
5th October 2009, 09:32
On some operating systems (windows) I have found that the commit will fail (leaving a journal file) if any of your sql statements are still active. It is necessary to use the clear() function on all active sql statements before the commit.

Merkura
30th October 2009, 08:10
Hello Cydside, I'm facing the very same problem, but i don't understand your hint. Can you please post some code? Thx Merkura