Results 1 to 12 of 12

Thread: QSqlDatabase, removeDatabase

  1. #1
    Join Date
    May 2015
    Posts
    42
    Thanks
    6
    Thanked 1 Time in 1 Post
    Qt products
    Qt5
    Platforms
    Windows

    Default QSqlDatabase, removeDatabase

    Dear All,

    I come back to a very popular error-message. I tried out a lot. I RTFM. I have no clue how to avoid the following mesage if dataGrabber is destrouyed.

    QSqlDatabasePrivate::removeDatabase: connection 'qt_sql_default_connection' is still in use, all queries will cease to work.
    Qt Code:
    1. class dataGrabber : public QObject
    2. {
    3. Q_OBJECT
    4. public:
    5. explicit dataGrabber(QObject *parent = nullptr);
    6. ~dataGrabber();
    7.  
    8. private:
    9. QSqlQuery *query;
    10.  
    11. public slots:
    12. bool openDatabase();
    13. bool closeDatabase();
    14. }
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. bool dataGrabber::openDatabase(){
    2.  
    3. dB = QSqlDatabase::addDatabase("QODBC");
    4. dB.setHostName("localhost");
    5. dB.setDatabaseName("DRIVER={Microsoft Access Driver (*.mdb)};FIL={MS Access};"
    6. "DSN='';DBQ=" + dbPath + ";");
    7.  
    8. if(dB.open()){
    9. query = new QSqlQuery(dB); // open dB and prepare query
    10. prepareQuery();
    11. return true;
    12. {
    13. else{
    14. QMessageBox::warning(0, "Failure !", dB.lastError().text(), "OK");
    15. return false;
    16. }
    17.  
    18. return true;
    19. }
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. bool dataGrabber::closeDatabase(){
    2.  
    3. query->finish();
    4. query->clear();
    5.  
    6. delete query;
    7.  
    8. dB.close();
    9.  
    10. return true;
    11. }
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. dataGrabber::~dataGrabber(){
    2.  
    3. dB.removeDatabase("qt_sql_default_connection");
    4. }
    To copy to clipboard, switch view to plain text mode 


    As always, every help highly appreciated.

    Cheers and thanks.

  2. #2
    Join Date
    Jan 2006
    Location
    Bremen, Germany
    Posts
    554
    Thanked 86 Times in 81 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QSqlDatabase, removeDatabase

    See https://doc.qt.io/qt-5/qsqldatabase.html#removeDatabase - there is still a reference in your member 'QSqlDatabase dB;'

  3. #3
    Join Date
    May 2015
    Posts
    42
    Thanks
    6
    Thanked 1 Time in 1 Post
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: QSqlDatabase, removeDatabase

    Thanks ChristianEhrlicher

    there is still a reference in your member 'QSqlDatabase dB;'
    Yes, that's why it fails. But I do not get it. The tutorial (which I read) says to leave the function containing dB for the object to be destroyed and then call dB.removeDatabase(). How can I call something on a destroyed object?
    Or am I completely mislead here?

  4. #4
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QSqlDatabase, removeDatabase

    Quote Originally Posted by mustermann.klaus@gmx.de View Post
    How can I call something on a destroyed object?
    QSqlDatabase::removeDatabase() is a static method, just like QSqlDatabase::addDatabase().


    So that would probably look similar to this
    Qt Code:
    1. const QString connectionName = db.connectionName();
    2. db = QSqlDatabase();
    3. QSqlDatabase::removeDatabase(connnectionName);
    To copy to clipboard, switch view to plain text mode 

    Cheers,
    _

  5. The following user says thank you to anda_skoa for this useful post:

    mustermann.klaus@gmx.de (17th May 2019)

  6. #5
    Join Date
    May 2015
    Posts
    42
    Thanks
    6
    Thanked 1 Time in 1 Post
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: QSqlDatabase, removeDatabase

    Okay, but I know the connectionName.

    Qt Code:
    1. dB.removeDatabase("qt_sql_default_connection");
    To copy to clipboard, switch view to plain text mode 

    I understand why but really stucked on the question from where to call that ominous removeDatabase()

    Cheers, Lars

  7. #6
    Join Date
    Jan 2006
    Location
    Bremen, Germany
    Posts
    554
    Thanked 86 Times in 81 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QSqlDatabase, removeDatabase

    Simply ise the code from anda_skoa ...

  8. The following user says thank you to ChristianEhrlicher for this useful post:

    mustermann.klaus@gmx.de (17th May 2019)

  9. #7
    Join Date
    May 2015
    Posts
    42
    Thanks
    6
    Thanked 1 Time in 1 Post
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: QSqlDatabase, removeDatabase

    If it's that easy...Now it looks like this:

    Qt Code:
    1. dataGrabber::~dataGrabber(){
    2.  
    3. const QString connectionName = dB.connectionName();
    4. dB = QSqlDatabase();
    5. dB.removeDatabase(connectionName);
    6. }
    To copy to clipboard, switch view to plain text mode 

    Somehow I do not believe that I remove the right dB from the list. Do I?
    The errormessage is gone. But dB is still a member of my class datagrabber, which I simply overwrite with an invalid object. I really do not understand, if this is the way, why I should do so.

    greetingd from rainy Berlin, Lars

  10. #8
    Join Date
    Jan 2006
    Location
    Bremen, Germany
    Posts
    554
    Thanked 86 Times in 81 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QSqlDatabase, removeDatabase

    That' not the code anda_skoa wrote for you... and why it has to be done that way was explicitly written in the documentation I gave you the link to - db holds a reference which also needs to be removed *before* QSqlDatabase::removeDatabase() is called.

  11. #9
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QSqlDatabase, removeDatabase

    Quote Originally Posted by mustermann.klaus@gmx.de View Post
    Somehow I do not believe that I remove the right dB from the list. Do I?
    Why would it not be the right one?

    Aside from not calling the static method as usual with "ClassName::methodName" this is passing the connection name of the "db" object you have been using.
    Which, I assume, is the one you would want to remove.

    Quote Originally Posted by mcanonic View Post
    But dB is still a member of my class datagrabber, which I simply overwrite with an invalid object.
    Since "dB" is not a pointer, the only way to make it drop its connection is by invalidating it through overwriting.

    Cheers,
    _

  12. The following user says thank you to anda_skoa for this useful post:

    mustermann.klaus@gmx.de (17th May 2019)

  13. #10
    Join Date
    May 2015
    Posts
    42
    Thanks
    6
    Thanked 1 Time in 1 Post
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: QSqlDatabase, removeDatabase

    Hello everybody. Thanks for guiding me. To me it still seems confusing that an object has no method to end a connection to the world without beeing destroyed. But if so, I'll take it.

    Qt Code:
    1. dataGrabber::~dataGrabber(){
    2.  
    3. const QString connectionName = dB.connectionName();
    4. dB = QSqlDatabase();
    5. QSqlDatabase::removeDatabase(connnectionName);
    6. }
    To copy to clipboard, switch view to plain text mode 

    Works fine. Thank you.

    Lars

  14. #11
    Join Date
    Jul 2012
    Posts
    244
    Thanks
    27
    Thanked 15 Times in 14 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QSqlDatabase, removeDatabase

    I think QSqlDatabase is designed to be used in a more ad-hoc way, not as member variable perhaps.


    Either way doing dB->close() seems cleaner to me?

  15. #12
    Join Date
    May 2015
    Posts
    42
    Thanks
    6
    Thanked 1 Time in 1 Post
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: QSqlDatabase, removeDatabase

    Quote Originally Posted by tuli View Post
    I think QSqlDatabase is designed to be used in a more ad-hoc way, not as member variable perhaps.


    Either way doing dB->close() seems cleaner to me?
    I think so too (in the meantime). As you can see in my initial post: dB->close(); is what I did in first sight.

    Cheers.

Similar Threads

  1. Replies: 0
    Last Post: 2nd December 2011, 20:52
  2. QSqlDatabase::removeDatabase problem
    By xeroblast in forum Qt Programming
    Replies: 1
    Last Post: 8th December 2010, 07:50
  3. Replies: 0
    Last Post: 14th July 2010, 14:40
  4. Replies: 12
    Last Post: 2nd April 2010, 10:50
  5. Replies: 10
    Last Post: 6th March 2006, 17:08

Tags for this Thread

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.