[Qt-interest] QSqlDatabase with Sqlite question

Jason H scorp1us at yahoo.com
Thu Nov 19 20:25:52 CET 2009


Well, I agree with everyone, but the truth is you can use a QSqlQuery to hold a result set and avoid having to copy it to some other storage for after the disconnect. Its not a terribly useful feature, but for small datasets it is quite nice when you're limiting concurrent DB connections.

I should have included the QSqlDatabase declaration within the { } 



See QSqlDatabase::removeDatabase():

// WRONG
QSqlDatabase db = QSqlDatabase::database("sales");
QSqlQuery query("SELECT NAME, DOB FROM EMPLOYEES", db);
QSqlDatabase::removeDatabase("sales"); // will output a warning 
// "db" is now a dangling invalid database connection,  // "query" contains an invalid result set

The correct way to do it:

{
     QSqlDatabase db = QSqlDatabase::database("sales");
     QSqlQuery query("SELECT NAME, DOB FROM EMPLOYEES", db);
}

// Both "db" and "query" are destroyed because they are out of scope
QSqlDatabase::removeDatabase("sales"); // correct

------------- This is wholy backwards from any other resource that you work with which for files is: 
if (open() )
{  
   statements; 
   close(); 
}

the DB syntax is :
{
   addDatabase()
   if (open())
   {
      statements;
      close();
   }
}
removeDataabse();

It is non-intitive that they remove is not at the same stack frame as the add. 


----- Original Message ----
From: Duane Hebert <spoo at flarn.com>
To: qt-interest at trolltech.com
Sent: Thu, November 19, 2009 12:48:18 PM
Subject: Re: [Qt-interest] QSqlDatabase with Sqlite question

....

And I don't find that very intuitive either.  Calling close() should work as expected
IMO.


      




More information about the Qt-interest-old mailing list