Warning shown in QT. Should I be worried about it? Or can I choose to ignore it?
-
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?
-
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)