QSqlDatabasePrivate::addDatabase: duplicate connection name 'MyConnect', old connection removed.
-
Hello guys, I need to fix a problem
I just have created a program(login/register system) with QT Creator.
The register works perfectly but when I try to log-in it appears this problem and I cannot log-in.
And also it is like if the program didn't read the part of compare: if(usernameFromDB == username && passwordFromDB == password){
and the messagebox doesn't appear.This is the code
#include "mainwindow.h"
#include "ui_mainwindow.h"MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
ui->username->setPlaceholderText("Enter your username");
ui->password->setPlaceholderText("Enter your password");
ui->email->setPlaceholderText("Enter your email");
ui->telefono->setPlaceholderText("Enter your phone");
ui->usernamelogin->setPlaceholderText("Enter your username");
ui->passwordlogin->setPlaceholderText("Enter your password");}
MainWindow::~MainWindow()
{
delete ui;
}void MainWindow::on_registerBtn_clicked()
{// Connessione al Database database = QSqlDatabase::addDatabase("QMYSQL"); database.setHostName("localhost"); database.setUserName("root"); database.setPassword(""); database.setDatabaseName("register"); // Memorizza le informazioni if(database.open()){ QString username = ui->username->text(); QString password = ui->password->text(); QString email = ui->email->text(); QString telefono = ui->telefono->text(); // Query per memorizzare le informazioni nelle celle di db QSqlQuery qry; qry.prepare("INSERT INTO users (username,password,email,telefono) " "VALUES(:username, :password, :email, :telefono)"); qry.bindValue(":username", username); qry.bindValue(":password", password); qry.bindValue(":email", email); qry.bindValue(":telefono", telefono); if(qry.exec()){ QMessageBox::information(this, "Inserted", "Data inserted successfully"); }else{ QMessageBox::information(this, "Not Inserted", "Data inserted unsuccessfully"); } // Se la connessione non è riuscita }else{ QMessageBox::information(this, "Non connesso", "Il database non è riuscito a connettersi"); }
}
void MainWindow::on_loginBtn_clicked()
{QSqlDatabase db; // Connessione al Database db = QSqlDatabase::addDatabase("QMYSQL", "MyConnect"); db.setHostName("localhost"); db.setUserName("root"); db.setPassword(""); db.setDatabaseName("register"); QString username = ui->usernamelogin->text(); QString password = ui->passwordlogin->text(); if(db.open()){ QMessageBox::information(this, "Connesso", "Il database è connesso"); QSqlQuery query(QSqlDatabase::database("MyConnect")); query.prepare(QString("SELECT * FROM users WHERE username = :username AND password = :password")); query.bindValue(":username", username); query.bindValue(":password", password); if(!query.exec()){ QMessageBox::information(this, "Failed", "Query non inserita correttamente"); }else{ QMessageBox::information(this, "Connected", "Query controllata correttamente"); if(query.next()){ QString usernameFromDB = query.value(1).toString(); QString passwordFromDB = query.value(2).toString(); if(usernameFromDB == username && passwordFromDB == password){ QMessageBox::information(this, "Success", "Login success"); }else{ QMessageBox::information(this, "Failed", "Login failed!"); } } } }else{ QMessageBox::information(this, "Non connesso", "Il database non è riuscito a connettersi"); }
}
-
@killer7forte said in QSqlDatabasePrivate::addDatabase: duplicate connection name 'MyConnect', old connection removed.:
i have tried to write if(query.next()){ and while(query.next()) but it doesn't change anything
-
Hi and welcome to devnet,
@killer7forte said in QSqlDatabasePrivate::addDatabase: duplicate connection name 'MyConnect', old connection removed.:
database
Looks like you have a QSqlDatabase object as class member. That's the first problem. Don't do that, there's no need for that as QSqlDatabase already provides everything needed to manage these objects.
Second, you don't close the close the connection before opening a new one which is wrong.