Hi all,

Is it possible to connect to multiple database with different server/host at the same time? If yes, how?

I'm currently trying to connect to two different database, one in localhost and other in a network PC. I'm establishing connection using QSqlDatabase,

Qt Code:
  1. QSqlDatabase defaultDB = QSqlDatabase::addDatabase( "QMYSQL" );
  2. defaultDB.setUserName( "root");
  3. defaultDB.setPassword( "" );
  4. defaultDB.setHostName( "localhost" );
  5. defaultDB.setPort( 3306 );
  6.  
  7. if(!defaultDB.open()){
  8. QMessageBox::information(this, "Connection Failed!", defaultDB.lastError().text(),
  9. QMessageBox::Ok, QMessageBox::NoButton);
  10. return;
  11. }
To copy to clipboard, switch view to plain text mode 

then I want to create another database connection, with host: 192.168.0.1


Qt Code:
  1. QSqlDatabase DB = QSqlDatabase::addDatabase( "QMYSQL" );
  2. DB.setUserName( "root");
  3. DB.setPassword( "" );
  4. DB.setHostName( "192.168.0.1" );
  5. DB.setPort( 3306 );
  6.  
  7. if(!DB.open()){
  8. QMessageBox::information(this, "Connection Failed!", DB.lastError().text(),
  9. QMessageBox::Ok, QMessageBox::NoButton);
  10. return;
  11. }
To copy to clipboard, switch view to plain text mode 

but after the second database is open, I got this warning:

Qt Code:
  1. QSqlDatabasePrivate::removeDatabase: connection 'qt_sql_default_connection' is still in use, all queries will cease to work.
  2. QSqlDatabasePrivate::addDatabase: duplicate connection name 'qt_sql_default_connection', old connection removed.
To copy to clipboard, switch view to plain text mode 

Thank you very much!