Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Update: Forum Guidelines & Code of Conduct

    Solved QSqlDatabasePrivate::addDatabase: duplicate connection name 'MyConnection' error

    General and Desktop
    qtcreator c++ database runtime error
    2
    2
    1845
    Loading More Posts
    • 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.
    • L
      Lasith last edited by kshegunov

      I have written a Qt program in c++ inorder to access a database and load database tables to table view! My program does not show any compile errors but gives 2 runtime errors

      QSqlDatabasePrivate::removeDatabase: connection 'MyConnection' is still in use, all queries will cease to work.
      QSqlDatabasePrivate::addDatabase: duplicate connection name 'MyConnection', old connection removed.
      

      There are mainly to classes namely MainWindow(Has database connection methods) and Dialog Following are my codes

      MainWindow.h

      class MainWindow : public QMainWindow
      {
          Q_OBJECT
      
      public:
          explicit MainWindow(QWidget *parent = 0);
          ~MainWindow();
      
      public: QSqlDatabase mydb;
              bool conOpen(QString userName,QString password,QString hostname,int port,QString service){
                  mydb=QSqlDatabase::addDatabase("QOCI","MyConnection");
                  mydb.setUserName(userName);
                  mydb.setPassword(password);
                  mydb.setHostName(hostname);
                  mydb.setPort(port);
                  mydb.setDatabaseName(service);
                  return mydb.open();
      
      }
      

      MainWindow.cpp

      void MainWindow::on_pushButton_clicked()
      {
          Dialog *dialog1 = new Dialog(this);
         if(conOpen(ui->uname->text(),ui->pword->text(),ui->ip->text(),ui->port->text().toInt(),ui->service->text())){
      
              dialog1->show();
      }
      

      Dialog.h

      class Dialog : public QDialog
      {
          Q_OBJECT
      
      public:
          explicit Dialog(QWidget *parent = 0);
          ~Dialog();
      
      private slots:
          void on_pushButton_clicked();
      

      Dialog.cpp

      void Dialog::on_pushButton_clicked()
      {
      
          QSqlQueryModel *modal = new QSqlQueryModel();
          if(QSqlDatabase::contains("MyConnection")){
      
              QSqlQuery* qry=new QSqlQuery(QSqlDatabase::database("MyConnection"));
              qry->prepare("select NAME FROM Employees");
                 qry->exec();
              modal->setQuery(*qry);
            ui->tableView->setModel(modal);
      
          }
      }
      

      The MainWindow form is used for login and Dialog form is used to retrieve entries in database table to a table view How can I correct the issue?

      Please take care to use the code tags where appropriate when making a post ~kshegunov

      [Added code tags ~kshegunov]

      jsulm 1 Reply Last reply Reply Quote 0
      • jsulm
        jsulm Lifetime Qt Champion @Lasith last edited by

        @Lasith You should open the database only once. Currently you do it each time button is pressed.

        https://forum.qt.io/topic/113070/qt-code-of-conduct

        1 Reply Last reply Reply Quote 3
        • First post
          Last post