Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [SOLVED] QSqlDatabase annoying warnings
QtWS25 Last Chance

[SOLVED] QSqlDatabase annoying warnings

Scheduled Pinned Locked Moved General and Desktop
6 Posts 4 Posters 10.5k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Offline
    R Offline
    robvr
    wrote on last edited by
    #1

    I have the following bit of code:

    @
    class zzzz
    {
    ...
    QSqlDatabase m_database;
    }
    @

    and later

    @
    zzzz::function()
    {
    m_database= QSqlDatabase::addDatabase("QSQLITE");
    ...
    }
    @

    Whenever this runs, I get hit with the following lines on my console:

    bq. QSqlDatabasePrivate::removeDatabase: connection 'qt_sql_default_connection' is still in use, all queries will cease to work.
    QSqlDatabasePrivate::addDatabase: duplicate connection name 'qt_sql_default_connection', old connection removed.

    Is there any way to get rid of these complaints? Or better yet, can someone tell me how to do this correctly? I'm obviously missing something here..

    Thanks

    Rob

    1 Reply Last reply
    0
    • L Offline
      L Offline
      lgeyer
      wrote on last edited by
      #2

      It's quite hard to say what you acutally did wrong without seeing your code, but you will have to make sure that all queries have been gone out of scope before the database does or is explicitly removed.

      1 Reply Last reply
      0
      • R Offline
        R Offline
        robvr
        wrote on last edited by
        #3

        Oh dear, looks like I messed up...

        I can't very well post the actual code, since that belongs to a customer. I thought the stripped out bits I did post were enough to reproduce the issue, but the quick and dirty version I just put together at home does not show the problem. I'll start stripping down the real code until the bare minimum to show the problem remains when I get back to the office tomorrow. I'll post further updates when I have a complete minimal example.

        Rob

        1 Reply Last reply
        0
        • F Offline
          F Offline
          franku
          wrote on last edited by
          #4

          I've got some applications where closing the database gives warnings like the one mentioned first. But it newer harmed anyway. What I do not understand is the second message. It looks like you were opening the database connection more than once.

          This, Jen, is the internet.

          1 Reply Last reply
          0
          • C Offline
            C Offline
            ChrisW67
            wrote on last edited by
            #5

            If you call function() more than once without cleanly calling QSqlDatabase::removeDatabase() in between then you will get this warning. To cleanly close the database:

            @
            // make sure any query or model depending on the database connection is
            // out-of-scope/deleted, then

            // For a named connection
            QSqlDatabase::removeDatabase("FunkyDBName");

            // For the default connection
            QString connName;
            {
            QSqlDatabase db = QSqlDatabase::database();
            connName = db.connectionName();
            }
            QSqlDatabase::removeDatabase(connName);
            @

            You will have difficulty because you are holding m_database as a member variable and it cannot be out-of-scope. I find it easier to fetch a local handle handle using QSqlDatabase::database() in routines that need it and never holding a handle for a long period.

            On the other hand, you may be calling QSqlDatabase::addDatabase() when you really want QSqlDatabase::database() to get a handle to an existing database for use inside function().

            1 Reply Last reply
            0
            • R Offline
              R Offline
              robvr
              wrote on last edited by
              #6

              Thank you all for the responses, and just as importantly, for providing a forum where asking for help forced me to to take a new, hard look at all the code to be able to explain the problem.
              It turns out somewhere else in the code there was also a call to QSqlDatabase::addDatabase("QSQLITE"), which had escaped my notice the first times round. This second call should not have been there in the first place, and eliminating it solved this issue. In retrospect, the runtime system was entirely correct in complaining as it did (no surprise, really..).

              Rob

              1 Reply Last reply
              0

              • Login

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • Users
              • Groups
              • Search
              • Get Qt Extensions
              • Unsolved