I got confused with the manual , should i work like this:
{
QSqlDatabase db = QSqlDatabase::addDatabase (...);
QSqlQuery query (db);
query.exec (...);
}
QSqlDatabase::removeDatabase (...);
As the document points out, query or db will be deconstructed automatically.
But is that efficient ?
Well , if i cache db inside a class , like the following:
class Dummy {
Dummy() {
db = QSqlDatabase::addDatabase (...);
}
~Dummy() {
db.close();
}
bool run() {
QSqlQuery query (db);
bool retval = query.exec (...);
blabla ...
}
private:
QSqlDatabase db;
};
Sometimes i could see warnings like:
QSqlDatabasePrivate::removeDatabase: connection 'BLABLA' is still in use, all queries will cease to work.
Even if i didn’t call run().
When you create a
QSqlDatabaseobject withaddDatabaseor when you callremoveDatabase, you are merely associating or disassociating a tuple (driver, hostname:port, database name, username/password) to a name (or to the default connection name if you don’t specify a connection name).The SQL driver is instantiated, but the database will only be opened when you call
QSqlDatabase::open.That connection name is defined application-wide. So if you call
addDatabasein each of the objects that use it, you are changing allQSqlDatabaseobjects that uses the same connection name and invalidating all queries that were active on them.The first code example you cited shows how to correctly disassociate the connection name, by ensuring that:
QSqlQueryare detached from theQSqlDatabasebefore closing the database by callingQSqlQuery::finish(), which is automatic when theQSqlQueryobject goes out of scope,QSqlDatabasewith the same connection name areclose()d when you callQSqlDatabase::removeDatabase(close()is also called automatically when theQSqlDatabaseobject goes out of scope).When you create the QSqlDatabase, depending on whether you want the connection to stay open for the application lifetime (1) or just when needed (2), you can:
keep a single
QSqlDatabaseinstance in one single class (for example, in your mainwindow), and use it in other objects that needs it either by passing theQSqlDatabasedirectly or just the connection name that you pass toQSqlDatabase::databaseto get theQSqlDatabaseinstance back.QSqlDatabase::databaseusesQHashto retrieve aQSqlDatabasefrom its name, so it is probably negligibly slower than passing theQSqlDatabaseobject directly between objects and functions, and if you you use the default connection, you don’t even have to pass anything anywhere, just callQSqlDatabase::database()without any parameter.configure the
QSqlDatabaseonce, open it to test that the parameters are correct, and ditch the instance. The connection name, will still be accessible anywhere, but the database will have to be reopened:In that case, note that you shouldn’t close the database explicitly, because you can have multiple objects using the same database connection in a reentrant manner (for example, if a function A use the connection and calls B which also use the connection. If B closes the connection before returning control to A, the connection will also be closed for A, which is probably a bad thing).