Q:

How often should I set the database in QT?

I have the following code for setting an SQLite database in a QT for Symbian project
QSqlDatabase db = QSqlDatabase::addDatabase( "QSQLITE" );
db.setDatabaseName( "test.db" );
if( !db.open() )
{
    qDebug() << db.lastError();
    qFatal( "Failed to connect." );
}

[Database access code...]

Now I have it before every QSqlQuery, but I get the following warning, so I suppose it's not the best solution.

QSqlDatabasePrivate::addDatabase: duplicate connection name 'qt_sql_default_connection', old connection removed.

As I have several widgets that use the database, I wonder how often should I use the above code. Should I put it only in my MainController when setting up the app? Or should it exist before every database access?

A:

It should be appear only once for database.
If you need more than one database then use more db objects, but if it's only one DB, then go with only one declaration.
Edited:
For more info, visit this.

