Important: Please read the Qt Code of Conduct -

Qt Sqlite question

  • 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);
    ret =;


    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?


  • Lifetime Qt Champion


    You should also add which version of Qt and OS you are currently running

  • I'm using Qt 4.8.4 and I am using 32 bit Linux Mint 17.

  • Lifetime Qt Champion

    What happens if you run queries on that corrupted database ?

  • 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.

  • Lifetime Qt Champion

    If not, you could create a suggestion on the but tracker (but first look whether there's already something about it)

  • Sqlite databases should mot be corrupted by a simple power outage. From

    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.

  • 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 this

    begin exclusive transaction

    Using transactions I haven't got corrupted databases anymore.

Log in to reply