Close database .db. connection ’info.db’ is still in use, all queries will cease to work.



  • Hi, I'm new

    I want to delete a sqlite database(.db file) in my aplication. For
    this, I delete the file but, before, I have to close the database, but
    I can't
    @|
    QSqlDatabase::removeDatabase('info.db');
    QFile fil('/info.db');
    fil.remove(nombreArchivo);
    @
    I see the error

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

    I don't know how to close

    [Edit : please use @ code tags around your code, Eddy]


  • Moderators

    sounds like you have to clean up a pointer to your database connection.

    Can you show us the relevant code you use to make the database connection?


  • Moderators

    have a look at "this thread":http://developer.qt.nokia.com/forums/viewthread/328/#58084.

    I think it's similar to yours.



  • At least you should close the db before removing it:

    @
    QSqlDatabase db = QSqlDatabase::database("info.db");
    db.close();
    QSqlDatabase::removeDatabase("info.db");
    @

    And make sure, you do not have any QSqlQueries open at that time, as those are invalid then.

    You might want to check the error results of the various objects.

    Your code to remove the file looks fishy too. You instantiate a file with a path, but call a static method with another file path to actually remov the file.



  • This error is actually quite hard to avoid. You will have to make sure that every query using the database goes out of scope before you try to close the database. That is very inconvenient, but that is the way it is. I usually try to explicitly scope QSqlQuery objects, so that I have control over their life time. So, in stead of:

    @
    double calculateCostOfRaise(double percentage) {
    QSqlDatabase db(...);

    double totalCost(0.0);
    double factor(percentage/100.0);

    QSqlQuery query = QSqlQuery(db, "SELECT * FROM Employees");
    int fieldNo = query.record().indexOf("salary");
    while (query.next()) {
    totalCost += factor * query.value(fieldNo).toDouble();
    }

    db.removeDatabase(...);
    return totalCost;
    }
    @

    I usually do something like:

    @
    double calculateCostOfRaise(double percentage) {
    QSqlDatabase db(...);

    double totalCost(0.0);
    double factor(percentage/100.0);

    { //explicit scope around QSqlQuery usuage
    QSqlQuery query = QSqlQuery(db, "SELECT * FROM Employees");
    int fieldNo = query.record().indexOf("salary");
    while (query.next()) {
    totalCost += factor * query.value(fieldNo).toDouble();
    }
    } // QSqlQuery is out of scope here, so before the database delete

    db.removeDatabase(...);

    return totalCost;
    }
    @

    (And yes, I know that the above operation can be done more efficiently, that is not the point.)



  • Shouldn't "QSqlQuery::clear() ":http://doc.qt.nokia.com/4.7/qsqlquery.html#clear do the trick?



  • Perhaps it does, yes.



  • Just tested, it, doesn't seem so. A suggestion for a QSqlQuery::close() would be ok then. I'll have to think over it.



  • Perhaps the warning should just be removed. It sounds very obvious to me that if you close a database, queries on that database do not work anymore. I usually ran into this while trying to exit and cleanup (a module in) my application. It would have been useful if QSqlDatabase would have been a class where you would have actually have to keep an instance around to represent the database, and the database would have automatically closed if that instance was deleted or something like that. But that is not the case, any database removal is explicit. That makes a warning at that point useless IMHO.



  • This is my conection

    @
    base = QSqlDatabase::addDatabase("QSQLITE","info.db");
    base.setHostName("/" + "info.db");
    base.setDatabaseName("/" + "info.db");
    base.setUserName("root");
    @

    I use QSqlQuery::clear() for every QSqlQuery, but the error persist

    [EDIT: code formatting, please wrap in @-tags, Volker]



  • [quote author="Andre" date="1317547945"]Perhaps the warning should just be removed. It sounds very obvious to me that if you close a database, queries on that database do not work anymore. I usually ran into this while trying to exit and cleanup (a module in) my application. It would have been useful if QSqlDatabase would have been a class where you would have actually have to keep an instance around to represent the database, and the database would have automatically closed if that instance was deleted or something like that. But that is not the case, any database removal is explicit. That makes a warning at that point useless IMHO. [/quote]

    That would had made the design different, but not solved this particular issue.

    All in all, it's just a warning: you have active queries / cursors / models / records / etc. that are still using the database when you try to close it. You can ignore it, but it's a better idea to go around and clear / destroy them first.



  • I usually like the warnings, the can give a hint to a possible design flaw leading to hard to debug errors.


Log in to reply
 

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