Results 1 to 4 of 4

Thread: Understanding "Connecting to Databases"

  1. #1
    Join Date
    Sep 2008
    Location
    Portugal
    Posts
    171
    Thanks
    57
    Thanked 4 Times in 4 Posts
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Understanding "Connecting to Databases"

    Hi

    I'm trying to better undestand how the connection to a mysal database works(http://qt-project.org/doc/qt-5.0/qts...onnecting.html).

    Ther is something i'm missing about removing a connection i still miss.
    Qt Code:
    1. #include <QSqlDatabase>
    2.  
    3. QSqlDatabase *connection(QString newHostName, QString newDatabase, QString newUserName, QString newPassword)
    4. {
    5. QSqlDatabase *db = new QSqlDatabase(QSqlDatabase::addDatabase("QMYSQL", "connection1"));
    6. db->setHostName(newHostName);
    7. db->setDatabaseName(newDatabase);
    8. db->setUserName(newUserName);
    9. db->setPassword(newPassword);
    10. return db;
    11. }
    12.  
    13. int main()
    14. {
    15. //QCoreApplication a(argc, argv);
    16. bool dbStatus = false;
    17. for(int i=0; i<2;i++)
    18. {
    19. if(dbStatus)
    20. {
    21. db->close();
    22. db->removeDatabase("connection1");
    23. dbStatus = false;
    24. }
    25. else
    26. {
    27. db = connection("localhost", "db01", "user1", "123");
    28. db->open();
    29. dbStatus = true;
    30. }
    31. }
    32. }
    To copy to clipboard, switch view to plain text mode 

    This code loops the connect/disconnect to database.
    Why do i get the message every time i remove the database?
    "QSqlDatabasePrivate::removeDatabase: connection 'connection1' is still in use, all queries will cease to work."

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Understanding "Connecting to Databases"

    You are not deleting the connection object anywhere. First of all there is no reason to use the new operator to create an instance of QSqlDatabase. Second of all a call to removeDatabase() has to occur only if all QSqlDatabase objects pointing to that connection have been deleted (it is not enough to close the connection).

    Therefore this is wrong:

    Qt Code:
    1. void fun() {
    2. QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL", "connection1");
    3. QSqlDatabase::removeDatabase("connection1");
    4. }
    To copy to clipboard, switch view to plain text mode 

    and this is correct:

    Qt Code:
    1. void fun() {
    2. {
    3. QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL", "connection1");
    4. }
    5. QSqlDatabase::removeDatabase("connection1");
    6. }
    To copy to clipboard, switch view to plain text mode 
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. The following user says thank you to wysota for this useful post:

    graciano (29th December 2013)

  4. #3
    Join Date
    Sep 2008
    Location
    Portugal
    Posts
    171
    Thanks
    57
    Thanked 4 Times in 4 Posts
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Understanding "Connecting to Databases"

    You are not deleting the connection object anywhere
    Grrr ... i forgot
    First of all there is no reason to use the new operator to create an instance of QSqlDatabase.
    I was thinking of an on/off button in the UI.

    Anyway ... this works fine:
    Qt Code:
    1. #include <QSqlDatabase>
    2.  
    3. QSqlDatabase *connection(QString newHostName, QString newDatabase, QString newUserName, QString newPassword)
    4. {
    5. QSqlDatabase *db = new QSqlDatabase(QSqlDatabase::addDatabase("QMYSQL", "connection1"));
    6. db->setHostName(newHostName);
    7. db->setDatabaseName(newDatabase);
    8. db->setUserName(newUserName);
    9. db->setPassword(newPassword);
    10. return db;
    11. }
    12.  
    13. int main()
    14. {
    15. bool dbStatus = false;
    16. for(int i=0; i<6;i++)
    17. {
    18. if(dbStatus)
    19. {
    20. db->close();
    21. delete db;
    22. QSqlDatabase::removeDatabase("connection1");
    23. dbStatus = false;
    24.  
    25. }
    26. else
    27. {
    28. db = connection("localhost", "db01", "user1", "123");
    29. db->open();
    30. dbStatus = true;
    31. }
    32. }
    33. }
    To copy to clipboard, switch view to plain text mode 

  5. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Understanding "Connecting to Databases"

    Quote Originally Posted by graciano View Post
    I was thinking of an on/off button in the UI.
    This doesn't mean you have to use the new operator:

    Qt Code:
    1. #include <QSqlDatabase>
    2.  
    3. QSqlDatabase connection(QString newHostName, QString newDatabase, QString newUserName, QString newPassword)
    4. {
    5. QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL", "connection1");
    6. db.setHostName(newHostName);
    7. db.setDatabaseName(newDatabase);
    8. db.setUserName(newUserName);
    9. db.setPassword(newPassword);
    10. return db;
    11. }
    12.  
    13. int main()
    14. {
    15. bool dbStatus = false;
    16. for(int i=0; i<6;i++)
    17. {
    18. if(dbStatus)
    19. {
    20. db.close();
    21. db = QSqlDatabase();
    22. QSqlDatabase::removeDatabase("connection1");
    23. dbStatus = false;
    24.  
    25. }
    26. else
    27. {
    28. db = connection("localhost", "db01", "user1", "123");
    29. db.open();
    30. dbStatus = true;
    31. }
    32. }
    33. }
    To copy to clipboard, switch view to plain text mode 
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. Replies: 1
    Last Post: 7th April 2010, 21:46
  2. Replies: 0
    Last Post: 27th February 2010, 17:14
  3. Replies: 3
    Last Post: 15th February 2010, 17:27
  4. Replies: 3
    Last Post: 8th July 2008, 19:37
  5. Translation QFileDialog standart buttons ("Open"/"Save"/"Cancel")
    By victor.yacovlev in forum Qt Programming
    Replies: 4
    Last Post: 24th January 2008, 19:05

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.