Results 1 to 8 of 8

Thread: Qt Check if there is an open database connection

  1. #1
    Join Date
    Dec 2010
    Location
    South Africa
    Posts
    56
    Thanks
    8
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Maemo/MeeGo

    Default Qt Check if there is an open database connection

    Good day all.

    Im currently using this code in my project to open a mysql database file

    ..... mydatabase.cpp....

    Qt Code:
    1. bool myDatabase::createDatabaseConn()
    2. {
    3. QSettings settings("ATSTech", "ats_shopfront");
    4.  
    5. settings.beginGroup("database");
    6.  
    7. QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL");
    8.  
    9. db.setHostName(settings.value("server").toString());
    10. db.setDatabaseName("dbname");
    11. db.setUserName(settings.value("databaseUsername").toString());
    12. db.setPassword(settings.value("databasePassword").toString());
    13.  
    14. if (!db.open()) {
    15. //QMessageBox::critical(0, QObject::tr("Database Error"), db.lastError().text());
    16. return false;
    17. }
    18. return true;
    19.  
    20. settings.endGroup();
    21. }
    To copy to clipboard, switch view to plain text mode 

    No when i use this from my other pages like so

    Qt Code:
    1. myDatabase *getinfo = new myDatabase();
    2. getinfo->createDatabaseConn();
    To copy to clipboard, switch view to plain text mode 

    i get the following warinings/errors

    Qt Code:
    1. QSqlDatabasePrivate::addDatabase: duplicate connection name 'qt_sql_default_connection', old connection removed.
    To copy to clipboard, switch view to plain text mode 

    Is there any way to in the code at the top to check if there is already a connection and if so use that one instead of removing and creating a new one to the database.
    im sure that would make things faster aswell

    regards
    Donovan Hoare

  2. #2
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,536
    Thanked 284 Times in 279 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Qt Check if there is an open database connection

    First use addDatabase like this :
    Qt Code:
    1. bool myDatabase::createDatabaseConn()
    2. {
    3. QSettings settings("ATSTech", "ats_shopfront");
    4.  
    5. settings.beginGroup("database");
    6.  
    7. QSqlDatabase db = QSqlDatabase::database();
    8. if( db.isValid() )
    9. return true;
    10.  
    11. db = QSqlDatabase::addDatabase("QMYSQL");
    12.  
    13. db.setHostName(settings.value("server").toString());
    14. db.setDatabaseName("dbname");
    15. db.setUserName(settings.value("databaseUsername").toString());
    16. db.setPassword(settings.value("databasePassword").toString());
    17.  
    18. if (!db.open()) {
    19. //QMessageBox::critical(0, QObject::tr("Database Error"), db.lastError().text());
    20. return false;
    21. }
    22. return true;
    23.  
    24. settings.endGroup();
    25. }
    To copy to clipboard, switch view to plain text mode 

  3. #3
    Join Date
    Sep 2011
    Location
    Manchester
    Posts
    538
    Thanks
    3
    Thanked 106 Times in 103 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Qt Check if there is an open database connection

    First when adding a database, name the connection:
    Qt Code:
    1. QSqlDatabase::addDatabase( "QMYSQL", "OneNameOrOther" );
    To copy to clipboard, switch view to plain text mode 
    Then use that name to query for the connection:
    Qt Code:
    1. if( QSqlDatabase::contains( "OneNameOrOther" ) )
    2. {
    3. QSqlDatabase db = QSqlDatabase::database( "OneNameOrOther" );
    4. // now do some stuff with it
    5. }
    6. else
    7. {
    8. // connection not found, do something
    9. }
    To copy to clipboard, switch view to plain text mode 

  4. #4
    Join Date
    Jan 2012
    Location
    Argentina
    Posts
    167
    Thanks
    33
    Thanked 10 Times in 10 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Qt Check if there is an open database connection

    Spitfire has the answer

  5. #5
    Join Date
    Dec 2010
    Location
    South Africa
    Posts
    56
    Thanks
    8
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Maemo/MeeGo

    Default Re: Qt Check if there is an open database connection

    Quote Originally Posted by Spitfire View Post
    First when adding a database, name the connection:
    Qt Code:
    1. QSqlDatabase::addDatabase( "QMYSQL", "OneNameOrOther" );
    To copy to clipboard, switch view to plain text mode 
    Then use that name to query for the connection:
    Qt Code:
    1. if( QSqlDatabase::contains( "OneNameOrOther" ) )
    2. {
    3. QSqlDatabase db = QSqlDatabase::database( "OneNameOrOther" );
    4. // now do some stuff with it
    5. }
    6. else
    7. {
    8. // connection not found, do something
    9. }
    To copy to clipboard, switch view to plain text mode 

    According to your device i changed my code to this

    Qt Code:
    1. QSettings settings("ATSTech", "StockManager");
    2.  
    3. settings.beginGroup("database");
    4.  
    5. /* if( QSqlDatabase::contains( "StockManagerConn" ) )
    6.   {
    7.   QSqlDatabase db = QSqlDatabase::database( "StockManagerConn" );
    8.   qDebug()<<"Database Open : Using Stockmanager Connection";
    9.   return true;
    10.   }
    11.   else
    12.   {*/
    13.  
    14. QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL","StockManagerConn");
    15.  
    16. db.setHostName(settings.value("server").toString());
    17. db.setDatabaseName("stockmanager");
    18. db.setUserName(settings.value("databaseUsername").toString());
    19. db.setPassword(settings.value("databasePassword").toString());
    20.  
    21.  
    22.  
    23. if (!db.open()) {
    24. QMessageBox::critical(0, QObject::tr("Database Error"), db.lastError().text());
    25. return false;
    26. }
    27.  
    28. qDebug()<<"Database Opened : Started Stockmanager Connection";
    29. return true;
    30. //}
    31.  
    32.  
    33. settings.endGroup();
    To copy to clipboard, switch view to plain text mode 

    but i started getting this error

    Qt Code:
    1. Database Opened : Started Stockmanager Connection
    2. QSqlQuery::exec: database not open
    3. ERROR : inserting Company Name QSqlError(-1, "Driver not loaded", "Driver not loaded")
    To copy to clipboard, switch view to plain text mode 

    As soon as i remove the connection name it works great.
    What could i possibly be doing wrong.

    Regards

  6. #6
    Join Date
    Sep 2011
    Location
    Manchester
    Posts
    538
    Thanks
    3
    Thanked 106 Times in 103 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Qt Check if there is an open database connection

    You're showing wrong part of your code.
    Error is generated by the part that executes a query not sets up the database.

    You're probably not using connection name there.
    When creating the query you have to specify which connection you want to use:
    Qt Code:
    1. QSqlQuery query( QSqlDatabase::database( "StockManagerConn" ) );
    To copy to clipboard, switch view to plain text mode 
    otherwise it will fall back on the default name and fail.

  7. The following user says thank you to Spitfire for this useful post:

    ShapeShiftme (17th February 2012)

  8. #7
    Join Date
    Dec 2010
    Location
    South Africa
    Posts
    56
    Thanks
    8
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Maemo/MeeGo

    Default [Solved] Qt Check if there is an open database connection

    Quote Originally Posted by Spitfire View Post
    You're showing wrong part of your code.
    Error is generated by the part that executes a query not sets up the database.

    You're probably not using connection name there.
    When creating the query you have to specify which connection you want to use:
    Qt Code:
    1. QSqlQuery query( QSqlDatabase::database( "StockManagerConn" ) );
    To copy to clipboard, switch view to plain text mode 
    otherwise it will fall back on the default name and fail.
    Thank you so much you are right.
    When declaring my QSqlQuery i changed it to add the connection and it worked great.
    You are a star.

  9. #8
    Join Date
    Sep 2011
    Location
    Manchester
    Posts
    538
    Thanks
    3
    Thanked 106 Times in 103 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: [Solved] Qt Check if there is an open database connection

    I'm glad I could help

    Take care!

Similar Threads

  1. Check network connection
    By sophister in forum Qt Programming
    Replies: 8
    Last Post: 3rd September 2012, 12:00
  2. [SOLVED] database opened .. database not open
    By kapitanluffy in forum Qt Programming
    Replies: 1
    Last Post: 27th February 2011, 10:39
  3. Replies: 1
    Last Post: 2nd April 2010, 06:42
  4. Check if a table exists in a mysql database
    By graciano in forum Qt Programming
    Replies: 8
    Last Post: 5th November 2009, 02:44
  5. database connection
    By peace_comp in forum Qt Programming
    Replies: 4
    Last Post: 13th May 2008, 12:16

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.