2013-11-23

Using sqlite in Qt Application



 I got this error, every time a reopen or open a new database file:
 
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.

Sometimes I get lazy and ask google first. This time google didn't tell me.
 A few noobs like me had the same issue. There was discussion about that, but they actually was digging to deep!

The answer is very simple!

Load driver only once:

QSqlDatabase::addDatabase("QSQLITE");

And then, the database file you can close and open multiple times:

db.setDatabaseName("myDatabase.sqlite");

if (db.open())
{
qDebug() << db.lastError();

}
else
{
qDebug() << db.tables();
}


db.close();




No comments:

Post a Comment