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. closing database connection

closing database connection

Scheduled Pinned Locked Moved Solved General and Desktop
6 Posts 3 Posters 8.4k 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.
  • B Offline
    B Offline
    bart.hollis
    wrote on 15 Apr 2018, 02:03 last edited by
    #1

    I have a function in my program that creates or opens a SqLite database:

    QSqlError createDb()
    {
        QSqlDatabase db;
        db = QSqlDatabase::addDatabase("QSQLITE");
        db.setDatabaseName("tracker.db");
        db.open();
        return db.lastError();
    }
    

    It works fine.
    In the function I call to quit the program is a call to this function:

    /QSqlError closeDb()
    {
        QSqlDatabase db;
        db = QSqlDatabase::database();
    
    // debug text:
        if ( db.isOpen() )
            std::cout << "the database is:  open" << std::endl;
        else
            std::cout << "the database is:  Closed" << std::endl;
    
        db.close();
    
    // debug text.
        if ( db.isOpen() )
            std::cout << "the database is:  open" << std::endl;
        else
            std::cout << "the database is:  Closed" << std::endl;
    
        db.removeDatabase( QSqlDatabase::defaultConnection );
        return db.lastError();
    }
    

    It also seems to work, based on the debug couts in place.

    In various functions in the program, I create a QSqlQuery, but these are created within the function and should, as I understand it, go away or become non-existant when the function ends.

    When I run the program, and close it, after the debug messages showing the database is closed, I get an error message stating :

    QSqlDatabasePrivate::removeDatabase: connection 'qt_sql_default_connection' is still in use, all queries will cease to work.
    

    I read that message to mean that the database is closed but that I still have a connection to it somewhere. Further, there are some querys hanging around and that means memory leaks!

    Looking at the docs for QSqlQuery, I see QSqlQuery::clear() with the statement that I should rarely if ever need it. I see ~QSqlQuery() which causes a crash. I see QSqlQuery::finish() which there's normally no need to call it.

    So, what do I do? ( Read that as what do I not understand? )

    1 Reply Last reply
    0
    • C Offline
      C Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on 15 Apr 2018, 05:52 last edited by
      #2

      You have to make sure that no QSqlQuery exits using this connection when you want to close it to avoid this warning. Or just ignore it :)

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      1 Reply Last reply
      1
      • B Offline
        B Offline
        bart.hollis
        wrote on 15 Apr 2018, 09:52 last edited by
        #3

        But if a query is created within a function, does it not cease to exist when the function returns?

        A 1 Reply Last reply 15 Apr 2018, 15:40
        0
        • C Offline
          C Offline
          Christian Ehrlicher
          Lifetime Qt Champion
          wrote on 15 Apr 2018, 11:59 last edited by Christian Ehrlicher
          #4

          Yes but there must be an open QSqlQuery anywhere - at least this is what the warning tells you.
          /edit: maybe a QSqlQuery as member value somewhere?

          Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
          Visit the Qt Academy at https://academy.qt.io/catalog

          1 Reply Last reply
          2
          • B bart.hollis
            15 Apr 2018, 09:52

            But if a query is created within a function, does it not cease to exist when the function returns?

            A Offline
            A Offline
            aha_1980
            Lifetime Qt Champion
            wrote on 15 Apr 2018, 15:40 last edited by
            #5

            @bart.hollis said in closing database connection:

            But if a query is created within a function, does it not cease to exist when the function returns?

            if you create the query as a stack variable, the it is destroyed when the surrounding scope is leaved.

            if you create the query as heap variable (with new) then it exists until delete or program end.

            Qt has to stay free or it will die.

            1 Reply Last reply
            0
            • B Offline
              B Offline
              bart.hollis
              wrote on 15 Apr 2018, 20:49 last edited by
              #6

              I Found it!

              In the closeDB function I wrote, I called the line:

              QSqlDatabase db = QSqlDatabase::database();
              

              And then closed it. This obviously created an instance of the connection which was left dangling when I removed it. The new function reads as follows:

              // >>>>>>>>>>>>>>>>  Close the database when quitting  <<<<<<<<<<<<<<<<<<<<<
              void closeDb()
              {
                  {
                      QSqlDatabase db = QSqlDatabase::database();
                      db.close();
                  }
                  QSqlDatabase::removeDatabase( QSqlDatabase::defaultConnection );
                  return;
              }
              
              

              This has eliminated the warning.

              Thanks all for your assistance.

              1 Reply Last reply
              0

              1/6

              15 Apr 2018, 02:03

              • Login

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