title: Handling QSqlDatabase connections
body: <p>What is the correct way to handle connections for <code>QSqlDatabase</code>?</p>
  
  <p>In my program I am doing it this way:</p>
  
  <pre><code>DatabaseConnector *databaseConnector = 0;
  try
  {
      databaseConnector = new DatabaseConnector();
      //Do stuff...
      delete databaseConnector;
  }
  catch(QString e)
  {
      delete databaseConnector;
      QMessageBox::information(this,"Error",e);
  }
  </code></pre>
  
  <p><strong>databaseConnector.h</strong></p>
  
  <pre><code>#ifndef DATABASECONNECTOR_H
  #define DATABASECONNECTOR_H
  #include &lt;QtSql&gt;
  
  class DatabaseConnector
  {
  public:
      DatabaseConnector();
      DatabaseConnector(QString hostname, QString database, QString user, QString password);
      ~DatabaseConnector();
  private:
      QSqlDatabase db;
  };
  
  #endif // DATABASECONNECTOR_H
  </code></pre>
  
  <p><strong>databaseconnector.cpp</strong></p>
  
  <pre><code>#include "databaseconnector.h"
  #include &lt;QString&gt;
  
  DatabaseConnector::DatabaseConnector()
  {
      QSettings settings;
  
      db = QSqlDatabase::addDatabase("QIBASE");
      db.setHostName(settings.value("db/host").toString());
      db.setDatabaseName(settings.value("db/name").toString());
      db.setUserName(settings.value("db/user").toString());
      db.setPassword(settings.value("db/pass").toString());
  
      if(!db.open())
      {
          QString databaseConnectionError = db.lastError().text();
          throw databaseConnectionError;
      }
  }
  
  DatabaseConnector::DatabaseConnector(QString hostname, QString database, QString user,     QString password)
  {
      db = QSqlDatabase::addDatabase("QIBASE");
      db.setHostName(hostname);
      db.setDatabaseName(database);
      db.setUserName(user);
      db.setPassword(password);
      if(!db.open())
      {
          QString databaseConnectionError = db.lastError().text();
          throw databaseConnectionError;
      }
  
  }
  
  DatabaseConnector::~DatabaseConnector()
  {
      db.close();
  }
  </code></pre>
  
  <p>I'm getting error even if I use <code>QSqlDatabase::removeDatabase(db.connectionName());</code></p>
  
  <blockquote>
    <p>QSqlDatabasePrivate::addDatabase: duplicate connection name 'qt_sql_default_connection', old connection removed.</p>
  </blockquote>