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. using model to fill tableView with QMYSQL
Forum Updated to NodeBB v4.3 + New Features

using model to fill tableView with QMYSQL

Scheduled Pinned Locked Moved Unsolved General and Desktop
31 Posts 6 Posters 4.4k 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.
  • Thank YouT Thank You

    @Christian-Ehrlicher
    Yes it works

    QSqlDatabasePrivate::addDatabase: duplicate connection name 'report', old connection removed.
    

    But this is error is very very disgusting. Like we should remove the connection,
    Can you say another answer or different way??

    @JonB Because I read somewhere that removing database is always great
    https://doc.qt.io/qt-5/qsqldatabase.html#removeDatabase

    JonBJ Offline
    JonBJ Offline
    JonB
    wrote on last edited by
    #14

    @Thank-You
    But you have an ongoing ui->information->setModel(model) which is still using the database, and you wonder why it goes wrong after you have done a removeDatabase()? Where does it say that situation is a good idea?

    1 Reply Last reply
    0
    • Thank YouT Thank You

      @Christian-Ehrlicher
      Yes it works

      QSqlDatabasePrivate::addDatabase: duplicate connection name 'report', old connection removed.
      

      But this is error is very very disgusting. Like we should remove the connection,
      Can you say another answer or different way??

      @JonB Because I read somewhere that removing database is always great
      https://doc.qt.io/qt-5/qsqldatabase.html#removeDatabase

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

      @Thank-You said in using model to fill tableView with QMYSQL:

      QSqlDatabasePrivate::addDatabase: duplicate connection name 'report', old connection removed.

      Simply add the database once...

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

      Thank YouT 1 Reply Last reply
      1
      • jsulmJ jsulm

        @Thank-You said in using model to fill tableView with QMYSQL:

        QSqlDatabasePrivate::addDatabase: duplicate connection name 'report', old connection removed.

        Simply add the database once...

        Thank YouT Offline
        Thank YouT Offline
        Thank You
        wrote on last edited by
        #16

        @jsulm
        https://doc.qt.io/qt-5/qsqldatabase.html#removeDatabase
        Warning: There should be no open queries on the database connection when this function is called, otherwise a resource leak will occur.

        {
            QSqlDatabase db = QSqlDatabase::database("sales");
            QSqlQuery query("SELECT NAME, DOB FROM EMPLOYEES", db);
        }
        // Both "db" and "query" are destroyed because they are out of scope
        QSqlDatabase::removeDatabase("sales"); // correct
        

        From the official documentation I found that queries should not be open.
        I don't know wether I am understanding what you are saying or not. If you mean different thing, please describe in simpler words

        Let's make QT free or It will go forever

        TRUE AND FALSE <3

        jsulmJ JonBJ 2 Replies Last reply
        0
        • Thank YouT Thank You

          @jsulm
          https://doc.qt.io/qt-5/qsqldatabase.html#removeDatabase
          Warning: There should be no open queries on the database connection when this function is called, otherwise a resource leak will occur.

          {
              QSqlDatabase db = QSqlDatabase::database("sales");
              QSqlQuery query("SELECT NAME, DOB FROM EMPLOYEES", db);
          }
          // Both "db" and "query" are destroyed because they are out of scope
          QSqlDatabase::removeDatabase("sales"); // correct
          

          From the official documentation I found that queries should not be open.
          I don't know wether I am understanding what you are saying or not. If you mean different thing, please describe in simpler words

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

          @Thank-You I was talking about ADDING database.
          "QSqlDatabasePrivate::addDatabase: duplicate connection name 'report', old connection removed." - this means you're trying to add a connection which already was added...

          What you posted now shows again that you still are removing the database though you was told several times already to not to do so. Why?!

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

          Thank YouT 1 Reply Last reply
          2
          • Thank YouT Thank You

            @jsulm
            https://doc.qt.io/qt-5/qsqldatabase.html#removeDatabase
            Warning: There should be no open queries on the database connection when this function is called, otherwise a resource leak will occur.

            {
                QSqlDatabase db = QSqlDatabase::database("sales");
                QSqlQuery query("SELECT NAME, DOB FROM EMPLOYEES", db);
            }
            // Both "db" and "query" are destroyed because they are out of scope
            QSqlDatabase::removeDatabase("sales"); // correct
            

            From the official documentation I found that queries should not be open.
            I don't know wether I am understanding what you are saying or not. If you mean different thing, please describe in simpler words

            JonBJ Offline
            JonBJ Offline
            JonB
            wrote on last edited by JonB
            #18

            @Thank-You said in using model to fill tableView with QMYSQL:

            From the official documentation I found that queries should not be open.

            Again: in your case the query is not out of scope/destroyed because you have:

                    QSqlQueryModel *model = new QSqlQueryModel();
                    QSqlQuery *query = new QSqlQuery(db);
                    model->setQuery(*query);
                    ui->information->setModel(model)
            
            1 Reply Last reply
            0
            • jsulmJ jsulm

              @Thank-You I was talking about ADDING database.
              "QSqlDatabasePrivate::addDatabase: duplicate connection name 'report', old connection removed." - this means you're trying to add a connection which already was added...

              What you posted now shows again that you still are removing the database though you was told several times already to not to do so. Why?!

              Thank YouT Offline
              Thank YouT Offline
              Thank You
              wrote on last edited by Thank You
              #19

              @jsulm
              If I am right ,
              We get this error when we didn't close the database properly.

              Please see my code at very top ,
              There I created database and after executing it
              I just removed the database

              Yes as said by @Christian-Ehrlicher and @VRonin If I remove this line it actually works but this error is obtained.

              Again I will tell you ,
              It was working fine when I used the "SQLITE" database, The same code. Even there is no fault in query too.
              What is difference between using these database, Is it due to ASYNCHRONUS thing?
              Am I right ??

              Let's make QT free or It will go forever

              TRUE AND FALSE <3

              JonBJ 1 Reply Last reply
              0
              • Thank YouT Thank You

                @jsulm
                If I am right ,
                We get this error when we didn't close the database properly.

                Please see my code at very top ,
                There I created database and after executing it
                I just removed the database

                Yes as said by @Christian-Ehrlicher and @VRonin If I remove this line it actually works but this error is obtained.

                Again I will tell you ,
                It was working fine when I used the "SQLITE" database, The same code. Even there is no fault in query too.
                What is difference between using these database, Is it due to ASYNCHRONUS thing?
                Am I right ??

                JonBJ Offline
                JonBJ Offline
                JonB
                wrote on last edited by
                #20

                @Thank-You
                We have tried to tell you. And one more time: just because bad code might work with SQLite but behave differently with MySQL does not mean we can tell you why you might get away with it in some circumstances.

                Why don't you just think about what you are doing? If you add database, open a query onto it and then set a permanent model to that which is referenced by an outside-world QTableView, and then you remove the database while that is still the case, what do you actually expect to happen? Can you not see this might be a problem?

                • Don't remove the database while you have a view/model using it.
                • Don't re-add the same database while you have previously added it and not yet removed it.

                Frankly I don't understand your whole architecture of opening a database while you are displaying a message box, and leaving it to persist into the outside world. The usual approach is to open a database initially/first time wanted and persist it till you are ready to close it and never want it again. I wouldn't be adding/removing the same database repeatedly.

                Thank YouT 1 Reply Last reply
                2
                • JonBJ JonB

                  @Thank-You
                  We have tried to tell you. And one more time: just because bad code might work with SQLite but behave differently with MySQL does not mean we can tell you why you might get away with it in some circumstances.

                  Why don't you just think about what you are doing? If you add database, open a query onto it and then set a permanent model to that which is referenced by an outside-world QTableView, and then you remove the database while that is still the case, what do you actually expect to happen? Can you not see this might be a problem?

                  • Don't remove the database while you have a view/model using it.
                  • Don't re-add the same database while you have previously added it and not yet removed it.

                  Frankly I don't understand your whole architecture of opening a database while you are displaying a message box, and leaving it to persist into the outside world. The usual approach is to open a database initially/first time wanted and persist it till you are ready to close it and never want it again. I wouldn't be adding/removing the same database repeatedly.

                  Thank YouT Offline
                  Thank YouT Offline
                  Thank You
                  wrote on last edited by Thank You
                  #21

                  Don't remove the database while you have a view/model using it

                  How can I know that it is using the database or not??

                  I just tried inserting QMessageBox when

                  if(query->exec()){
                  // same as the upper code here 
                  QMessageBox::information(this,"NOw the database is shown", " This is also shown");
                  }
                  

                  bed72770-61b8-47f8-b13a-4e0a7dd44ca2-image.png

                  After clicking OK,
                  1898caf6-1200-40ad-948d-07096e55b0fe-image.png
                  The data vanishes again
                  @jsulm @JonB can you see this?

                  Let's make QT free or It will go forever

                  TRUE AND FALSE <3

                  JonBJ 1 Reply Last reply
                  0
                  • Thank YouT Thank You

                    Don't remove the database while you have a view/model using it

                    How can I know that it is using the database or not??

                    I just tried inserting QMessageBox when

                    if(query->exec()){
                    // same as the upper code here 
                    QMessageBox::information(this,"NOw the database is shown", " This is also shown");
                    }
                    

                    bed72770-61b8-47f8-b13a-4e0a7dd44ca2-image.png

                    After clicking OK,
                    1898caf6-1200-40ad-948d-07096e55b0fe-image.png
                    The data vanishes again
                    @jsulm @JonB can you see this?

                    JonBJ Offline
                    JonBJ Offline
                    JonB
                    wrote on last edited by
                    #22

                    @Thank-You
                    After showing the message box, does your code execute QSqlDatabase::removeDatabase()? Just a "yes" or "no" answer.

                    Thank YouT 1 Reply Last reply
                    0
                    • JonBJ JonB

                      @Thank-You
                      After showing the message box, does your code execute QSqlDatabase::removeDatabase()? Just a "yes" or "no" answer.

                      Thank YouT Offline
                      Thank YouT Offline
                      Thank You
                      wrote on last edited by Thank You
                      #23

                      @JonB
                      No it doesn't
                      It executes
                      removeDatabase
                      at the very end of the program
                      like

                      {
                      db initialize
                      execute query and set model
                      show the message box
                      }
                      remove the database

                      or you can see the code same but there is
                      messagebox inside if(query->exec){
                      // all execution like model etc
                      messagebox
                      }

                      Let's make QT free or It will go forever

                      TRUE AND FALSE <3

                      JonBJ 1 Reply Last reply
                      0
                      • Thank YouT Thank You

                        @JonB
                        No it doesn't
                        It executes
                        removeDatabase
                        at the very end of the program
                        like

                        {
                        db initialize
                        execute query and set model
                        show the message box
                        }
                        remove the database

                        or you can see the code same but there is
                        messagebox inside if(query->exec){
                        // all execution like model etc
                        messagebox
                        }

                        JonBJ Offline
                        JonBJ Offline
                        JonB
                        wrote on last edited by JonB
                        #24

                        @Thank-You
                        If you really want anyone to look at this, show your actual relevant, minimal code which reproduces the issue, not a description of it.

                        Thank YouT 1 Reply Last reply
                        0
                        • JonBJ JonB

                          @Thank-You
                          If you really want anyone to look at this, show your actual relevant, minimal code which reproduces the issue, not a description of it.

                          Thank YouT Offline
                          Thank YouT Offline
                          Thank You
                          wrote on last edited by Thank You
                          #25

                          @JonB I have posted the code right at the top
                          Exactly that code brings this error

                          void tableFill::fill_table(){
                          QString command = "select * from countryDetails;";
                             {
                          QString databaseName = "root";
                          QString databasePassword= "";
                          QString databaseUsername= "root";
                          QString databaseHost = "localhost";
                          int databaseName = 3306;
                          
                          
                                 QSqlDatabase db =  QSqlDatabase::addDatabase("QMYSQL");
                                 db.setDatabaseName(databaseName);
                          db.setPort(databasePort);
                          db.setHostName(databaseHostname);
                          db.setUserName(databaseUsername);
                          db.setPassword(databasePassword);
                              
                                 if(db.open()){
                                     QSqlQueryModel *model = new QSqlQueryModel();
                                     QSqlQuery *query = new QSqlQuery(db);
                                     query->prepare(command);
                                     if(query->exec()){
                                         model->setQuery(*query);
                                         ui->information->setModel(model);
                          //setting the model 
                                     }else{
                                         QMessageBox::warning(this,"ERrors in query exec" ,"Error while using query execution"+query->lastError().text());
                                     }
                          
                                 }else{
                                     QMessageBox::warning(this,"Error in database ", "Database is not connected "+db.lastError().text());
                                 }
                          
                             }
                             QSqlDatabase::removeDatabase("qt_sql_default_connection"); 
                          }
                          

                          Let's make QT free or It will go forever

                          TRUE AND FALSE <3

                          Christian EhrlicherC JonBJ 2 Replies Last reply
                          0
                          • Thank YouT Thank You

                            @JonB I have posted the code right at the top
                            Exactly that code brings this error

                            void tableFill::fill_table(){
                            QString command = "select * from countryDetails;";
                               {
                            QString databaseName = "root";
                            QString databasePassword= "";
                            QString databaseUsername= "root";
                            QString databaseHost = "localhost";
                            int databaseName = 3306;
                            
                            
                                   QSqlDatabase db =  QSqlDatabase::addDatabase("QMYSQL");
                                   db.setDatabaseName(databaseName);
                            db.setPort(databasePort);
                            db.setHostName(databaseHostname);
                            db.setUserName(databaseUsername);
                            db.setPassword(databasePassword);
                                
                                   if(db.open()){
                                       QSqlQueryModel *model = new QSqlQueryModel();
                                       QSqlQuery *query = new QSqlQuery(db);
                                       query->prepare(command);
                                       if(query->exec()){
                                           model->setQuery(*query);
                                           ui->information->setModel(model);
                            //setting the model 
                                       }else{
                                           QMessageBox::warning(this,"ERrors in query exec" ,"Error while using query execution"+query->lastError().text());
                                       }
                            
                                   }else{
                                       QMessageBox::warning(this,"Error in database ", "Database is not connected "+db.lastError().text());
                                   }
                            
                               }
                               QSqlDatabase::removeDatabase("qt_sql_default_connection"); 
                            }
                            
                            Christian EhrlicherC Offline
                            Christian EhrlicherC Offline
                            Christian Ehrlicher
                            Lifetime Qt Champion
                            wrote on last edited by Christian Ehrlicher
                            #26

                            @Thank-You And we told you at least two times that you must not remove the database connection while there is an open query for your QSqlTableModel!

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

                            Thank YouT 1 Reply Last reply
                            0
                            • Thank YouT Thank You

                              @JonB I have posted the code right at the top
                              Exactly that code brings this error

                              void tableFill::fill_table(){
                              QString command = "select * from countryDetails;";
                                 {
                              QString databaseName = "root";
                              QString databasePassword= "";
                              QString databaseUsername= "root";
                              QString databaseHost = "localhost";
                              int databaseName = 3306;
                              
                              
                                     QSqlDatabase db =  QSqlDatabase::addDatabase("QMYSQL");
                                     db.setDatabaseName(databaseName);
                              db.setPort(databasePort);
                              db.setHostName(databaseHostname);
                              db.setUserName(databaseUsername);
                              db.setPassword(databasePassword);
                                  
                                     if(db.open()){
                                         QSqlQueryModel *model = new QSqlQueryModel();
                                         QSqlQuery *query = new QSqlQuery(db);
                                         query->prepare(command);
                                         if(query->exec()){
                                             model->setQuery(*query);
                                             ui->information->setModel(model);
                              //setting the model 
                                         }else{
                                             QMessageBox::warning(this,"ERrors in query exec" ,"Error while using query execution"+query->lastError().text());
                                         }
                              
                                     }else{
                                         QMessageBox::warning(this,"Error in database ", "Database is not connected "+db.lastError().text());
                                     }
                              
                                 }
                                 QSqlDatabase::removeDatabase("qt_sql_default_connection"); 
                              }
                              
                              JonBJ Offline
                              JonBJ Offline
                              JonB
                              wrote on last edited by JonB
                              #27

                              @Thank-You
                              This is exactly the code as it was. It has removeDatabase() immediately after the message box show. Which we have told you multiple times is wrong. It does not have the

                              QMessageBox::information(this,"NOw the database is shown", " This is also shown");
                              

                              you later claimed to have put in, nor the output you showed in your last screenshot. Which doesn't correspond to that either.

                              What else is there to say? You don't listen, you don't adjust your code, you show screenshots which don't pertain to the code.

                              I think we are all done. We don't know what answer you are prepared to accept. I suggest you keep your code exactly as you have it, since that is the only thing you are prepared to accept. Bye.

                              1 Reply Last reply
                              0
                              • Christian EhrlicherC Christian Ehrlicher

                                @Thank-You And we told you at least two times that you must not remove the database connection while there is an open query for your QSqlTableModel!

                                Thank YouT Offline
                                Thank YouT Offline
                                Thank You
                                wrote on last edited by
                                #28

                                @Christian-Ehrlicher And when to remove it
                                Or leave it without removing ?
                                I said that it will give me errors,
                                QSqlDatabasePrivate::addDatabase: duplicate connection name 'qt_sql_default_connection', old connection removed.
                                I don't want this application to have this error?

                                OR
                                How can I get the signal when query does not require the database.

                                @JonB Please refer to my last code. It is exactly the same, Nothing different. May be I gave you the example of another table fill.
                                Just see the last one and say exactly what is the problem just for the last time.

                                Let's make QT free or It will go forever

                                TRUE AND FALSE <3

                                jsulmJ 1 Reply Last reply
                                0
                                • Thank YouT Thank You

                                  @Christian-Ehrlicher And when to remove it
                                  Or leave it without removing ?
                                  I said that it will give me errors,
                                  QSqlDatabasePrivate::addDatabase: duplicate connection name 'qt_sql_default_connection', old connection removed.
                                  I don't want this application to have this error?

                                  OR
                                  How can I get the signal when query does not require the database.

                                  @JonB Please refer to my last code. It is exactly the same, Nothing different. May be I gave you the example of another table fill.
                                  Just see the last one and say exactly what is the problem just for the last time.

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

                                  @Thank-You said in using model to fill tableView with QMYSQL:

                                  I said that it will give me errors,

                                  And I said you to not to add the database again and again, do it just once. I really don't know why it is necessary to repeat same things again and again...
                                  Remove the database when it is not needed anymore (for example in the destructor of your class).

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

                                  Thank YouT 1 Reply Last reply
                                  0
                                  • jsulmJ jsulm

                                    @Thank-You said in using model to fill tableView with QMYSQL:

                                    I said that it will give me errors,

                                    And I said you to not to add the database again and again, do it just once. I really don't know why it is necessary to repeat same things again and again...
                                    Remove the database when it is not needed anymore (for example in the destructor of your class).

                                    Thank YouT Offline
                                    Thank YouT Offline
                                    Thank You
                                    wrote on last edited by
                                    #30

                                    @jsulm OOOOw
                                    Maybe it can help ,
                                    I will try and say the result.

                                    Let's make QT free or It will go forever

                                    TRUE AND FALSE <3

                                    1 Reply Last reply
                                    0
                                    • N Offline
                                      N Offline
                                      Nekreg45
                                      wrote on last edited by
                                      #31

                                      Maybe an analogy would help with the advice that everybody has given. Pretend like running a query on a database is like teaching a dog a new trick. You want the dog to be alive when you teach the trick and still be alive to enjoy watching the trick.

                                      What people are saying is that in your fill table function, you are creating a dog, teaching a trick, killing the dog, and wondering how come it doesn't perform the trick.

                                      In conclusion, you need your dog to live as long as the lifetime that you want the user to see your table. Did this help?

                                      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