PDA

View Full Version : duplicate connection name 'qt_sql_default_connection'



rahulvishwakarma
18th March 2020, 14:00
hi to all, I've centos 7.5 in VM, and having Qt5.7 in same VM.

I am trying to build some program, in this a class connDB having static method as follows :-


QSqlDatabase * connDB::connectDB(QSqlDatabase *db, QString *uname, QString *passwd, QString *dbname, QString *host )
{
db = new QSqlDatabase(QSqlDatabase::addDatabase("QMYSQL"));
db->setDatabaseName(*dbname);
db->setHostName(*host);
db->setUserName(*uname);
db->setPassword(*passwd);
db->setPort(3306);

return db;
}


now in a slot :-


void MyMainWindow::on_pushButtonLogin_clicked()
{
QString user = ui->lineEditUserName->text().trimmed();
QString passwd = ui->lineEditPassword->text().trimmed();
QString dbname = "cbs";
QString host = "serverora11gr2.db.net";

db = connDB::connectDB(db, &user, &passwd, &dbname, &host);

if(db->open())
{
QMessageBox::information(this, "Login", " Connection Succeeded");
}
else
{
QMessageBox::warning(this, "Login", "Coneection failure : " + db->lastError().text());
}

db->close();
QSqlDatabase::removeDatabase(dbname);
delete db;
}

when I tried to to run it is successfully runs but when i press login button more it shows following
QSqlDatabasePrivate::addDatabase: duplicate connection name 'qt_sql_default_connection', old connection removed.

how to overcome this problem.13356

d_stranz
18th March 2020, 14:41
how to overcome this problem.

Read what the error message is telling you. It's a duplicate connection. Then look at your slot code and think about what it is doing. At a minimum, you should check to see if the DB is already open before trying to connect, not to mention checking to see if the pointer that is returned is non-null before trying to use it.

rahulvishwakarma
18th March 2020, 15:53
i foud solution :-


void MyMainWindow::on_pushButtonLogin_clicked()
{
QString user = ui->lineEditUserName->text().trimmed();
QString passwd = ui->lineEditPassword->text().trimmed();
QString dbname = "cbs";
QString host = "serverora11gr2.db.net";



db = connDB::connectDB(db, &user, &passwd, &dbname, &host);

if(db->open())
{
QMessageBox::information(this, "Login", " Connection Succeeded");
}
else
{
QMessageBox::warning(this, "Login", "Coneection failure : " + db->lastError().text());
}

db->close(); // added in this line this
delete db; // added in this line this
QSqlDatabase::removeDatabase(dbname); // added in this line this
}

d_stranz
18th March 2020, 16:39
Don't just add random code, see that it doesn't work, and then expect people here to solve the problem for you. Read the error messages. If the DB is "still in use" it probably means you haven't closed the DB before trying to remove the connection.

And if you are connecting to the same database each time, why would you want to close or remove it? Keep the connection until you are really done with it, don't try to connect if it is already connected, and don't remove the connection if you will use it again. All you are doing is making your program less responsive because of the repeated connections and disconnections.