Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Update: Forum Guidelines & Code of Conduct

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

    General and Desktop
    3
    7
    1041
    Loading More Posts
    • 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
      rahulvishwakarma last edited by

      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 :-

      jsulm 1 Reply Last reply Reply Quote 0
      • jsulm
        jsulm Lifetime Qt Champion @rahulvishwakarma last edited by jsulm

        @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 Reply Quote 4
        • R
          rahulvishwakarma last edited by

          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.

          jsulm 1 Reply Last reply Reply Quote 0
          • jsulm
            jsulm Lifetime Qt Champion @rahulvishwakarma last edited by

            @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 Reply Quote 3
            • R
              rahulvishwakarma last edited by

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

              jsulm JonB 2 Replies Last reply Reply Quote 0
              • jsulm
                jsulm Lifetime Qt Champion @rahulvishwakarma last edited by

                @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 Reply Quote 2
                • JonB
                  JonB @rahulvishwakarma last edited by

                  @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 Reply Quote 0
                  • First post
                    Last post