Why is a duplicate database connection message displayed.
-
wrote on 25 Jun 2021, 17:09 last edited by lincoln
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. -
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. -
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.
-
wrote on 25 Jun 2021, 18:12 last edited by
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; }
-
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. -
wrote on 25 Jun 2021, 18:30 last edited by
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;
}
-
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.
-
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.
-
@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.
@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.