Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. SQLite driver returning "true" when opening a file that is not a database
Forum Update on Monday, May 27th 2025

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

Scheduled Pinned Locked Moved Solved General and Desktop
3 Posts 2 Posters 557 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Offline
    R Offline
    Robert Hairgrove
    wrote on last edited by
    #1

    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)

    1 Reply Last reply
    0
    • dheerendraD Offline
      dheerendraD Offline
      dheerendra
      Qt Champions 2022
      wrote on last edited by
      #2

      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);
      if(!qry.isValid()){
         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
      @Community Service
      Certified Qt Specialist
      http://www.pthinks.com

      R 1 Reply Last reply
      3
      • dheerendraD dheerendra

        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);
        if(!qry.isValid()){
           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.

        R Offline
        R Offline
        Robert Hairgrove
        wrote on last edited by
        #3

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

        1 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved