Close database .db. connection ’info.db’ is still in use, all queries will cease to work.
-
wrote on 30 Sept 2011, 08:45 last edited by
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 errorbq. 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]
-
wrote on 30 Sept 2011, 10:13 last edited by
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?
-
wrote on 30 Sept 2011, 11:14 last edited by
have a look at "this thread":http://developer.qt.nokia.com/forums/viewthread/328/#58084.
I think it's similar to yours.
-
wrote on 30 Sept 2011, 12:32 last edited by
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.
-
wrote on 2 Oct 2011, 08:34 last edited by
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 deletedb.removeDatabase(...);
return totalCost;
}
@(And yes, I know that the above operation can be done more efficiently, that is not the point.)
-
wrote on 2 Oct 2011, 09:15 last edited by
Shouldn't "QSqlQuery::clear() ":http://doc.qt.nokia.com/4.7/qsqlquery.html#clear do the trick?
-
wrote on 2 Oct 2011, 09:17 last edited by
Perhaps it does, yes.
-
wrote on 2 Oct 2011, 09:26 last edited by
Just tested, it, doesn't seem so. A suggestion for a QSqlQuery::close() would be ok then. I'll have to think over it.
-
wrote on 2 Oct 2011, 09:32 last edited by
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.
-
wrote on 3 Oct 2011, 09:08 last edited by
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]
-
wrote on 3 Oct 2011, 13:14 last edited by
[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.
-
wrote on 3 Oct 2011, 23:10 last edited by
I usually like the warnings, the can give a hint to a possible design flaw leading to hard to debug errors.
1/12