Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QSqlDatabasePrivate::removeDatabase
Qt 6.11 is out! See what's new in the release blog

QSqlDatabasePrivate::removeDatabase

Scheduled Pinned Locked Moved General and Desktop
19 Posts 7 Posters 29.4k 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.
  • ? Offline
    ? Offline
    A Former User
    wrote on last edited by
    #9

    Here:

    @bool MysqlConnector2::close()
    {
    bool result=true;
    QString connection;
    connection=db.connectionName();
    db.close();

    QSqlDatabase::removeDatabase(connection);
    
    
    return result;
    

    }
    @

    1 Reply Last reply
    0
    • N Offline
      N Offline
      Niak74
      wrote on last edited by
      #10

      Got it, but where do you call MysqlConnector2::close() (outside this class).

      Does the error message appear when you close your app, or when you call MysqlConnector2::close() method ? (verify this point using debug mode and breakpoints !)

      I recommand you to make atomic methods that implie to open AND close connection to your DB. By this way, you will be sure release your resources.

      See the EDIT in my previous message.

      1 Reply Last reply
      0
      • ? Offline
        ? Offline
        A Former User
        wrote on last edited by
        #11

        Here i call the close():

        @ conn->close();
        logger->close();
        logger->logOnFile("MainButton","mousePressEvent","Exiting from program.");
        exit(0);
        @

        just before exiting...

        1 Reply Last reply
        0
        • L Offline
          L Offline
          luca
          wrote on last edited by
          #12

          I do this:
          @
          db->close();
          delete db;
          QSqlDatabase::removeDatabase(dbName);
          @

          1 Reply Last reply
          0
          • ? Offline
            ? Offline
            A Former User
            wrote on last edited by
            #13

            Thanks for the suggestion!

            Doing like this it works:

            @bool MysqlConnector2::close()
            {
            bool result=true;

            QString connection;
            connection=db.connectionName();
            db.close();
            delete &db;
            QSqlDatabase::removeDatabase(connection);
            
            
            return result;
            

            }@

            1 Reply Last reply
            0
            • ? Offline
              ? Offline
              A Former User
              wrote on last edited by
              #14

              delete &db is really aehm... ugly! You are calling the destructor on a stack-allocated object. This will lead to the destructor of db getting called twice, once when you do it and once when it goes out of scope. Undefined behaviour is lurking here... nothing I want close to my databases;-)

              You could hold a pointer to a db instead, initializing it like this db = new QSqlDatabase(QSqlDatabase::addDatabase(...));

              That pointer can be deleted properly in your close method.

              1 Reply Last reply
              0
              • ? Offline
                ? Offline
                A Former User
                wrote on last edited by
                #15

                You are right,

                that was only an attempt.

                Now i will modify the code to be less ugly..eheheh

                1 Reply Last reply
                0
                • ? Offline
                  ? Offline
                  A Former User
                  wrote on last edited by
                  #16

                  finally, how do you solve the problem?

                  1 Reply Last reply
                  0
                  • S Offline
                    S Offline
                    SaiyanRiku
                    wrote on last edited by
                    #17

                    Nice, it works for me too, just by declaring a pointer instead of an object and using it like this:

                    @
                    m_db = new QSqlDatabase(QSqlDatabase::addDatabase("QSQLITE"));
                    m_szConnectionName = m_db->connectionName();
                    ...
                    m_db->open();
                    ...
                    m_db->close();
                    ...
                    delete m_db
                    QSqlDatabase::removeDatabase(m_szConnectionName);
                    @

                    1 Reply Last reply
                    0
                    • S Offline
                      S Offline
                      SherifOmran
                      wrote on last edited by
                      #18

                      how do you define m_db?

                      1 Reply Last reply
                      0
                      • S Offline
                        S Offline
                        SherifOmran
                        wrote on last edited by
                        #19

                        ok i found it
                        QSqlDatabase *db;

                        thanks it worked for me

                        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
                        • Unsolved