sqlite Database is not giving failed connection
Solved
General and Desktop
-
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"); mydb.setDatabaseName(myConnection); if (!mydb.open()) { qDebug() << "Error: connection with database fail"; return false; } else { 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?
-
Hi,
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.