[Qt-interest] Models prevent QSqlDatabase::removeDatabase() from removing the DB

Sven Grunewaldt strayer at olle-orks.org
Wed Feb 11 23:40:05 CET 2009


I use two Models in my Code that display the contents of a SQLite 
database. My application can change the SQLite database while running.

This is the code that is executed when the user opens a SQLite database:
bool DataHandler::openDatabase(QString file, bool removeBeforeOpen)
{
     // Ggfs. alte DB schließen
     if (QSqlDatabase::contains(QSqlDatabase::defaultConnection))
     {
         QSqlDatabase::database().close();
         QSqlDatabase::removeDatabase(QSqlDatabase::defaultConnection);
     }

     // Angegebene Datei löschen
     if (removeBeforeOpen && QFile::exists(file))
     {
         QFileInfo fh(file);
         qDebug() << fh.isWritable(); // outputs true
         qDebug() << QFile::remove(file); // outputs false (see below)
     }

     QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
     db.setDatabaseName(file);

     if (!db.open())
     {
         QMessageBox::critical(0, QObject::tr("Datenbankfehler!"),
             db.lastError().text());
         return false;
     }

     return true;
}

QSqlDatabase::removeDatabase(QSqlDatabase::defaultConnection); causes 
this warning:
QSqlDatabasePrivate::removeDatabase: connection 
'qt_sql_default_connection' is still in use, all queries will cease to work.

The software "Unlocker" shows several handles on the opened SQLite file 
whose count increases whenever I add a "new" database, and THIS causes 
that I can't replace the SQLite database. (see if(removeBeforeOpen [...]) )

I tried to find out if I can remove the models from the views and delete 
the pointers, but I could not find any informations about this.
If I don't create the Models the warning disappears.

Regards,
Sven Grunewaldt



More information about the Qt-interest-old mailing list