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 :-
{
db->setDatabaseName(*dbname);
db->setHostName(*host);
db->setUserName(*uname);
db->setPassword(*passwd);
db->setPort(3306);
return db;
}
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;
}
To copy to clipboard, switch view to plain text mode
now in a slot :-
void MyMainWindow::on_pushButtonLogin_clicked()
{
QString user
= ui
->lineEditUserName
->text
().
trimmed();
QString passwd
= ui
->lineEditPassword
->text
().
trimmed();
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();
delete db;
}
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;
}
To copy to clipboard, switch view to plain text mode
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.cbs login problem.jpg
Bookmarks