[Qt-interest] QSqlDatabase with Sqlite question

Duane Hebert spoo at flarn.com
Fri Nov 20 14:01:44 CET 2009


"Tony Rietwyk" <tony.rietwyk at rightsoft.com.au> wrote in message news:he4fvu$vc7$1 at eple.troll.no...
> 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. 

If you put your code in a slot for a button click and click it twice you get:
QSqlDatabasePrivate::addDatabase: duplicate connection name 'qt_sql_default_connection', old connection removed.
You need to remove the link to the current connection by calling 
connection = QSqlDatabase();  before removing it or it doesn't remove it.

Look at the previous post from Bill King for an explanation.





More information about the Qt-interest-old mailing list