Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. QSqlDatabasePrivate::addDatabase: duplicate connection name 'MyConnect', old connection removed.
Qt 6.11 is out! See what's new in the release blog

QSqlDatabasePrivate::addDatabase: duplicate connection name 'MyConnect', old connection removed.

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
3 Posts 2 Posters 1.4k Views 1 Watching
  • 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.
  • K Offline
    K Offline
    killer7forte
    wrote on last edited by
    #1

    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");
    }
    

    }

    K 1 Reply Last reply
    0
    • K killer7forte

      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");
      }
      

      }

      K Offline
      K Offline
      killer7forte
      wrote on last edited by
      #2

      @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

      1 Reply Last reply
      0
      • SGaistS Offline
        SGaistS Offline
        SGaist
        Lifetime Qt Champion
        wrote on last edited by
        #3

        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.

        Interested in AI ? www.idiap.ch
        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

        1 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved