[Qt-interest] QSqlDatabase with Sqlite question

KC Jones kc at asperasoft.com
Thu Nov 19 18:38:15 CET 2009


Frankly, I do not see why the call to QSqlDatabase::close() is not
sufficient.  If I call close() on a connection, then try an access
queries previously created on that connection, that would be my bug.
Calling close() should do exactly what it says -- close the
connection.  What ellse does it do?

I'd almost be willing to overlook the message, but it is fouling up my
test code, making it hard to differentiate a clean run from a dirty
one (which is a topic for another thread about how to improve
QTestLib).  I really would like to figure out how to squelch this one.
 So indulge me.  If I understand what you're saying, the following
modified code should be message free.  But it is not:

#include <QtCore>
#include <QtSql>

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

    QSqlDatabase connection = QSqlDatabase::addDatabase("QSQLITE");

    connection.setDatabaseName("./test.db");
    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();
}


On Wed, Nov 18, 2009 at 6:33 PM, Jason H <scorp1us at yahoo.com> wrote:
> I'm replying to Bill because I cannot find KC Jone's original email.
>
> The behavior is correct.
> "connection" object still exists and references the database connection.
> I've racked my brain tring to produce code that does not generate the
> warning. Usually I give up, because it requires me to think differently than
> any other kind of programming language or environment.
>
> But what you need to do is provide another stack frame that creates the
> connection object,  and out side of it connect and disconnect ie.
> connect();
> {
>     QSqlQuery q; // work with dateabase via queries
> }
> disconnect();
>
> When the disconnect happens, there is no longer a 'q' object referencing the
> connection.
>
> It is *hugely* annoying but can be ignored. Its one of the more useless
> warning messages ever created. But it does have a reason, because if you
> don't buffer the entire result set, you can disconnect, go to fetch a row
> and fail. That's why you get a warning. You might know you're done with the
> DB, but it does not.
>\




More information about the Qt-interest-old mailing list