[Qt-interest] QSqlDatabase with Sqlite question

Tony Rietwyk tony.rietwyk at rightsoft.com.au
Thu Nov 19 23:14:07 CET 2009


Hi KC, 

The point is that you must put the QSqlDatabase declaration in a separate
scope - either a nested block, or a separate object.  The QSqlDatabase is
just a proxy or handle to the real database connection in the driver.  The
message should say that you have QSqlDatabase objects present. 

QString connName = "QSQLITE";

{
    QSqlDatabase connection = QSqlDatabase::addDatabase(connName);

    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();
    }

    // Now the QSqlDatabase will get destroyed, so there are none accessing
the driver. 
}

QSqlDatabase::removeDatabase(connName);

Yes, it is a horrible design - Qt designers are as human as the rest of us.
They get it wrong occasionally.  (Like having QWidget polluted with methods
that I think should have been in a QWindow).  But they can't maintain binary
compatibility and easily fix these things. 

Hope that helps, 

Tony.


> -----Original Message-----
> From: qt-interest-bounces at trolltech.com 
> [mailto:qt-interest-bounces at trolltech.com] On Behalf Of KC Jones
> Sent: Friday, 20 November 2009 06:43
> To: Jason H
> Cc: qt-interest at trolltech.com; Duane Hebert
> Subject: Re: [Qt-interest] QSqlDatabase with Sqlite question
> 
> 
> Thanks for all the advice.  Somehow, I'm still not arriving at a
> solution.  (BTW: Providing an explicit name for the connection only
> seemed to make matters worse.)  But following Jason's advice as I
> understood it, I now have this code, still plagued by the same warning
> message:
> 
> #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();
>     }
> 
>     QSqlDatabase::removeDatabase(connection.connectionName());
> 
>     return a.exec();
> }





More information about the Qt-interest-old mailing list