Important: Please read the Qt Code of Conduct -

SQLite driver returning "true" when opening a file that is not a database

  • I took a .png file and renamed it to .db, then tried to open it using the QtSql functions. When I call QSqlDatabase::open() after setting the database name to this file, it returns true. Also, isValid() returns true. QSqlDatabase::lastError() returns an empty string after each of these calls.

    Is there some better way to determine if the file the user has chosen is indeed a valid SQLite database before attempting to use it? I know there is a certain file header that I could parse outside of Qt, but would like to do it "the Qt way" if possible.

    Thank you.

    (Qt version is 5.11.1, BTW and running on Linux Ubuntu 18.04.1)

  • Qt Champions 2017

    I had similar issue earlier. Just gave the following query and returned with error.

    QString query = "SELECT name FROM sqlite_master";
    QSqlQuery qry = db.exec(query);
       qWarning() << " Error =" << qry.lastError().text() << endl;

    Based on this I decided the wrong file. In fact my idea was to read the header information of the db file. I was in hurry and quickly solved it using above method.

  • @dheerendra ... Thank you! Sounds like a good idea. I finally ended up using "PRAGMA schema_version" as described

Log in to reply