Qt Sqlite question
-
wrote on 6 Aug 2014, 17:59 last edited by
I can't seem to trap an open error when I corrupt the database file on purpose.
Here is my code:
@
// Find QSLite driver
db = QSqlDatabase::addDatabase("QSQLITE", "connection");QString path(DATABASE_PATH); path.append(QDir::separator()).append(DATABASE_NAME); db.setDatabaseName(path); ret = db.open();
@
When I run this the first time it will create a sqlite database in my database path using the database name and the ret variable will equal true, but when I go in and corrupt the database file and run the code again it will still open the database and the ret variable will equal true like nothing is wrong. I've tried using db.isValid() but that also returns true. The database file is clearly invalid. I must be doing something wrong, can anyone tell me how to test if the sqlite database file is corrupted when opening?
Thanks
-
Hi,
You should also add which version of Qt and OS you are currently running
-
wrote on 6 Aug 2014, 22:04 last edited by
I'm using Qt 4.8.4 and I am using 32 bit Linux Mint 17.
-
What happens if you run queries on that corrupted database ?
-
wrote on 7 Aug 2014, 17:27 last edited by
If I run queries on the corrupted database I get an error. I guess the only thing I can do is run a query something like "select sqlite_version()" and if I get an error then I know I have a corrupted database. The reason why I need to check if the db is corrupt is because sometimes on a power outage the db will get corrupted on my embedded system and I need to do a quick restore.
I thought I'd get an error on open but that doesn't seem to be the case. It only errors on open if you don'y have the driver installed. Not sure if that is the case with Qt 5 but will try it later today.
-
If not, you could create a suggestion on the but tracker (but first look whether there's already something about it)
-
wrote on 8 Aug 2014, 12:08 last edited by
Sqlite databases should mot be corrupted by a simple power outage. From http://sqlite.org/about.html
bq. SQLite responds gracefully to memory allocation failures and disk I/O errors. Transactions are ACID even if interrupted by system crashes or power failures. All of this is verified by the automated tests using special test harnesses which simulate system failures.
If you have a repeatable scenario that corrupts a database you should raise it as a bug with Sqlite.
-
wrote on 8 Aug 2014, 13:53 last edited by
I'm using Sqlite in an intensive contest from many years with Qt and Freepascal. In the past I had problems with corrupted databases. You can follow some guides to restore them.
I have resolved the problems using transactions like thisbegin exclusive transaction
commitUsing transactions I haven't got corrupted databases anymore.
1/8