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 Database is not giving failed connection
Forum Update on Monday, May 27th 2025

sqlite Database is not giving failed connection

Scheduled Pinned Locked Moved Solved General and Desktop
3 Posts 2 Posters 607 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.
  • A Offline
    A Offline
    Asimov
    wrote on 15 Apr 2017, 16:49 last edited by
    #1

    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?

    1 Reply Last reply
    0
    • S Offline
      S Offline
      SGaist
      Lifetime Qt Champion
      wrote on 15 Apr 2017, 16:57 last edited by SGaist
      #2

      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.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • A Offline
        A Offline
        Asimov
        wrote on 15 Apr 2017, 17:50 last edited by
        #3

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

        1 Reply Last reply
        0

        2/3

        15 Apr 2017, 16:57

        • Login

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