closing database connection



  • 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? )



  • 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 :)



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



  • 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 Champions 2017

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



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


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.