[Qt-interest] QSqlDatabase with Sqlite question

KC Jones kc at asperasoft.com
Thu Nov 19 22:55:04 CET 2009


> I still suspect connection.connectionName()
> Would you be willing to add a few qDebug statements to print out its value at several points in the program ?

Thanks to this advice, and some code provided to me off-list, I have
finally reached my goal of message free db connection shut down.
Frankly, I do not understand.  There is no way I would ever have come
to this solution on my own.  Either I'm dense, or there is something
screwy in the API.  Probably both.

So for Google-y-posterity, here is the working solution to the
"QSqlDatabasePrivate::removeDatabase: connection
'qt_sql_default_connection' is still in use, all queries will cease
towork." mystery.  Thank you, all:

#include <QtCore>
#include <QtSql>

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);

    QFile::remove("./test.db");
    QSqlDatabase connection = QSqlDatabase::addDatabase("QSQLITE");

    connection.setDatabaseName("./test.db");
    if (connection.open())
    {
        QSqlQuery* q = new QSqlQuery();
        q->exec("CREATE TABLE foo2 ( id integer PRIMARY KEY, bar integer );");
        delete q;
        connection.close();
    }

    QString ConnectionName = connection.connectionName();
    connection = QSqlDatabase();
    QSqlDatabase::removeDatabase(ConnectionName);

    return a.exec();
}



More information about the Qt-interest-old mailing list