[Qt-interest] connect mysql
Tony Rietwyk
tony.rietwyk at rightsoft.com.au
Tue Sep 20 02:40:11 CEST 2011
> Sent: Tuesday, 20 September 2011 7:41 AM
>
> I guess clear() is not enough or there is another query which was not cleared.
> According to documentation you have to make sure no open queries on the
> database connection when this function is called.
> For some reason it does not.
>
> Alex
>
>
> Sent: Tuesday, September 13, 2011 11:32 PM
>
> Hello
> I don t know why application always output that information. Anybody
> could tell me why.
>
>
>
> --------------------------------- snippets
> query.clear();
> f5ErpDb.close();
> f5ErpDb.removeDatabase("QODBC");
>
> camMysqlDb = QSqlDatabase::addDatabase("QMYSQL");
>
>
> ------------------- application output
> QSqlDatabasePrivate::removeDatabase: connection
> 'qt_sql_default_connection' is still in use, all queries will cease to work.
> QSqlDatabasePrivate::addDatabase: duplicate connection name
> 'qt_sql_default_connection', old connection removed.
Hi Alex & Pengliang,
QSqlDatabase.close says that it invalidates any queries - it says nothing about them being open or valid at the time. So you need to use block structuring to release the objects entirely:
{
QSqlDatabase f5ErpDb = QSqlDatabase::addDatabase("QODBC");
...
{
QSqlQuery query(f5ErpDb);
...
}
f5ErpDb.close();
QSqlDatabase::removeDatabase("QODBC");
}
{
QSqlDatabase camMysqlDb = QSqlDatabase::addDatabase("QMYSQL");
...
// Etc...
}
Encapsulating each block in its own method is the obvious recommendation!
Hope that helps,
Tony.
More information about the Qt-interest-old
mailing list