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. QSqlDatabasePrivate::removeDatabase
Forum Updated to NodeBB v4.3 + New Features

QSqlDatabasePrivate::removeDatabase

Scheduled Pinned Locked Moved General and Desktop
19 Posts 7 Posters 28.0k 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.
  • R Offline
    R Offline
    rileo8
    wrote on last edited by
    #1

    Hi,

    how can i remove this warning message?

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

    1 Reply Last reply
    0
    • T Offline
      T Offline
      tobias.hunger
      wrote on last edited by
      #2

      Just going by the message: Disconnect from the database before trying to do whatever is finally triggering the warning.

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

        I do like this:

        when exiting:

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

        @
        and the conn->close is here:

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

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

        }
        @

        so to me it seems correct.....

        what the problem here?

        Thanks!

        1 Reply Last reply
        0
        • T Offline
          T Offline
          tobias.hunger
          wrote on last edited by
          #4

          Hmmm... I never used our SQL classes... but ...

          the documentation on connectionName says:

          bq. Returns the connection name, which may be empty. Note: The connection name is not the database name.

          So I think this is definitely not what you want.

          I think you need to get the database name, close the connection to the database (db.close()) and only afterwards remove the database.

          1 Reply Last reply
          0
          • R Offline
            R Offline
            rileo8
            wrote on last edited by
            #5

            I did this:

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

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

            }@

            but the warning is still here....

            1 Reply Last reply
            0
            • T Offline
              T Offline
              tobias.hunger
              wrote on last edited by
              #6

              Looks like I have been wrong then:-( Try reading the documentation on

              @void QSqlDatabase::removeDatabase ( const QString & connectionName )@

              The example there does exactly match your situation from what I see.

              1 Reply Last reply
              0
              • R Offline
                R Offline
                rileo8
                wrote on last edited by
                #7

                I read the documentiation but...i don't understand how to apply the example to my situation...

                here is my class .cpp

                @#include "mysqlconnector2.h"

                MysqlConnector2::MysqlConnector2()
                {

                }

                bool MysqlConnector2::open()
                {
                bool result=false;

                {
                db=QSqlDatabase::addDatabase("QMYSQL");
                db.setHostName("localhost");
                db.setDatabaseName("osty");
                db.setUserName("root");
                db.setPassword("root");

                if(db.open())
                {
                result=true;
                }

                }
                return result;
                }

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

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

                }

                QList < QHash <QString,QString> > MysqlConnector2::query_with_results(QString query)
                {
                QList < QHash <QString,QString> > table;

                int i;
                if(db.open())
                {

                  QSqlQuery qr=QSqlQuery(db);
                  qr.exec&#40;query&#41;;
                
                  QSqlRecord rec = qr.record();
                
                  int columns=rec.count();
                
                  while(qr.next())
                  {
                    QHash <QString,QString> row;
                    i=0;
                    for(i=0;i<columns;i++)
                    {
                      // std::cout<<"NUMBER: "<<qr.value(i).toDouble()<<std::endl;
                       // seems that qr.value(i).toString() is language dependent...
                       row[rec.fieldName(i)]=qr.value(i).toString();//.replace(",",".");
                      // std::cout<<"VALUE : "<<row[rec.fieldName(i)].toStdString()<<std::endl;
                    }
                
                    table<<row;
                  }
                

                }

                return table;
                }

                QString MysqlConnector2::query(QString query)
                {
                QString result="";

                if(db.open())
                {
                QSqlQuery qr=QSqlQuery(db);
                if(!qr.exec(query))
                {
                result=qr.lastError().text();
                }

                }
                return result;
                }@

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

                  Where do you call close() ?

                  Did you write any destructor for this class ?

                  EDIT : My way to do :

                  @
                  MyClass::MyClass()
                  {
                  m_db = QSqlDatabase::addDatabase("QMYSQL");
                  [...]
                  }

                  MyClass::~MyClass
                  {
                  QSqlDatabase::removeDatabase("QMYSQL");
                  }

                  Type MyClass::aFunctionThatUsesDB(args)
                  {
                  if(m_db.open()) {
                  [My stuff...]
                  m_db.close();
                  }
                  else { displayError(); }
                  }
                  @

                  1 Reply Last reply
                  0
                  • R Offline
                    R Offline
                    rileo8
                    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
                      • R Offline
                        R Offline
                        rileo8
                        wrote on last edited by
                        #11

                        Here i call the close():

                        @ conn->close();
                        logger->close();
                        logger->logOnFile("MainButton","mousePressEvent","Exiting from program."&#41;;
                        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
                          • R Offline
                            R Offline
                            rileo8
                            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 &amp;db;
                            QSqlDatabase::removeDatabase(connection);
                            
                            
                            return result;
                            

                            }@

                            1 Reply Last reply
                            0
                            • T Offline
                              T Offline
                              tobias.hunger
                              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
                              • R Offline
                                R Offline
                                rileo8
                                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
                                • C Offline
                                  C Offline
                                  chikymanzanares
                                  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 Extensions
                                        • Unsolved