Check database (mysql) connection in other window
-
Hi,
I am new to qt5, that is why i need to ask.
I am working with mysql und several windows.
In the main window I am creating the connection.
StartWindow::StartWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::StartWindow) { ui->setupUi(this); // config mysql db = QSqlDatabase::addDatabase("QMYSQL"); db.setHostName("localhost"); db.setUserName("root"); db.setPassword("root"); db.setDatabaseName("mytable"); } void StartWindow::on_b_login_clicked() { if (db.open()) { MainWindow *main = new MainWindow(&user, this); this->hide(); main->show(); } }
In my second window I can easy do the queries like
QString sql = "SELECT * FROM members ORDER BY name ASC"; QSqlQuery query(sql);
My question is how can I check if the mysql connection is active. I now that I need isValid, but the mysql object is not in that scope. I like to check the connection, when it is false, try a reconnect and when this is false as well i show an error. A function for for that check would be fine. I still do not know where to declare a function that I can use it in any window and everywhere.
Maybe you have some hints that might help me.
-
@Philipp-DE said in Check database (mysql) connection in other window:
but the mysql object is not in that scope.
-
Hi,
To add to @Christian-Ehrlicher, as explained in the documentation, do not keep a QSqlDatabase member variable, the connections as managed by the QSqlDatabase class. It's even more true since you are using only the default connection.
-
Hi,
when I check QSqlDatabase::database() it will return a QSqlDatabase but I am not sure if it check a connection.
This where would be the logic I am looking for, but nothing happens.
if (QSqlDatabase::database().isOpen() == false) { qDebug() << "Database not connected"; if (QSqlDatabase::database().open() == false) { // connection not possible qDebug() << "Database reconnect failed"; } else { qDebug() << "Database reconnect succeded"; } }
-
You are saying nothing happens, you mean nothing is printed through qDebug()?
If so would that not mean your connection is already open?
QT is fantastic for DB connections. Im working on a multithreaded program right now and even in new threads, I can still grab the db connections with QSqlDatabase::database().
--James
-
Hi,
I started my application and mysql works fine. I am using XAMPP als local Webserver.
Then I close Xampp, so mysql is not anymore available and I tried to run my code.
The connection could not be valid anymore, but there is not output from qdebug.
I like to run my application later on local computer, but use mysql on a webserver and when the application is running, of course I believe it might lose the connection.
The last 15 years I worked with php. There I create a connection which each request, but in desktop software I think it is better to check if before I run new operations?
-
Nothing stops you from creating a controller class that will open and close the connection for handling each request.
From what you wrote, you seem to want to expose a database directly on internet. That's highly discouraged. Currently, databases are shielded behind a REST service that provides a clean API with only access to the data that makes sense. This allows to keep your databases and their content secured and if needed you can easily change the database stack without having to change your application.
-
With request in the website I mean everytime when a page is loaded. Not for each function using mysql. That would be to slow.
You said that there is a REST API for internal communication.
Does that mean when I know that the connection is once open, that I do not need to check later (for example second window or 5 hours later) if it is still open?
What is when the mysql Server is not any more available, because of technical problems.
That is my I think about to check it, try to reconnect and then decide to cancel a special task.
-
What exactly is your Qt application going to do ?
-
Hi,
I am working on a simple application to manage the members in a sport club, track there payments and stuff like this. There are no processes running automatically.
As web based software it would be very easy, but I like to create a desktop software and ask he what I should do, because a connection might not work anymore during the application runs.
-
Where is that database going to be located ? Is it a remote service ? Is it on the same network as the machine running your application ?
-
It is a mysql Database which I like to run on a webserver (not local). The application will run on a local computer. There are only few and small data sets.
But to open and close the connection for each action will not be the right way. That why I aske how to check if the connection is still establish and when not, i will try a reconnect.
-
In that case and as I already wrote before, you should implement a proper web service to access the data from your desktop application. You then use QNetworkAccessManager to query that service.