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. Another QSqlDatase connection question
Qt 6.11 is out! See what's new in the release blog

Another QSqlDatase connection question

Scheduled Pinned Locked Moved General and Desktop
7 Posts 3 Posters 3.2k Views 1 Watching
  • 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.
  • K Offline
    K Offline
    kreyszig
    wrote on last edited by
    #1

    Hi,

    This issue does seem to crop up in google searches but I still haven't found out why my issue persists.
    It's the old
    @SqlDatabasePrivate::removeDatabase: connection 'myconnectionname' is still in use, all queries will cease to work@
    problem.
    I will pretext this by saying I'm a C programmer new to QT and rusty on C++ so go easy please.
    I've tried to distill this down to as simple a situation as possible. I have a dialog which I wish to be able to open a given database, do something with it, then close it.
    I have found a way to get this to work, but would like a better solution.

    One button opens a connection to a database, some other code will work with that connection (which I've removed for now) and another button will close the database.
    My Open button was as follows:
    @void test_QT::on_openDbButton_clicked(void)
    {
    QSqlDatabase db;
    db=QSqlDatabase::addDatabase("QSQLITE", "test");
    printf("Added database connection %s\n", db.connectionName().toAscii().constData());
    db.setDatabaseName("test.db3");
    if (!db.open())
    printf("Open failed\n");
    }
    @

    After reading the example of SQLDatabase::RemoveDatabase, I twigged that I could not do this:
    @
    void test_QT::on_closeDbButton_clicked(void)
    {
    QString conn;
    QSqlDatabase db = QSqlDatabase::database("test");
    conn=db.connectionName(); /* just checking! */
    if(db.isValid())
    {
    printf("Closing database %s\n", db.databaseName().toAscii().constData());
    db.close();
    }
    QSqlDatabase::removeDatabase("test");
    }
    @
    and had to move the QSqlDatabase::removeDatabase call to the open button, prior to the addDatabase call, as by this time the db object in the close button is out of scope and has been destroyed.
    It would make more sense to me to destroy the database connection as soon as the database is closed. Is there a way I can do this nicely?

    EDIT: how to escape double colon o?

    1 Reply Last reply
    0
    • W Offline
      W Offline
      webmaster.skelton
      wrote on last edited by
      #2

      Calling db.close destroys the connection("found here":http://doc.qt.nokia.com/latest/qsqldatabase.html#close). So when you call QSqlDatabase::removeDatabase you are trying to remove a connection that has already been removed. As far as the scope issue, you could always make db a member of the class itself( of course this comes with other issues that will need to be addressed).

      1 Reply Last reply
      0
      • K Offline
        K Offline
        kreyszig
        wrote on last edited by
        #3

        erm...the error message suggests that the connection is still in use, which should not be the case if the database using it has been closed, as far as I can tell from the docs. The point is qt seems to be complaining that it hasn't been removed properly, since it thinks something is still referencing it.
        I originally made db part of the class, and doing so means it doesn't go out of scope, doesn't get destroyed, and would not be able to be removed even in the open code.

        So, not sure any of what you just suggested was useful, but thanks for trying.

        1 Reply Last reply
        0
        • M Offline
          M Offline
          mabrand
          wrote on last edited by
          #4

          You call removeDatabase("test") while QSqlDatabase db still exists and it refers to "test". This causes the warning about the database being in use.

          1 Reply Last reply
          0
          • K Offline
            K Offline
            kreyszig
            wrote on last edited by
            #5

            I'm aware of the reason I am getting the warning...what is the best way around this?
            I know the QT example for removedatabase makes sure db doesn't exist by letting it go out of scope.
            My workaround does this by calling removedatabase before I open another connection, but I would like it to occur when I close the current one.
            Is there a way to remove db nicely before calling removedatabase in the same method? (freeing it directly doesn't seem very elegant)

            1 Reply Last reply
            0
            • M Offline
              M Offline
              mabrand
              wrote on last edited by
              #6

              You could use {} to limit the scope of QSqlDatabase db. For example:

              @void test_QT:<span class="smiley">:o</span>n_closeDbButton_clicked(void)
              {
              QString conn;
              {
              QSqlDatabase db = QSqlDatabase::database("test");
              conn=db.connectionName(); /* just checking! */
              if(db.isValid())
              {
              printf("Closing database %s\n", db.databaseName().toAscii().constData());
              db.close();
              }
              }
              QSqlDatabase::removeDatabase("test");
              }@

              1 Reply Last reply
              0
              • K Offline
                K Offline
                kreyszig
                wrote on last edited by
                #7

                lol..i can't believe i didn't think of that, it's exactly how it's done in the example.
                many thanks!

                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