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. When trying to make some queries and deletes in the database, the program closes and does not inform the reason of the crash
Forum Updated to NodeBB v4.3 + New Features

When trying to make some queries and deletes in the database, the program closes and does not inform the reason of the crash

Scheduled Pinned Locked Moved Solved General and Desktop
21 Posts 6 Posters 2.7k Views 3 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.
  • mrjjM mrjj

    Hi
    Do you use/open any other database at the same time besides
    BDLogin ?

    Alexandre CameloA Offline
    Alexandre CameloA Offline
    Alexandre Camelo
    wrote on last edited by
    #8

    @mrjj

    Hi, mrjj!

    I open the same database in two different instances. The name of the database is 'REGISTERS'. I need to open it in the login window, instance ‘BDLogin’ and when the username and password are validated, the main window opens. Then I close the instance 'BDLogin', close the login window and open the main window. In the builder area, I create a new instance (BDCadasters) and open the database again.

    Although I close one instance to open another, in the debug area there are always two messages stating that there was an open database and that it is being unusable for another to open. Really do not understand.

    Detail: The database has 3 tables. I use different tables in each of the windows.

    1 Reply Last reply
    0
    • SGaistS SGaist

      Hi,

      BDLogin is a member variable, isn't it ?
      There's a warning in the QSqlDatabase documentation that explicitly say that you shall not keep QSqlDatabase object as member variables.

      Alexandre CameloA Offline
      Alexandre CameloA Offline
      Alexandre Camelo
      wrote on last edited by
      #9

      @SGaist

      Hi, SGainst!

      I will give almost the same answer I gave to mrjj, with some additions:

      I open the same database in two different instances. The name of the database is 'REGISTERS'. I need to open it in the login window, instance ‘BDLogin’ and when the username and password are validated, the main window opens. Then I close the instance 'BDLogin', close the login window and open the main window. In the builder area, I create a new instance (BDCadasters) and open the database again.

      Although I close one instance to open another, in the debug area there are always two messages stating that there was an open database and that it is being unusable for another to open. Really do not understand.

      Detail: The database has 3 tables. I use different tables in each of the windows.

      Despite all my explanation, I was left with a little doubt: what is member variable? Where in the code should I NOT keep it?

      I'm new to QT and C ++, so I will answer your question as I know: BDLogin is an instance of QSqldatabase, created to open the database. Would that be a member variable?

      Alexandre CameloA 1 Reply Last reply
      0
      • Alexandre CameloA Alexandre Camelo

        @SGaist

        Hi, SGainst!

        I will give almost the same answer I gave to mrjj, with some additions:

        I open the same database in two different instances. The name of the database is 'REGISTERS'. I need to open it in the login window, instance ‘BDLogin’ and when the username and password are validated, the main window opens. Then I close the instance 'BDLogin', close the login window and open the main window. In the builder area, I create a new instance (BDCadasters) and open the database again.

        Although I close one instance to open another, in the debug area there are always two messages stating that there was an open database and that it is being unusable for another to open. Really do not understand.

        Detail: The database has 3 tables. I use different tables in each of the windows.

        Despite all my explanation, I was left with a little doubt: what is member variable? Where in the code should I NOT keep it?

        I'm new to QT and C ++, so I will answer your question as I know: BDLogin is an instance of QSqldatabase, created to open the database. Would that be a member variable?

        Alexandre CameloA Offline
        Alexandre CameloA Offline
        Alexandre Camelo
        wrote on last edited by
        #10

        @Alexandre-Camelo @mrjj @fcarney @Christian-Ehrlicher @SGaist

        One question for all of you trying to help me: I have declared all QSqlquery in the .h file so that they are available all the time, in any scope of the cpp file. I did wrong? Can I have multiple QSqlquery open at the same time? I realize that the error occurs when I am using these queries at the same time. Example: I search by ID, then do a search by name, and then delete the record. AT THAT EXACT TIME, the error occurs.

        1 Reply Last reply
        0
        • Alexandre CameloA Offline
          Alexandre CameloA Offline
          Alexandre Camelo
          wrote on last edited by
          #11

          BUDIEEEEESSSSSSSSS !!!!!!!

          I got to decipher the riddle !!!!!

          Thanks to partner @fcarney! Your tip for running the program in debug mode was very valuable!

          I noticed that the problem was NOT in any database or query.

          There is a tablewidget on the form, which lists all customers. Whenever there is an update on the form, it is reflected in the tablewidget (additions, deletions, selections ...).

          Similarly, when I select a record in tablewidget, this action is reflected in the form.

          But I was using the "itemSelectonChanged" slot in the table widget. THAT WAS THE MISTAKE !!!!! Whenever some code updated tablewidget, the "itemSelectonChanged" slot was activated, generating an infinite loop that broke the code.

          I changed the tablewidget slot to "itemDoubleClicked" and it's all settled!

          Thank you very much to all of you. Each of your opinions helped me learn a little more about QT and C ++.

          @fcarney @Christian-Ehrlicher @mrjj @SGaist

          1 Reply Last reply
          2
          • Christian EhrlicherC Offline
            Christian EhrlicherC Offline
            Christian Ehrlicher
            Lifetime Qt Champion
            wrote on last edited by
            #12

            You should still fix the database warning as it may lead to crashes.

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

            Alexandre CameloA 1 Reply Last reply
            2
            • Christian EhrlicherC Christian Ehrlicher

              You should still fix the database warning as it may lead to crashes.

              Alexandre CameloA Offline
              Alexandre CameloA Offline
              Alexandre Camelo
              wrote on last edited by Alexandre Camelo
              #13

              @Christian-Ehrlicher

              Exactly.

              I haven't figured out how to do it yet.

              I created a "Connection" class that contains two functions: 1) Open database; 2) Close database. It is through the functions of this class that I am opening the database. This class is in an .h file, which I refer to when I need to open the DB.

              Example: I compile the program and the login window opens. In your .h file, I create a new instance for the "Connection" class. In the cpp file builder is the "open database" command. When user data is validated, the login window is closed and I use the .close () command to close the database.

              Shortly after this, the main window opens and the whole process above (new instance creation, database opening) is repeated. Even so, the database open error messages appear.

              I thought the error would occur in the class when entering the DB connection type: BDCadastros = QSqlDatabase :: addDatabase ("QSQLITE"). However, when I remove this command from the class and place it as a global variable, the database is not opened. I do not know what to do.

              Here's the code of the class I created (remembering that I put it in an .h file that I give <include> to other files):

              <code>
              class ConexaoCadastros
              {
              public:
              QSqlDatabase BDCadastros; //Cria um objeto da classe QSqlDatabase

              ConexaoCadastros() //Coloca o tipo de banco de dados no CONSTRUTOR da classe
              {
                  BDCadastros=QSqlDatabase::addDatabase("QSQLITE");
              }
              
              
              //Função para abrir o BD
              bool Abrir()
              {
                      BDCadastros.setDatabaseName (glbCAMINHOBDCADASTROS);  //Seleciona o banco de dados
              
                      if(!BDCadastros.open())    //Abre o BD, testando se ele foi aberto com sucesso
                      {
                          return false;
                      }
                      else
                      {
                          return true;
                      }
              
              }
              
              
              //Função para fechar o BD
              void Fechar()
              {
                  BDCadastros.close();
              }
              

              };
              <code/>

              Do you have any suggestions?

              1 Reply Last reply
              0
              • Christian EhrlicherC Offline
                Christian EhrlicherC Offline
                Christian Ehrlicher
                Lifetime Qt Champion
                wrote on last edited by Christian Ehrlicher
                #14

                @Alexandre-Camelo said in When trying to make some queries and deletes in the database, the program closes and does not inform the reason of the crash:

                I use the .close () command to close the database.

                Why? Why don't you just follow the documentation, let the db open and get the connection with QSqlDatabase::database(QString)?

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

                Alexandre CameloA 1 Reply Last reply
                1
                • Christian EhrlicherC Christian Ehrlicher

                  @Alexandre-Camelo said in When trying to make some queries and deletes in the database, the program closes and does not inform the reason of the crash:

                  I use the .close () command to close the database.

                  Why? Why don't you just follow the documentation, let the db open and get the connection with QSqlDatabase::database(QString)?

                  Alexandre CameloA Offline
                  Alexandre CameloA Offline
                  Alexandre Camelo
                  wrote on last edited by
                  #15

                  @Christian-Ehrlicher said in When trying to make some queries and deletes in the database, the program closes and does not inform the reason of the crash:

                  @Alexandre-Camelo said in When trying to make some queries and deletes in the database, the program closes and does not inform the reason of the crash:

                  I use the .close () command to close the database.

                  Why? Why don't you just follow the documentation, let the db open and get the connection with QSqlDatabase::database(QString)?

                  As I said, I'm new to QT and C ++.

                  I tried in many ways to leave the database open and make all queries throughout the code.

                  But I can't.

                  Whenever I close one window and open another, the previous window's database closes (even if I don't use the close () command.) This way, I am forced to reopen the DB when I open the new window. It's worse: The open database message appears.

                  How do I leave the database open during all code execution?

                  jsulmJ 1 Reply Last reply
                  0
                  • Christian EhrlicherC Offline
                    Christian EhrlicherC Offline
                    Christian Ehrlicher
                    Lifetime Qt Champion
                    wrote on last edited by
                    #16

                    @Alexandre-Camelo said in When trying to make some queries and deletes in the database, the program closes and does not inform the reason of the crash:

                    How do I leave the database open during all code execution?

                    As stated in the documentation - open the database once with QSqlDatabase db = QSqlDatabase::addDatabase("..."); and get the db connection with QSqlDatabase db = 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
                    2
                    • Alexandre CameloA Alexandre Camelo

                      @Christian-Ehrlicher said in When trying to make some queries and deletes in the database, the program closes and does not inform the reason of the crash:

                      @Alexandre-Camelo said in When trying to make some queries and deletes in the database, the program closes and does not inform the reason of the crash:

                      I use the .close () command to close the database.

                      Why? Why don't you just follow the documentation, let the db open and get the connection with QSqlDatabase::database(QString)?

                      As I said, I'm new to QT and C ++.

                      I tried in many ways to leave the database open and make all queries throughout the code.

                      But I can't.

                      Whenever I close one window and open another, the previous window's database closes (even if I don't use the close () command.) This way, I am forced to reopen the DB when I open the new window. It's worse: The open database message appears.

                      How do I leave the database open during all code execution?

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

                      @Alexandre-Camelo said in When trying to make some queries and deletes in the database, the program closes and does not inform the reason of the crash:

                      the previous window's database closes

                      Do you have a member variable in your window which holds the database connection?
                      @SGaist already pointed out that you should NOT do this.
                      QSqlDatabase manages all your connections for you and you can get any open connection using QSqlDatabase::database();
                      There is absolutely no need to have member variables holding the connection.

                      https://forum.qt.io/topic/113070/qt-code-of-conduct

                      Alexandre CameloA 1 Reply Last reply
                      3
                      • jsulmJ jsulm

                        @Alexandre-Camelo said in When trying to make some queries and deletes in the database, the program closes and does not inform the reason of the crash:

                        the previous window's database closes

                        Do you have a member variable in your window which holds the database connection?
                        @SGaist already pointed out that you should NOT do this.
                        QSqlDatabase manages all your connections for you and you can get any open connection using QSqlDatabase::database();
                        There is absolutely no need to have member variables holding the connection.

                        Alexandre CameloA Offline
                        Alexandre CameloA Offline
                        Alexandre Camelo
                        wrote on last edited by
                        #18

                        @jsulm

                        OK friends.

                        It worked.

                        Thank you very much.

                        One last question: You talked about "DOCUMENTATION". Where do I find her? On the QT website? No QT help?

                        All I have done is watching videos on youtube.

                        1 Reply Last reply
                        0
                        • fcarneyF Offline
                          fcarneyF Offline
                          fcarney
                          wrote on last edited by
                          #19

                          Literally I Googled "qt docs":
                          https://doc.qt.io/
                          Also, when searching for something specific search:
                          "qt json" or "qobject" or "qstring" etc

                          C++ is a perfectly valid school of magic.

                          Alexandre CameloA 1 Reply Last reply
                          1
                          • fcarneyF fcarney

                            Literally I Googled "qt docs":
                            https://doc.qt.io/
                            Also, when searching for something specific search:
                            "qt json" or "qobject" or "qstring" etc

                            Alexandre CameloA Offline
                            Alexandre CameloA Offline
                            Alexandre Camelo
                            wrote on last edited by
                            #20

                            @fcarney said in When trying to make some queries and deletes in the database, the program closes and does not inform the reason of the crash:

                            Literally I Googled "qt docs":
                            https://doc.qt.io/
                            Also, when searching for something specific search:
                            "qt json" or "qobject" or "qstring" etc

                            Thanks guys.

                            You guys helped me A LOT!

                            In a little while I'll be back to "bother" you again.

                            :-D

                            1 Reply Last reply
                            0
                            • fcarneyF Offline
                              fcarneyF Offline
                              fcarney
                              wrote on last edited by
                              #21

                              @Alexandre-Camelo said in When trying to make some queries and deletes in the database, the program closes and does not inform the reason of the crash:

                              In a little while I'll be back to "bother" you again.

                              Its all about the "bother", no trouble...

                              C++ is a perfectly valid school of magic.

                              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