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. connection 'qt_sql_default_connection' is still in use, all queries will cease to work.

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

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

    hi to all, I've qt 5.7 in centos7. I am facing problem of connection "'qt_sql_default_connection' is still in use, all queries will cease to work." When i start program it errors like :

    QSqlDatabasePrivate::removeDatabase: connection 'qt_sql_default_connection' is still in use, all queries will cease to work. QSqlDatabasePrivate::addDatabase: duplicate connection name 'qt_sql_default_connection', old connection removed.
    

    mine starting code is :-

    #include "dialoglogin.h"
    #include "ui_dialoglogin.h"
    
    DialogLogin::DialogLogin(QWidget *parent) :
        QDialog(parent),
        ui(new Ui::DialogLogin)
    {
        ui->setupUi(this);
        //QString database = "cbsdb";
        db = new QSqlDatabase(QSqlDatabase::addDatabase("QMYSQL"));//, "cbsdb"));
        db->setDatabaseName("cbs");
        db->setHostName("serverora.db.net");
        db->setUserName("rahul");
        db->setPassword("rahul");
        db->setPort(3306);
        db->setConnectOptions();
        dlgmenu = new DialogMenu(this);
        query = new QSqlQuery;
    }
    
    DialogLogin::~DialogLogin()
    {
        QSqlDatabase::database().close();
        delete db;
        delete ui;
    }
    
    void DialogLogin::on_pushButtonCancel_clicked()
    {
    
        exit(0);
    }
    
    void DialogLogin::on_pushButtonLogin_clicked()
    {
        if(db->open())
        {
            QMessageBox::information(this, "inside DialogLogin::db->open ","inside db->open");
            sql = "select User from tableLogin where User = ? and Password = ?";
    
            QString user = ui->lineEditUserName->text().trimmed();
            QString password = ui->lineEditPassword->text().trimmed();
            if(!query->prepare(sql))
            {
                QMessageBox::information(this, "inside DialogLogin::query->prepare", "Error : " + query->lastError().text());
                return;
            }
    
            query->bindValue(0, user);
            query->bindValue(1, password);
    
            if(query->exec())
            {
                if(query->next())
                {
                    if(QString::compare(user, query->value(0).toString().trimmed()) == 0) // && ( QString::compare(password, query->value(1).toString().trimmed()) == 0) )
                    {
                        this->close();
                        dlgmenu->exec();                    
                    }
                    else
                    {
                        QMessageBox::information(this, "Login Form", "user name or password is incorrect");
                    }
                }
                else
                {
                    QMessageBox::information(this, "Login Form", "query.next() is failed : " + query->lastError().text());
                }
            }
            else
            {
                QMessageBox::information(this, "Login Form", " Error in login " + query->lastError().text());
            }
        }
        else
        {
            QMessageBox::information(this,"cbs LoginForm", "database cannot be openned : " + db->lastError().text());
        }
    }
    

    error is :-

    jsulmJ 1 Reply Last reply
    0
    • R rahulvishwakarma

      hi to all, I've qt 5.7 in centos7. I am facing problem of connection "'qt_sql_default_connection' is still in use, all queries will cease to work." When i start program it errors like :

      QSqlDatabasePrivate::removeDatabase: connection 'qt_sql_default_connection' is still in use, all queries will cease to work. QSqlDatabasePrivate::addDatabase: duplicate connection name 'qt_sql_default_connection', old connection removed.
      

      mine starting code is :-

      #include "dialoglogin.h"
      #include "ui_dialoglogin.h"
      
      DialogLogin::DialogLogin(QWidget *parent) :
          QDialog(parent),
          ui(new Ui::DialogLogin)
      {
          ui->setupUi(this);
          //QString database = "cbsdb";
          db = new QSqlDatabase(QSqlDatabase::addDatabase("QMYSQL"));//, "cbsdb"));
          db->setDatabaseName("cbs");
          db->setHostName("serverora.db.net");
          db->setUserName("rahul");
          db->setPassword("rahul");
          db->setPort(3306);
          db->setConnectOptions();
          dlgmenu = new DialogMenu(this);
          query = new QSqlQuery;
      }
      
      DialogLogin::~DialogLogin()
      {
          QSqlDatabase::database().close();
          delete db;
          delete ui;
      }
      
      void DialogLogin::on_pushButtonCancel_clicked()
      {
      
          exit(0);
      }
      
      void DialogLogin::on_pushButtonLogin_clicked()
      {
          if(db->open())
          {
              QMessageBox::information(this, "inside DialogLogin::db->open ","inside db->open");
              sql = "select User from tableLogin where User = ? and Password = ?";
      
              QString user = ui->lineEditUserName->text().trimmed();
              QString password = ui->lineEditPassword->text().trimmed();
              if(!query->prepare(sql))
              {
                  QMessageBox::information(this, "inside DialogLogin::query->prepare", "Error : " + query->lastError().text());
                  return;
              }
      
              query->bindValue(0, user);
              query->bindValue(1, password);
      
              if(query->exec())
              {
                  if(query->next())
                  {
                      if(QString::compare(user, query->value(0).toString().trimmed()) == 0) // && ( QString::compare(password, query->value(1).toString().trimmed()) == 0) )
                      {
                          this->close();
                          dlgmenu->exec();                    
                      }
                      else
                      {
                          QMessageBox::information(this, "Login Form", "user name or password is incorrect");
                      }
                  }
                  else
                  {
                      QMessageBox::information(this, "Login Form", "query.next() is failed : " + query->lastError().text());
                  }
              }
              else
              {
                  QMessageBox::information(this, "Login Form", " Error in login " + query->lastError().text());
              }
          }
          else
          {
              QMessageBox::information(this,"cbs LoginForm", "database cannot be openned : " + db->lastError().text());
          }
      }
      

      error is :-

      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by jsulm
      #2

      @rahulvishwakarma said in connection 'qt_sql_default_connection' is still in use, all queries will cease to work.:

      db = new QSqlDatabase(QSqlDatabase::addDatabase("QMYSQL"));//, "cbsdb"));

      Why do you have this db variable? Please check documentation to see how to properly use https://doc.qt.io/qt-5/qsqldatabase.html
      Also, it does not make sense to create a new connection and then second one and use the first one as parameter! You're calling copy constructor this way...

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      4
      • R Offline
        R Offline
        rahulvishwakarma
        wrote on last edited by
        #3

        because db is pointer so i have to do this
        db = new QSqlDatabase(QSqlDatabase::addDatabase("QMYSQL"));

        after that i use this to connect database with front end.

        jsulmJ 1 Reply Last reply
        0
        • R rahulvishwakarma

          because db is pointer so i have to do this
          db = new QSqlDatabase(QSqlDatabase::addDatabase("QMYSQL"));

          after that i use this to connect database with front end.

          jsulmJ Offline
          jsulmJ Offline
          jsulm
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @rahulvishwakarma said in connection 'qt_sql_default_connection' is still in use, all queries will cease to work.:

          so i have to do this

          No, you don't!
          Please read documentation!
          There is no need to create a QSqlDatabase instance manually - that is what addDatabase() does for you...

          https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          3
          • R Offline
            R Offline
            rahulvishwakarma
            wrote on last edited by
            #5

            can you please tell me by example how to do it. Actually i have another mini project that accepts this.

            jsulmJ JonBJ 2 Replies Last reply
            0
            • R rahulvishwakarma

              can you please tell me by example how to do it. Actually i have another mini project that accepts this.

              jsulmJ Offline
              jsulmJ Offline
              jsulm
              Lifetime Qt Champion
              wrote on last edited by
              #6

              @rahulvishwakarma There is an example in the documentation I pointed you to

              https://forum.qt.io/topic/113070/qt-code-of-conduct

              1 Reply Last reply
              2
              • R rahulvishwakarma

                can you please tell me by example how to do it. Actually i have another mini project that accepts this.

                JonBJ Offline
                JonBJ Offline
                JonB
                wrote on last edited by
                #7

                @rahulvishwakarma
                As @jsulm pointed you to, the very first example at https://doc.qt.io/qt-5/qsqldatabase.html#details shows you what you are supposed to do.

                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