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. Close database .db. connection ’info.db’ is still in use, all queries will cease to work.
Forum Update on Monday, May 27th 2025

Close database .db. connection ’info.db’ is still in use, all queries will cease to work.

Scheduled Pinned Locked Moved General and Desktop
12 Posts 5 Posters 14.4k 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.
  • C Offline
    C Offline
    chikymanzanares
    wrote on last edited by
    #1

    Hi, I'm new

    I want to delete a sqlite database(.db file) in my aplication. For
    this, I delete the file but, before, I have to close the database, but
    I can't
    @|
    QSqlDatabase::removeDatabase('info.db');
    QFile fil('/info.db');
    fil.remove(nombreArchivo);
    @
    I see the error

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

    I don't know how to close

    [Edit : please use @ code tags around your code, Eddy]

    1 Reply Last reply
    0
    • EddyE Offline
      EddyE Offline
      Eddy
      wrote on last edited by
      #2

      sounds like you have to clean up a pointer to your database connection.

      Can you show us the relevant code you use to make the database connection?

      Qt Certified Specialist
      www.edalsolutions.be

      1 Reply Last reply
      0
      • EddyE Offline
        EddyE Offline
        Eddy
        wrote on last edited by
        #3

        have a look at "this thread":http://developer.qt.nokia.com/forums/viewthread/328/#58084.

        I think it's similar to yours.

        Qt Certified Specialist
        www.edalsolutions.be

        1 Reply Last reply
        0
        • G Offline
          G Offline
          goetz
          wrote on last edited by
          #4

          At least you should close the db before removing it:

          @
          QSqlDatabase db = QSqlDatabase::database("info.db");
          db.close();
          QSqlDatabase::removeDatabase("info.db");
          @

          And make sure, you do not have any QSqlQueries open at that time, as those are invalid then.

          You might want to check the error results of the various objects.

          Your code to remove the file looks fishy too. You instantiate a file with a path, but call a static method with another file path to actually remov the file.

          http://www.catb.org/~esr/faqs/smart-questions.html

          1 Reply Last reply
          0
          • A Offline
            A Offline
            andre
            wrote on last edited by
            #5

            This error is actually quite hard to avoid. You will have to make sure that every query using the database goes out of scope before you try to close the database. That is very inconvenient, but that is the way it is. I usually try to explicitly scope QSqlQuery objects, so that I have control over their life time. So, in stead of:

            @
            double calculateCostOfRaise(double percentage) {
            QSqlDatabase db(...);

            double totalCost(0.0);
            double factor(percentage/100.0);

            QSqlQuery query = QSqlQuery(db, "SELECT * FROM Employees");
            int fieldNo = query.record().indexOf("salary");
            while (query.next()) {
            totalCost += factor * query.value(fieldNo).toDouble();
            }

            db.removeDatabase(...);
            return totalCost;
            }
            @

            I usually do something like:

            @
            double calculateCostOfRaise(double percentage) {
            QSqlDatabase db(...);

            double totalCost(0.0);
            double factor(percentage/100.0);

            { //explicit scope around QSqlQuery usuage
            QSqlQuery query = QSqlQuery(db, "SELECT * FROM Employees");
            int fieldNo = query.record().indexOf("salary");
            while (query.next()) {
            totalCost += factor * query.value(fieldNo).toDouble();
            }
            } // QSqlQuery is out of scope here, so before the database delete

            db.removeDatabase(...);

            return totalCost;
            }
            @

            (And yes, I know that the above operation can be done more efficiently, that is not the point.)

            1 Reply Last reply
            0
            • G Offline
              G Offline
              goetz
              wrote on last edited by
              #6

              Shouldn't "QSqlQuery::clear() ":http://doc.qt.nokia.com/4.7/qsqlquery.html#clear do the trick?

              http://www.catb.org/~esr/faqs/smart-questions.html

              1 Reply Last reply
              0
              • A Offline
                A Offline
                andre
                wrote on last edited by
                #7

                Perhaps it does, yes.

                1 Reply Last reply
                0
                • G Offline
                  G Offline
                  goetz
                  wrote on last edited by
                  #8

                  Just tested, it, doesn't seem so. A suggestion for a QSqlQuery::close() would be ok then. I'll have to think over it.

                  http://www.catb.org/~esr/faqs/smart-questions.html

                  1 Reply Last reply
                  0
                  • A Offline
                    A Offline
                    andre
                    wrote on last edited by
                    #9

                    Perhaps the warning should just be removed. It sounds very obvious to me that if you close a database, queries on that database do not work anymore. I usually ran into this while trying to exit and cleanup (a module in) my application. It would have been useful if QSqlDatabase would have been a class where you would have actually have to keep an instance around to represent the database, and the database would have automatically closed if that instance was deleted or something like that. But that is not the case, any database removal is explicit. That makes a warning at that point useless IMHO.

                    1 Reply Last reply
                    0
                    • C Offline
                      C Offline
                      chikymanzanares
                      wrote on last edited by
                      #10

                      This is my conection

                      @
                      base = QSqlDatabase::addDatabase("QSQLITE","info.db");
                      base.setHostName("/" + "info.db");
                      base.setDatabaseName("/" + "info.db");
                      base.setUserName("root");
                      @

                      I use QSqlQuery::clear() for every QSqlQuery, but the error persist

                      [EDIT: code formatting, please wrap in @-tags, Volker]

                      1 Reply Last reply
                      0
                      • D Offline
                        D Offline
                        dangelog
                        wrote on last edited by
                        #11

                        [quote author="Andre" date="1317547945"]Perhaps the warning should just be removed. It sounds very obvious to me that if you close a database, queries on that database do not work anymore. I usually ran into this while trying to exit and cleanup (a module in) my application. It would have been useful if QSqlDatabase would have been a class where you would have actually have to keep an instance around to represent the database, and the database would have automatically closed if that instance was deleted or something like that. But that is not the case, any database removal is explicit. That makes a warning at that point useless IMHO. [/quote]

                        That would had made the design different, but not solved this particular issue.

                        All in all, it's just a warning: you have active queries / cursors / models / records / etc. that are still using the database when you try to close it. You can ignore it, but it's a better idea to go around and clear / destroy them first.

                        Software Engineer
                        KDAB (UK) Ltd., a KDAB Group company

                        1 Reply Last reply
                        0
                        • G Offline
                          G Offline
                          goetz
                          wrote on last edited by
                          #12

                          I usually like the warnings, the can give a hint to a possible design flaw leading to hard to debug errors.

                          http://www.catb.org/~esr/faqs/smart-questions.html

                          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