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. Why is a duplicate database connection message displayed.
Forum Updated to NodeBB v4.3 + New Features

Why is a duplicate database connection message displayed.

Scheduled Pinned Locked Moved Solved General and Desktop
8 Posts 3 Posters 429 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.
  • lincolnL Offline
    lincolnL Offline
    lincoln
    wrote on last edited by lincoln
    #1

    I have a connection to a PostgreSQL db, to show information, what happens is that the main form is shown, the message "duplicate connection name" is shown, that is also shown in the other forms that I open.
    I have my connection in a class and in a getConnection () method.

    class for connection to the DB:

    #ifndef DBCONECTION_H
    #define DBCONECTION_H
    #include <QSqlDatabase>
    #include <QSqlError>
    
    class DbConection
    {
    public:
      DbConection();
      QSqlDatabase getConection();
      QString errorMessage(){return _errorMessage;}
    
    private:
      QSqlDatabase db;
      QString _errorMessage;
    
    };
    
    #endif // DBCONECTION_H
    
    
    #include "dbconection.h"
    
    DbConection::DbConection()
    {
    
    }
    
    QSqlDatabase DbConection::getConection()
    {
      db=QSqlDatabase::addDatabase("QPSQL");
      db.setDatabaseName("monitoreo_db");
      db.setHostName("localhost");
      db.setPassword("1234567");
      db.setPort(5432);
      db.setUserName("postgres");
      if(!db.open()){
        _errorMessage=db.lastError().databaseText();
        return db;
      }
    
      return db;
    
    }
    
    

    Main window

    
    #include "mainwindow.h"
    #include "./ui_mainwindow.h"
    #include <QMessageBox>
    #include <QDebug>
    #include <QSqlError>
    #include <QSqlQueryModel>
    #include <QStandardPaths>
    #include "dbconection.h"
    
    
    MainWindow::MainWindow(QWidget *parent)
      : QMainWindow(parent), ui(new Ui::MainWindow)
    {
        ui->setupUi(this);
        DbConection db;
        db.getConection();
    }
    
    void MainWindow::dataEstMonitoreo(int nro)
    {
      
      QSqlQuery qry;
      qry.prepare("SELECT codigo_estacion,fecha_muestra,hora_muestra,descripcion FROM customer WHERE nro=?");
      
      qry.addBindValue(nro);
    
      //  qry.addBindValue(fecha);
      if(!qry.exec()){
        qDebug()<<qry.lastError().text();
        qDebug()<<qry.lastError().nativeErrorCode();
        return;
      }
      qry.next();
      ui->txtCod.setText(qry.value(0).toString());
      ui->deDate.setDate(qry.value(1).toDate());
      ui->teTime.setTime(qry.value(2).toTime());
      ui->txtDesc.setText(qry.value(3).toString());
      
    }
    void MainWindow::on_btnFoto2_clicked()
    {
        dataEstMonitoreo(25);
    }
    

    this is the message that is displayed, every time I open a window.
    What would be the correct way to make the connection so that these messages do not appear, since I consider that it is not right for those messages to be displayed.

    edf28035-85b1-48b0-b408-64392c811cfa-image.png

    Solitary wolf

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

      You are re-creating the exact same database connection each time you call that function.

      There's no need for that.

      Create it once and then use it.
      You can open and close it if you want but there's no need to recreate it each time.

      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
      1
      • SGaistS Offline
        SGaistS Offline
        SGaist
        Lifetime Qt Champion
        wrote on last edited by
        #2

        Hi,

        @lincoln said in Why is a duplicate database connection message displayed.:

        private:
        QSqlDatabase db;

        Because of that.

        The documentation of the class explicitly says to not keep class member variables of the QSqlDatabase.

        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
        • lincolnL Offline
          lincolnL Offline
          lincoln
          wrote on last edited by
          #3

          I did this but the message is still displayed.

          QSqlDatabase DbConection::getConection()
          {
          
            QSqlDatabase db=QSqlDatabase::addDatabase("QPSQL");
            db.setDatabaseName("monitoreo_db");
            db.setHostName("localhost");
            db.setPassword("2311046");
            db.setPort(5432);
            db.setUserName("postgres");
            if(!db.open()){
              _errorMessage=db.lastError().databaseText();
              return db;
            }
          
          
          
            return db;
          
          }
          

          Solitary wolf

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

            You are re-creating the exact same database connection each time you call that function.

            There's no need for that.

            Create it once and then use it.
            You can open and close it if you want but there's no need to recreate it each time.

            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
            1
            • lincolnL Offline
              lincolnL Offline
              lincoln
              wrote on last edited by
              #5

              So I stay and the annoying message no longer comes out, thank you.

              #include "dbconection.h"
              QSqlDatabase db=QSqlDatabase::addDatabase("QPSQL");
              DbConection::DbConection()
              {
              
              }
              
              QSqlDatabase DbConection::getConection()
              {
                db.setDatabaseName("monitoreo_db");
                db.setHostName("localhost");
                db.setPassword("2311046");
                db.setPort(5432);
                db.setUserName("postgres");
                if(!db.open()){
                  _errorMessage=db.lastError().databaseText();
                  return db;
                }
              
                return db;
              

              }

              Solitary wolf

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

                That's one of the worst solution.

                There's no reason to make a static variable.

                There's QSqlDatabase::database to retrieve the connection.

                In the absolute, your wrapper class is not really needed.

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

                lincolnL 1 Reply Last reply
                1
                • SGaistS SGaist

                  That's one of the worst solution.

                  There's no reason to make a static variable.

                  There's QSqlDatabase::database to retrieve the connection.

                  In the absolute, your wrapper class is not really needed.

                  lincolnL Offline
                  lincolnL Offline
                  lincoln
                  wrote on last edited by
                  #7

                  @SGaist Well then, I am not an expert in this Qt, but well now I have more doubts than before. How it should make the connection to the database correctly.

                  Solitary wolf

                  jsulmJ 1 Reply Last reply
                  0
                  • lincolnL lincoln

                    @SGaist Well then, I am not an expert in this Qt, but well now I have more doubts than before. How it should make the connection to the database correctly.

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

                    @lincoln Like shown and explained in the documentation: https://doc.qt.io/qt-5/qsqldatabase.html
                    You create the connection once (for example in the constructor of MainWindow) and then you can get that connection at any time using https://doc.qt.io/qt-5/qsqldatabase.html#database
                    There is really not much to it.

                    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