Another QSqlDatase connection question
-
Hi,
This issue does seem to crop up in google searches but I still haven't found out why my issue persists.
It's the old
@SqlDatabasePrivate::removeDatabase: connection 'myconnectionname' is still in use, all queries will cease to work@
problem.
I will pretext this by saying I'm a C programmer new to QT and rusty on C++ so go easy please.
I've tried to distill this down to as simple a situation as possible. I have a dialog which I wish to be able to open a given database, do something with it, then close it.
I have found a way to get this to work, but would like a better solution.One button opens a connection to a database, some other code will work with that connection (which I've removed for now) and another button will close the database.
My Open button was as follows:
@void test_QT::on_openDbButton_clicked(void)
{
QSqlDatabase db;
db=QSqlDatabase::addDatabase("QSQLITE", "test");
printf("Added database connection %s\n", db.connectionName().toAscii().constData());
db.setDatabaseName("test.db3");
if (!db.open())
printf("Open failed\n");
}
@After reading the example of SQLDatabase::RemoveDatabase, I twigged that I could not do this:
@
void test_QT::on_closeDbButton_clicked(void)
{
QString conn;
QSqlDatabase db = QSqlDatabase::database("test");
conn=db.connectionName(); /* just checking! */
if(db.isValid())
{
printf("Closing database %s\n", db.databaseName().toAscii().constData());
db.close();
}
QSqlDatabase::removeDatabase("test");
}
@
and had to move the QSqlDatabase::removeDatabase call to the open button, prior to the addDatabase call, as by this time the db object in the close button is out of scope and has been destroyed.
It would make more sense to me to destroy the database connection as soon as the database is closed. Is there a way I can do this nicely?EDIT: how to escape double colon o?
-
Calling db.close destroys the connection("found here":http://doc.qt.nokia.com/latest/qsqldatabase.html#close). So when you call QSqlDatabase::removeDatabase you are trying to remove a connection that has already been removed. As far as the scope issue, you could always make db a member of the class itself( of course this comes with other issues that will need to be addressed).
-
erm...the error message suggests that the connection is still in use, which should not be the case if the database using it has been closed, as far as I can tell from the docs. The point is qt seems to be complaining that it hasn't been removed properly, since it thinks something is still referencing it.
I originally made db part of the class, and doing so means it doesn't go out of scope, doesn't get destroyed, and would not be able to be removed even in the open code.So, not sure any of what you just suggested was useful, but thanks for trying.
-
I'm aware of the reason I am getting the warning...what is the best way around this?
I know the QT example for removedatabase makes sure db doesn't exist by letting it go out of scope.
My workaround does this by calling removedatabase before I open another connection, but I would like it to occur when I close the current one.
Is there a way to remove db nicely before calling removedatabase in the same method? (freeing it directly doesn't seem very elegant) -
You could use {} to limit the scope of QSqlDatabase db. For example:
@void test_QT:<span class="smiley">:o</span>n_closeDbButton_clicked(void)
{
QString conn;
{
QSqlDatabase db = QSqlDatabase::database("test");
conn=db.connectionName(); /* just checking! */
if(db.isValid())
{
printf("Closing database %s\n", db.databaseName().toAscii().constData());
db.close();
}
}
QSqlDatabase::removeDatabase("test");
}@