Important: Please read the Qt Code of Conduct -

sqlite Database is not giving failed connection

  • I am connecting to an sqlite database, and that works no problem. The problem is that if I rename the database my code is not returning false. For instance check the following code.

    bool DbManager::connOpen()
        mydb = QSqlDatabase::addDatabase("QSQLITE");
        if (!
            qDebug() << "Error: connection with database fail";
            return false;
            qDebug() << "Database: connection ok";
            QSqlQuery query;
            query.exec("PRAGMA foreign_keys = ON;");
            return true;

    If I rename the database or delete the database I should get the qdebug message "Error: connection with database fail" and it should return false, but it doesn't. It is still giving me the message "database connection ok" and running code which shouldn't run if the database has failed.

    So why is open() failing to give the correct error msg on a failed open?

  • Lifetime Qt Champion


    Because unless you try to open your database in a place where you don't have the rights, SQLite will create the file if it doesn't exists thus it's working as expected.

  • @SGaist
    You are correct it is creating the file, rather than failing.
    So really I should just check to see if the file exists before trying to load it into the database, as I don't want to create it here.

Log in to reply