Qt Forum

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

    Update: Forum Guidelines & Code of Conduct

    Unsolved Warning shown in QT. Should I be worried about it? Or can I choose to ignore it?

    General and Desktop
    2
    2
    43
    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.
    • B
      burrito1111 last edited by

      I have added a database connection in two of my windows in QT.

      login.cpp

      mydb = QSqlDatabase::addDatabase("QSQLITE");
          mydb.setDatabaseName("C:/Users/burr1to/Desktop/Kaizen/Kaizen/testdb.db");
          if (!mydb.open()){
              qDebug()<<"Db problem";
          } else {
              qDebug()<< "Success";
          }
      
          QString username = ui->username->text();
          QString password = ui->password->text();
          QSqlQuery qry;
      
          if(qry.exec("select * from user where username = '"+username+"' and password = '"+password+"'")){
              int count=0;
              while(qry.next()){
                  count++;
              }
              if (count==1){
                  qDebug()<< "Successful login";
                  qry.clear();
                  mydb.close();
                  hide();
                  p.show();
      
              }
              if (count<1){
                  qDebug()<< "NOT Successful login";
                  qry.clear();
                  mydb.close();
              }
          }
      

      planner.cpp

          db = QSqlDatabase::addDatabase("QSQLITE");
          db.setDatabaseName("C:/Users/burr1to/Desktop/Kaizen/Kaizen/testdb.db");
          if (!db.open()){
              qDebug()<< "Failed";
          } else {
              qDebug()<< "Success";
      
          }
          if (!db.open()){
              return;
          }
          QString what = ui->addPlan->text();
      
          QSqlQuery q;
          if (what == ""){
              qDebug()<< "MT";
          }
          else{
              q.prepare("insert into plan(plandet) values (:plan)");
              q.bindValue(":plan",what);
              if (q.exec()){
                  qDebug()<< "Ok";
              }}
      
      
      
          ui->addPlan->clear();
      

      So the 'planner' page opens after the 'login' page, and even though I have closed the database in login.cpp, a warning is shown in console.

      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.

      Should I be worried about this? And what should I do to not get this error? Is the error happening because I used the same DB connection twice?

      1 Reply Last reply Reply Quote 0
      • Christian Ehrlicher
        Christian Ehrlicher Lifetime Qt Champion last edited by

        You should worry about it.
        Don't open the database more than once and then use QSqlDatabase::database() to retrieve the db connection (if you really need it)

        Qt has to stay free or it will die.

        1 Reply Last reply Reply Quote 2
        • First post
          Last post