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 3.7k 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.
  • T Offline
    T Offline
    Thank You
    wrote on 27 May 2021, 16:35 last edited by
    #1

    I ran into huge problem at the end of the project.
    I was using sqlite as the database at the starting of the project. Configured mysql database and liked it so I decided to use it.
    Everything is working absolutely fine except one
    Filling The datas in tableView using model
    a82f8a8d-2823-46d2-ab6e-5f07416ac525-image.png
    I am getting the data but it is empty.
    And fetching in text Edit works so fine.
    Using the same queries everything is working but filling doesnot work , See in the picture even the query is working and detecting that there are two datas but they aren't filled
    WHY

    What can I do to solve this?
    Plz I would be very thankfull

    Let's make QT free or It will go forever

    TRUE AND FALSE <3

    V 1 Reply Last reply 27 May 2021, 17:49
    0
    • T Thank You
      27 May 2021, 16:35

      I ran into huge problem at the end of the project.
      I was using sqlite as the database at the starting of the project. Configured mysql database and liked it so I decided to use it.
      Everything is working absolutely fine except one
      Filling The datas in tableView using model
      a82f8a8d-2823-46d2-ab6e-5f07416ac525-image.png
      I am getting the data but it is empty.
      And fetching in text Edit works so fine.
      Using the same queries everything is working but filling doesnot work , See in the picture even the query is working and detecting that there are two datas but they aren't filled
      WHY

      What can I do to solve this?
      Plz I would be very thankfull

      V Offline
      V Offline
      VRonin
      wrote on 27 May 2021, 17:49 last edited by
      #2

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

      Filling The datas in tableView using model

      Can you show your code?

      "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
      ~Napoleon Bonaparte

      On a crusade to banish setIndexWidget() from the holy land of Qt

      T 1 Reply Last reply 28 May 2021, 03:25
      1
      • N Offline
        N Offline
        Nekreg45
        wrote on 27 May 2021, 17:55 last edited by
        #3

        Just to cover the bases, did you create a QSqlTableModel with the SQL database, then pass it into your TableView? Did you set a table?

        Here's how I have mine setup:

        _database = loadDatabase("postgres");  //There is more code in this function, but I assume yours is correct.
        _tableModel = new QSqlTableModel(this,_database);
        _tableModel->setTable(tableName);
        _tableModel->setEditStrategy(QSqlTableModel::OnFieldChange);
        _tableModel->select();
        ui->tableView->setModel(_tableModel);
        

        I hope this helps.

        1 Reply Last reply
        2
        • V VRonin
          27 May 2021, 17:49

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

          Filling The datas in tableView using model

          Can you show your code?

          T Offline
          T Offline
          Thank You
          wrote on 28 May 2021, 03:25 last edited by Thank You
          #4

          @VRonin

           QString command = "select * from countryDetails;";
              {
                  QSqlDatabase db =  QSqlDatabase::addDatabase("QMYSQL");
                  db.setDatabaseName(databaseName);
          db.setPort(databasePort);
          db.setHostName(databaseHostname);
          db.setUserName(databaseUsername);
          db.setPassword(databasePassword);
                  db.open();
                  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,data.ERRORS.DATABASE_SELECT_NOT_WORKING_TITLE("CF"),data.ERRORS.DATABASE_SELECT_NOT_WORKING_BODY("CF")+query->lastError().text());
                      }
          
                  }else{
                      QMessageBox::warning(this,data.ERRORS.DATABASE_NOT_CONNECTED_TITLE("CF"),data.ERRORS.DATABASE_NOT_CONNECTED_BODY("CF")+db.lastError().text());
                  }
          
              }
              QSqlDatabase::removeDatabase("qt_sql_default_connection"); // removing database
          

          If I put the message box inside the query, then while the messagebox is open then the table is filled , as soon as I close the messagebox then data vanish but the rows with empty data remains there WHY?

           QString command = "select * from countryDetails;";
              {
                  QSqlDatabase db =  QSqlDatabase::addDatabase("QMYSQL");
                  db.setDatabaseName(databaseName);
          db.setPort(databasePort);
          db.setHostName(databaseHostname);
          db.setUserName(databaseUsername);
          db.setPassword(databasePassword);
                  db.open();
                  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);
          QMessageBox::information(this,"Now it is seen", "seen now");
          //setting the model 
                      }else{
                          QMessageBox::warning(this,data.ERRORS.DATABASE_SELECT_NOT_WORKING_TITLE("CF"),data.ERRORS.DATABASE_SELECT_NOT_WORKING_BODY("CF")+query->lastError().text());
                      }
          
                  }else{
                      QMessageBox::warning(this,data.ERRORS.DATABASE_NOT_CONNECTED_TITLE("CF"),data.ERRORS.DATABASE_NOT_CONNECTED_BODY("CF")+db.lastError().text());
                  }
          
              }
              QSqlDatabase::removeDatabase("qt_sql_default_connection"); // removing database
          

          Let's make QT free or It will go forever

          TRUE AND FALSE <3

          J 1 Reply Last reply 28 May 2021, 07:06
          0
          • V Offline
            V Offline
            VRonin
            wrote on 28 May 2021, 06:23 last edited by
            #5

            Do not remove the database. That’s the problem

            "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
            ~Napoleon Bonaparte

            On a crusade to banish setIndexWidget() from the holy land of Qt

            1 Reply Last reply
            2
            • Christian EhrlicherC Offline
              Christian EhrlicherC Offline
              Christian Ehrlicher
              Lifetime Qt Champion
              wrote on 28 May 2021, 06:44 last edited by
              #6

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

              QSqlQuery *query = new QSqlQuery(db);

              And don't create it on the heap - otherwise the query is leaking.

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

              T 1 Reply Last reply 28 May 2021, 07:14
              3
              • T Thank You
                28 May 2021, 03:25

                @VRonin

                 QString command = "select * from countryDetails;";
                    {
                        QSqlDatabase db =  QSqlDatabase::addDatabase("QMYSQL");
                        db.setDatabaseName(databaseName);
                db.setPort(databasePort);
                db.setHostName(databaseHostname);
                db.setUserName(databaseUsername);
                db.setPassword(databasePassword);
                        db.open();
                        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,data.ERRORS.DATABASE_SELECT_NOT_WORKING_TITLE("CF"),data.ERRORS.DATABASE_SELECT_NOT_WORKING_BODY("CF")+query->lastError().text());
                            }
                
                        }else{
                            QMessageBox::warning(this,data.ERRORS.DATABASE_NOT_CONNECTED_TITLE("CF"),data.ERRORS.DATABASE_NOT_CONNECTED_BODY("CF")+db.lastError().text());
                        }
                
                    }
                    QSqlDatabase::removeDatabase("qt_sql_default_connection"); // removing database
                

                If I put the message box inside the query, then while the messagebox is open then the table is filled , as soon as I close the messagebox then data vanish but the rows with empty data remains there WHY?

                 QString command = "select * from countryDetails;";
                    {
                        QSqlDatabase db =  QSqlDatabase::addDatabase("QMYSQL");
                        db.setDatabaseName(databaseName);
                db.setPort(databasePort);
                db.setHostName(databaseHostname);
                db.setUserName(databaseUsername);
                db.setPassword(databasePassword);
                        db.open();
                        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);
                QMessageBox::information(this,"Now it is seen", "seen now");
                //setting the model 
                            }else{
                                QMessageBox::warning(this,data.ERRORS.DATABASE_SELECT_NOT_WORKING_TITLE("CF"),data.ERRORS.DATABASE_SELECT_NOT_WORKING_BODY("CF")+query->lastError().text());
                            }
                
                        }else{
                            QMessageBox::warning(this,data.ERRORS.DATABASE_NOT_CONNECTED_TITLE("CF"),data.ERRORS.DATABASE_NOT_CONNECTED_BODY("CF")+db.lastError().text());
                        }
                
                    }
                    QSqlDatabase::removeDatabase("qt_sql_default_connection"); // removing database
                
                J Offline
                J Offline
                JonB
                wrote on 28 May 2021, 07:06 last edited by JonB
                #7

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

                    db.open();
                    if(db.open()){
                

                And don't do that, it makes no sense, if you understand what you are doing.

                T 1 Reply Last reply 28 May 2021, 07:12
                3
                • J JonB
                  28 May 2021, 07:06

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

                      db.open();
                      if(db.open()){
                  

                  And don't do that, it makes no sense, if you understand what you are doing.

                  T Offline
                  T Offline
                  Thank You
                  wrote on 28 May 2021, 07:12 last edited by Thank You
                  #8

                  @JonB Ok , I can remove it
                  But may be I was checking it only 🤣🤣🤣😂

                  Let's make QT free or It will go forever

                  TRUE AND FALSE <3

                  1 Reply Last reply
                  0
                  • Christian EhrlicherC Christian Ehrlicher
                    28 May 2021, 06:44

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

                    QSqlQuery *query = new QSqlQuery(db);

                    And don't create it on the heap - otherwise the query is leaking.

                    T Offline
                    T Offline
                    Thank You
                    wrote on 28 May 2021, 07:14 last edited by
                    #9

                    @Christian-Ehrlicher

                    QSqlQueryModel *model = new QSqlQueryModel();
                    QSqlQuery query(db);
                    query.prepare(command);
                     if(query.exec()){
                               model->setQuery(query);
                               ui->report->setModel(model);
                    }
                    

                    This one doesn't work either

                    How can I solve it?

                    Let's make QT free or It will go forever

                    TRUE AND FALSE <3

                    1 Reply Last reply
                    0
                    • Christian EhrlicherC Offline
                      Christian EhrlicherC Offline
                      Christian Ehrlicher
                      Lifetime Qt Champion
                      wrote on 28 May 2021, 07:16 last edited by
                      #10

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

                      How can I solve it?

                      Did you read @VRonin 's message?

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

                      T 1 Reply Last reply 28 May 2021, 07:21
                      1
                      • T Offline
                        T Offline
                        Thank You
                        wrote on 28 May 2021, 07:17 last edited by
                        #11

                        But why the same thing is working with
                        SQLITE
                        and not with
                        MYSQL

                        Let's make QT free or It will go forever

                        TRUE AND FALSE <3

                        J 1 Reply Last reply 28 May 2021, 07:21
                        0
                        • T Thank You
                          28 May 2021, 07:17

                          But why the same thing is working with
                          SQLITE
                          and not with
                          MYSQL

                          J Offline
                          J Offline
                          JonB
                          wrote on 28 May 2021, 07:21 last edited by
                          #12

                          @Thank-You
                          Who knows, they are different databases with different driver code & behaviour.

                          Why do you remove the database? What behaviour do you expect after the remove? Why do you care if the behaviour is different between SQLite versus MySQL if the code is wrong and can be corrected?

                          1 Reply Last reply
                          0
                          • Christian EhrlicherC Christian Ehrlicher
                            28 May 2021, 07:16

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

                            How can I solve it?

                            Did you read @VRonin 's message?

                            T Offline
                            T Offline
                            Thank You
                            wrote on 28 May 2021, 07:21 last edited by Thank You
                            #13

                            @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

                            Let's make QT free or It will go forever

                            TRUE AND FALSE <3

                            J jsulmJ 2 Replies Last reply 28 May 2021, 07:30
                            0
                            • T Thank You
                              28 May 2021, 07:21

                              @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

                              J Offline
                              J Offline
                              JonB
                              wrote on 28 May 2021, 07:30 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
                              • T Thank You
                                28 May 2021, 07:21

                                @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 28 May 2021, 07:31 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

                                T 1 Reply Last reply 28 May 2021, 07:36
                                1
                                • jsulmJ jsulm
                                  28 May 2021, 07:31

                                  @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...

                                  T Offline
                                  T Offline
                                  Thank You
                                  wrote on 28 May 2021, 07:36 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 J 2 Replies Last reply 28 May 2021, 07:39
                                  0
                                  • T Thank You
                                    28 May 2021, 07:36

                                    @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 28 May 2021, 07:39 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

                                    T 1 Reply Last reply 28 May 2021, 07:48
                                    2
                                    • T Thank You
                                      28 May 2021, 07:36

                                      @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

                                      J Offline
                                      J Offline
                                      JonB
                                      wrote on 28 May 2021, 07:41 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
                                        28 May 2021, 07:39

                                        @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?!

                                        T Offline
                                        T Offline
                                        Thank You
                                        wrote on 28 May 2021, 07:48 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

                                        J 1 Reply Last reply 28 May 2021, 07:57
                                        0
                                        • T Thank You
                                          28 May 2021, 07:48

                                          @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 ??

                                          J Offline
                                          J Offline
                                          JonB
                                          wrote on 28 May 2021, 07:57 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.

                                          T 1 Reply Last reply 28 May 2021, 08:05
                                          2

                                          1/31

                                          27 May 2021, 16:35

                                          • Login

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