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. How to nicely remove a database

How to nicely remove a database

Scheduled Pinned Locked Moved Solved General and Desktop
7 Posts 4 Posters 4.4k Views 2 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.
  • M Offline
    M Offline
    Mark81
    wrote on last edited by
    #1

    From the docs:

    [static] void QSqlDatabase::removeDatabase(const QString &connectionName)
    Removes the database connection connectionName from the list of database connections.
    Warning: There should be no open queries on the database connection when this function is called, otherwise a resource leak will occur.
    

    In my code I create a default connection to a database:

    QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
    db.setDatabaseName("db.sqlite");
    db.open();
    

    then in several functions I use queries, for example:

    void DatabaseManager::addListItem(QString table, QString name)
    {
        QSqlQuery query;
        query.prepare("INSERT INTO " + table + " (name) values(?)");
        query.addBindValue(name);
        query.exec();
    }
    

    when I'm done I want to remove the database connection:

    QSqlDatabase db = QSqlDatabase::database();
    db.close();
    QSqlDatabase::removeDatabase(db.connectionName());
    

    But I still receive the warning:

    QSqlDatabasePrivate::removeDatabase: connection 'qt_sql_default_connection' is still in use, all queries will cease to work.
    

    I don't understand why. The close method should free anything:

    Closes the database connection, freeing any resources acquired, and invalidating any existing QSqlQuery objects that are used with the database.
    

    Furthermore, because my queries are declared in different functions, the QSqlQuery object should be destroyed when exiting from the functions.

    Where's my mistake?

    1 Reply Last reply
    0
    • Christian EhrlicherC Offline
      Christian EhrlicherC Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by
      #2

      You must have a QSqlQuery somewhere around which is still using the connection. Maybe a QSqlTableModel or a QSqlQuery as a member variable.

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      1 Reply Last reply
      2
      • SGaistS Offline
        SGaistS Offline
        SGaist
        Lifetime Qt Champion
        wrote on last edited by SGaist
        #3

        That’s because you have an active QSqlDatabase object which is db.

        See the removeDatabase documentation for the technique to use.

        Interested in AI ? www.idiap.ch
        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

        M 1 Reply Last reply
        3
        • SGaistS SGaist

          That’s because you have an active QSqlDatabase object which is db.

          See the removeDatabase documentation for the technique to use.

          M Offline
          M Offline
          Mark81
          wrote on last edited by
          #4

          @SGaist I saw that note, but I misunderstood it. Fixed now.

          Thank YouT 1 Reply Last reply
          0
          • M Mark81

            @SGaist I saw that note, but I misunderstood it. Fixed now.

            Thank YouT Offline
            Thank YouT Offline
            Thank You
            wrote on last edited by
            #5

            @Mark81 Then please share code with us

            Let's make QT free or It will go forever

            TRUE AND FALSE <3

            1 Reply Last reply
            0
            • SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #6

              @Thank-You It's in the documentation I linked to.

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              Thank YouT 1 Reply Last reply
              0
              • SGaistS SGaist

                @Thank-You It's in the documentation I linked to.

                Thank YouT Offline
                Thank YouT Offline
                Thank You
                wrote on last edited by
                #7

                @SGaist Yes I got it

                Let's make QT free or It will go forever

                TRUE AND FALSE <3

                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