[Qt-interest] QSqlDatabase with Sqlite question

Jason H scorp1us at yahoo.com
Thu Nov 19 03:33:34 CET 2009


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.






________________________________
From: Bill King <bill.king at nokia.com>
To: qt-interest at trolltech.com
Sent: Wed, November 18, 2009 9:05:58 PM
Subject: Re: [Qt-interest] QSqlDatabase with Sqlite question

On 11/19/2009 11:53 AM, ext KC Jones wrote: 
When my application closes, while attempting to close the Sqlite
>database and its connection, I get an error message:
>
>QSqlDatabasePrivate::removeDatabase: connection
>'qt_sql_default_connection' is still in use, all queries will cease to
>work.
>
>I've searched the list archives and found a mention of this, but I
>must be dense since I cannot make the proposed fix work for me.  I'm
>closing my connection, which seems from the docs like it should be
>sufficient.  When I go to remove the database, I get the error
>message.  In my app -- actually in my QTest test cases -- when I
>attempt to close one db and open another, I also get the message.
>
>Below is some dead simple code to reproduce my problem.  Could not be
>more simple, right?  What am I missing?
>I'm running this on Mac 10.5.8, Qt 4.5.1.  The code is:
>
>#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();
>    connection.exec("CREATE TABLE foo ( id integer PRIMARY KEY, bar
>integer );"); // everything OK
>    connection.close(); // still OK
>
>    QSqlDatabase::removeDatabase(connection.connectionName()); //
>triggers warning message
>
>    return a.exec();
>}
>_______________________________________________
>Qt-interest mailing list
>Qt-interest at trolltech.com http://lists.trolltech.com/mailman/listinfo/qt-interest It's a bit of a tricky one, but I think it's a hangover
from not reassigning the connection object to an empty QSqlDatabase
object, so the "connection" still maintains a reference count of at
least 1 when you go to remove it. The close doesn't actually decrement
the reference count (nor does the open increment). Personally I'm not
happy with the current semantics, and will have to investigate the cost
of changing to open/close increment/decrement. (Possibly not,
especially in 4.6 as it's a behaviour change, and it's too late to get
behaviour changes into 4.6).

Hope this helps,
Bill.


-- 
Bill King, Software Engineer
Qt Development Frameworks, Nokia Pty Ltd
Brisbane Office



      
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.qt-project.org/pipermail/qt-interest-old/attachments/20091118/77c61032/attachment.html 


More information about the Qt-interest-old mailing list