Results 1 to 5 of 5

Thread: QSqlDatabase - How to connect to multiple database?

  1. #1
    Join Date
    Jul 2008
    Location
    Philippines
    Posts
    60
    Thanks
    9
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default QSqlDatabase - How to connect to multiple database?

    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!

  2. #2
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: QSqlDatabase - How to connect to multiple database?

    Have a look in the Ddocumentation!!! Just use the second parameter of addDatabase().

  3. #3
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: QSqlDatabase - How to connect to multiple database?

    Give one or both connections a name in QSqlDatabase::addDatabase()

  4. The following user says thank you to ChrisW67 for this useful post:

    cutie.monkey (10th March 2010)

  5. #4
    Join Date
    Mar 2008
    Location
    France
    Posts
    149
    Thanks
    2
    Thanked 21 Times in 21 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QSqlDatabase - How to connect to multiple database?

    Without giving a name at least to one of the connection, there will be both 'considered' as the default one.
    As suggested, giving a name will differentiate them from each other.

  6. The following user says thank you to toutarrive for this useful post:

    galaxy66 (27th July 2017)

  7. #5
    Join Date
    Jul 2008
    Location
    Philippines
    Posts
    60
    Thanks
    9
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QSqlDatabase - How to connect to multiple database?

    Thanks, its working..

    Cheers!

Similar Threads

  1. Releasing database file with QSqlDatabase
    By JPNaude in forum Qt Programming
    Replies: 24
    Last Post: 24th May 2011, 09:33
  2. How to BackUp DataBase and Restore it Use QSqlDataBase?
    By litterflybug in forum Qt Programming
    Replies: 1
    Last Post: 6th December 2009, 04:34
  3. cannot connect to QSqldatabase
    By mave-rick in forum Qt Programming
    Replies: 3
    Last Post: 19th August 2008, 17:52
  4. how to connect to a microsoft access database?
    By mismael85 in forum Qt Programming
    Replies: 3
    Last Post: 7th March 2008, 09:25
  5. Replies: 5
    Last Post: 28th August 2006, 14:36

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.