Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QSqlDatabasePrivate::addDatabase: duplicate connection name 'MyConnection' error
QtWS25 Last Chance

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

Scheduled Pinned Locked Moved Solved General and Desktop
qtcreatorc++databaseruntime error
2 Posts 2 Posters 2.3k Views
  • 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 Offline
    L Offline
    Lasith
    wrote on last edited by kshegunov
    #1

    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]

    jsulmJ 1 Reply Last reply
    0
    • L Lasith

      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]

      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @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
      3

      • Login

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