Results 1 to 5 of 5

Thread: Multiple connections to MySQL database

  1. #1
    Join Date
    Oct 2009
    Location
    South Africa
    Posts
    94
    Thanks
    4
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Multiple connections to MySQL database

    Hello
    Can someone assist me a bit please.
    I have set up a class to do the connecting to a database as follows:

    Qt Code:
    1. void DBMySQL::setupConnectionToDB()
    2. {
    3. db = QSqlDatabase::addDatabase("QMYSQL", m_ConnectionName);
    4. db.setDatabaseName(m_Database);
    5. db.setHostName(m_Host);
    6. db.setUserName(m_User);
    7. db.setPassword(m_Password);
    8. db.setConnectOptions();
    9. db.setConnectOptions("MYSQL_OPT_RECONNECT=1");
    10. ...
    To copy to clipboard, switch view to plain text mode 

    I am wanting to set up multiple connections, and hence the m_ConnectionName.

    I also have a function to query the database:
    Qt Code:
    1. QStringList DBMySQL::getData(QString queryString)
    2. {
    3. QSqlQuery query;
    4. m_DBState = query.exec(queryString);
    5. if (m_DBState) {
    6. query.setForwardOnly(true);
    7. int fieldCount = query.record().count();
    8. m_Result = QString::number(fieldCount,10) + " batches retrieved";
    9. while (query.next()) {
    10. QString str("");
    11. for (int i = 1; i <= fieldCount ; ++i) {
    12. if (i == fieldCount)
    13. str = str + query.value(i-1).toString();
    14. else
    15. str = str + query.value(i-1).toString() + ",";
    16. }
    17. sl << str;
    18. }
    19. }
    20. else
    21. m_Result = "Could not retrieve data";
    22. m_ErrorMsg = db.lastError().text();
    23. emit dbState(m_DBState, m_Result, m_ErrorMsg);
    24. return sl;
    25. }
    To copy to clipboard, switch view to plain text mode 

    This returns the result of the query in a QStringList.

    Now from my calling function I have done the following:

    Qt Code:
    1. dbConnector1 = new DBMySQL("server1","database1","user1","pw1","default");
    2. dbConnector2 = new DBMySQL("server1","database2","user2","pw2","other");
    To copy to clipboard, switch view to plain text mode 

    where dbConnector 1 & 2 are of type DBMySQL

    I have a MainWindow with a combobox on, and I want to populate it:

    Qt Code:
    1. void MainWindow::extractBatches(QString modelName)
    2. {
    3. QString queryString = "SELECT DISTINCT batch_no FROM doicii_5_6k ORDER BY batch_no";
    4. QStringList batchList = dbConnector2->getData(queryString); //from "other" database
    5. ui->comboBoxBatch->insertItems(0,batchList);
    6. ui->comboBoxBatch->setCurrentIndex(-1);
    7. }
    To copy to clipboard, switch view to plain text mode 

    However, I get the following error:

    QSqlQuery::exec: database not open
    QSqlDatabasePrivate::removeDatabase: connection 'default' is still in use, all queries will cease to work.
    QSqlDatabasePrivate::removeDatabase: connection 'other' is still in use, all queries will cease to work.
    Can someone explain what I am doing wrong?

    My reason for wanting to access different databases is because there are already existing databases in use, I want info from more than one in my program, sort of like making a report based on all data from all databases. I will then take this data and put it into the "default" database.

    Regards,
    Shaun

  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: Multiple connections to MySQL database

    Use
    Qt Code:
    1. QSqlQuery query(QSqlDatabase::database(m_ConnectionName));
    To copy to clipboard, switch view to plain text mode 
    because you have to tell which database QSqlQuery should use, otherwise it uses the default, which you haven't defined, hence you get the error.

  3. #3
    Join Date
    Oct 2009
    Location
    South Africa
    Posts
    94
    Thanks
    4
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Multiple connections to MySQL database

    Perfect.
    Thank you!
    Can I ask why it works like this though. Surely I have 2 different instances of DBMySQL class, they shouldn't even know about each other?

  4. #4
    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: Multiple connections to MySQL database

    The problem is, that QSqlQuery needs a database to perform on. If you don't specify one, Qt uses it default database which is defines as
    Qt Code:
    1. QSqlDatabase::database("defaultConnection")
    To copy to clipboard, switch view to plain text mode 
    So since you use addDatabase() with custom connection names, defaultConnection is not set. That is the problem.

    And QSqlDatabase is a singleton pattern therefore they know each other! (Not directly but...)

  5. #5
    Join Date
    Oct 2009
    Location
    South Africa
    Posts
    94
    Thanks
    4
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Multiple connections to MySQL database

    Ok, thanks.
    Didn't realise it was Singleton.

Similar Threads

  1. Removing database connections
    By Matt31415 in forum Qt Programming
    Replies: 1
    Last Post: 4th July 2010, 07:08
  2. Multiple database connections
    By Matt31415 in forum Qt Programming
    Replies: 9
    Last Post: 4th June 2010, 10:32
  3. QSqlite, multiple connections to in memory database.
    By adzajac in forum Qt Programming
    Replies: 9
    Last Post: 10th March 2010, 22:35
  4. Multiple database connections
    By cyberboy in forum Qt Programming
    Replies: 3
    Last Post: 30th March 2008, 16:56
  5. Qt on X11 with multiple display connections?
    By grenoble in forum Qt Programming
    Replies: 1
    Last Post: 25th February 2008, 12:44

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.