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,
defaultDB.setUserName( "root");
defaultDB.setPassword( "" );
defaultDB.setHostName( "localhost" );
defaultDB.setPort( 3306 );
if(!defaultDB.open()){
QMessageBox::information(this,
"Connection Failed!", defaultDB.
lastError().
text(),
return;
}
QSqlDatabase defaultDB = QSqlDatabase::addDatabase( "QMYSQL" );
defaultDB.setUserName( "root");
defaultDB.setPassword( "" );
defaultDB.setHostName( "localhost" );
defaultDB.setPort( 3306 );
if(!defaultDB.open()){
QMessageBox::information(this, "Connection Failed!", defaultDB.lastError().text(),
QMessageBox::Ok, QMessageBox::NoButton);
return;
}
To copy to clipboard, switch view to plain text mode
then I want to create another database connection, with host: 192.168.0.1
DB.setUserName( "root");
DB.setPassword( "" );
DB.setHostName( "192.168.0.1" );
DB.setPort( 3306 );
if(!DB.open()){
QMessageBox::information(this,
"Connection Failed!", DB.
lastError().
text(),
return;
}
QSqlDatabase DB = QSqlDatabase::addDatabase( "QMYSQL" );
DB.setUserName( "root");
DB.setPassword( "" );
DB.setHostName( "192.168.0.1" );
DB.setPort( 3306 );
if(!DB.open()){
QMessageBox::information(this, "Connection Failed!", DB.lastError().text(),
QMessageBox::Ok, QMessageBox::NoButton);
return;
}
To copy to clipboard, switch view to plain text mode
but after the second database is open, I got this warning:
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.
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.
To copy to clipboard, switch view to plain text mode
Thank you very much!
Bookmarks