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. Check database (mysql) connection in other window
QtWS25 Last Chance

Check database (mysql) connection in other window

Scheduled Pinned Locked Moved Unsolved General and Desktop
13 Posts 4 Posters 1.6k 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.
  • P Offline
    P Offline
    Philipp DE
    wrote on 10 May 2020, 16:19 last edited by
    #1

    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.

    1 Reply Last reply
    0
    • C Offline
      C Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on 10 May 2020, 16:35 last edited by
      #2

      @Philipp-DE said in Check database (mysql) connection in other window:

      but the mysql object is not in that scope.

      See QSqlDatabase::database()

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      1 Reply Last reply
      1
      • S Offline
        S Offline
        SGaist
        Lifetime Qt Champion
        wrote on 10 May 2020, 18:11 last edited by
        #3

        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.

        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
        • P Offline
          P Offline
          Philipp DE
          wrote on 10 May 2020, 21:19 last edited by
          #4

          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";
                  }
              }
          
          1 Reply Last reply
          0
          • J Offline
            J Offline
            JSher
            wrote on 11 May 2020, 00:35 last edited by
            #5

            @Philipp-DE

            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

            1 Reply Last reply
            2
            • P Offline
              P Offline
              Philipp DE
              wrote on 11 May 2020, 17:42 last edited by
              #6

              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?

              1 Reply Last reply
              0
              • S Offline
                S Offline
                SGaist
                Lifetime Qt Champion
                wrote on 11 May 2020, 17:54 last edited by
                #7

                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.

                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
                • P Offline
                  P Offline
                  Philipp DE
                  wrote on 11 May 2020, 22:15 last edited by
                  #8

                  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.

                  1 Reply Last reply
                  0
                  • S Offline
                    S Offline
                    SGaist
                    Lifetime Qt Champion
                    wrote on 12 May 2020, 17:59 last edited by
                    #9

                    What exactly is your Qt application going to do ?

                    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
                    • P Offline
                      P Offline
                      Philipp DE
                      wrote on 13 May 2020, 20:27 last edited by
                      #10

                      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.

                      1 Reply Last reply
                      0
                      • S Offline
                        S Offline
                        SGaist
                        Lifetime Qt Champion
                        wrote on 13 May 2020, 20:28 last edited by
                        #11

                        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 ?

                        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
                        • P Offline
                          P Offline
                          Philipp DE
                          wrote on 13 May 2020, 20:30 last edited by Philipp DE
                          #12

                          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.

                          1 Reply Last reply
                          0
                          • S Offline
                            S Offline
                            SGaist
                            Lifetime Qt Champion
                            wrote on 13 May 2020, 21:23 last edited by
                            #13

                            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.

                            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
                            2

                            3/13

                            10 May 2020, 18:11

                            topic:navigator.unread, 10
                            • Login

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