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. Update all rows in QSqlRelationalTableModel
Forum Updated to NodeBB v4.3 + New Features

Update all rows in QSqlRelationalTableModel

Scheduled Pinned Locked Moved Unsolved General and Desktop
13 Posts 3 Posters 950 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.
  • ZgemboZ Offline
    ZgemboZ Offline
    Zgembo
    wrote on last edited by Zgembo
    #1

    Hi all,

    I have a small problem regarding updating rows in QSqlRelationalTableModel. I manage to fetch all the data from the database in QSqlRelationalTableModel but when I try to update specific column in all rows it only updates first row.

    This is my code.

    QSqlRecord record;
    QString courseName;
    
    for (int i = 0; i < studentModel->rowCount(); i++) {
    		record = studentModel->record(i);
    		courseName = record.value("naziv").toString();
    		if (courseName.at(0) == 'U')
    			record.setValue("naziv", "UIS");
    		else if (courseName.at(0) == "T")
    			record.setValue("naziv", "TISZPK");
    		else if (courseName.at(0) == "R")
    			record.setValue("naziv", "RIS");
    		else if (courseName.at(0) == "W")
    			record.setValue("naziv", "WEB");
    
    		studentModel->setRecord(i, record);
    		studentModel->submitAll();
    	}
    
    JonBJ 1 Reply Last reply
    0
    • ZgemboZ Zgembo

      Hi all,

      I have a small problem regarding updating rows in QSqlRelationalTableModel. I manage to fetch all the data from the database in QSqlRelationalTableModel but when I try to update specific column in all rows it only updates first row.

      This is my code.

      QSqlRecord record;
      QString courseName;
      
      for (int i = 0; i < studentModel->rowCount(); i++) {
      		record = studentModel->record(i);
      		courseName = record.value("naziv").toString();
      		if (courseName.at(0) == 'U')
      			record.setValue("naziv", "UIS");
      		else if (courseName.at(0) == "T")
      			record.setValue("naziv", "TISZPK");
      		else if (courseName.at(0) == "R")
      			record.setValue("naziv", "RIS");
      		else if (courseName.at(0) == "W")
      			record.setValue("naziv", "WEB");
      
      		studentModel->setRecord(i, record);
      		studentModel->submitAll();
      	}
      
      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by
      #2

      @Zgembo
      I don't see what this has to do with QSqlRelationalTableModel, but never mind.

      Start by demonstrating how many times you go through the loop. Also demonstrate what it is you claim to be changing in the records which will cause an update in the database table, because it looks to me as though your updates change nothing from what is already there....

      ZgemboZ 1 Reply Last reply
      0
      • artwawA Offline
        artwawA Offline
        artwaw
        wrote on last edited by
        #3

        To add to @JonB - that submit (assuming you've setup the model to require one?) should be at the end of the loop. After it is done.
        Could you also share the structure of the tables please (I assume there's more than one since you use relations)? Knowing the relations and how you've set them up might be of help also.

        For more information please re-read.

        Kind Regards,
        Artur

        1 Reply Last reply
        0
        • JonBJ JonB

          @Zgembo
          I don't see what this has to do with QSqlRelationalTableModel, but never mind.

          Start by demonstrating how many times you go through the loop. Also demonstrate what it is you claim to be changing in the records which will cause an update in the database table, because it looks to me as though your updates change nothing from what is already there....

          ZgemboZ Offline
          ZgemboZ Offline
          Zgembo
          wrote on last edited by
          #4

          @JonB Hi
          studentModel is QSqlRelationalTableModel. I am retrieving approximately 20 rows from the database. Before sending the data to the GUI I would like to update all the rows in "naziv" column. I am not trying to save this updated data to the database, that is not the point. Maybe my logic is wrong.

          Tnx

          JonBJ 1 Reply Last reply
          0
          • ZgemboZ Zgembo

            @JonB Hi
            studentModel is QSqlRelationalTableModel. I am retrieving approximately 20 rows from the database. Before sending the data to the GUI I would like to update all the rows in "naziv" column. I am not trying to save this updated data to the database, that is not the point. Maybe my logic is wrong.

            Tnx

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

            @Zgembo said in Update all rows in QSqlRelationalTableModel:

            I am not trying to save this updated data to the database

            Then what is studentModel->submitAll();?

            ZgemboZ 1 Reply Last reply
            0
            • JonBJ JonB

              @Zgembo said in Update all rows in QSqlRelationalTableModel:

              I am not trying to save this updated data to the database

              Then what is studentModel->submitAll();?

              ZgemboZ Offline
              ZgemboZ Offline
              Zgembo
              wrote on last edited by
              #6

              @JonB said in Update all rows in QSqlRelationalTableModel:

              @Zgembo said in Update all rows in QSqlRelationalTableModel:

              I am not trying to save this updated data to the database

              Then what is studentModel->submitAll();?

              Looks like a mistake :-) I will try without it.

              JonBJ 1 Reply Last reply
              0
              • ZgemboZ Zgembo

                @JonB said in Update all rows in QSqlRelationalTableModel:

                @Zgembo said in Update all rows in QSqlRelationalTableModel:

                I am not trying to save this updated data to the database

                Then what is studentModel->submitAll();?

                Looks like a mistake :-) I will try without it.

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

                @Zgembo
                I don't know what you are or are not trying to achieve at all :)

                What about @artwaw's:

                Could you also share the structure of the tables please (I assume there's more than one since you use relations)? Knowing the relations and how you've set them up might be of help also.

                ?

                I wonder how/why you're using QSqlRelationalTableModel, and how this is connected to the code you show which seems to want to make updates....

                ZgemboZ 1 Reply Last reply
                0
                • JonBJ JonB

                  @Zgembo
                  I don't know what you are or are not trying to achieve at all :)

                  What about @artwaw's:

                  Could you also share the structure of the tables please (I assume there's more than one since you use relations)? Knowing the relations and how you've set them up might be of help also.

                  ?

                  I wonder how/why you're using QSqlRelationalTableModel, and how this is connected to the code you show which seems to want to make updates....

                  ZgemboZ Offline
                  ZgemboZ Offline
                  Zgembo
                  wrote on last edited by Zgembo
                  #8

                  @JonB
                  This is the complete code.

                  QSqlRelationalTableModel * Students::getEnrolledStudents()
                  {
                  	studentModel->setTable("numofstudentsgraph");
                  	studentModel->setSort(0, Qt::AscendingOrder);
                  	studentModel->setHeaderData(0, Qt::Horizontal, "courseYear");
                  	studentModel->setHeaderData(1, Qt::Horizontal, "coursename");
                  	studentModel->setHeaderData(2, Qt::Horizontal, "numOfStudents");
                  
                  	studentModel->select();
                  
                  	for (int i = 0; i < studentModel->rowCount(); i++) {
                  		QSqlRecord record = studentModel->record(i);
                  		QString courseName = record.value("naziv").toString();
                  
                  		if (courseName.at(0) == 'U')
                  			record.setValue("naziv", "UIS");
                  		else if (courseName.at(0) == "T")
                  			record.setValue("naziv", "TISZPK");
                  		else if (courseName.at(0) == "R")
                  			record.setValue("naziv", "RIS");
                  		else if (courseName.at(0) == "W")
                  			record.setValue("naziv", "WEB");
                  
                  		studentModel->setRecord(i, record);
                  	}
                  	//studentModel->submitAll();
                  
                  	return studentModel;
                  }
                  

                  And it does not update required column.

                  JonBJ 1 Reply Last reply
                  0
                  • ZgemboZ Zgembo

                    @JonB
                    This is the complete code.

                    QSqlRelationalTableModel * Students::getEnrolledStudents()
                    {
                    	studentModel->setTable("numofstudentsgraph");
                    	studentModel->setSort(0, Qt::AscendingOrder);
                    	studentModel->setHeaderData(0, Qt::Horizontal, "courseYear");
                    	studentModel->setHeaderData(1, Qt::Horizontal, "coursename");
                    	studentModel->setHeaderData(2, Qt::Horizontal, "numOfStudents");
                    
                    	studentModel->select();
                    
                    	for (int i = 0; i < studentModel->rowCount(); i++) {
                    		QSqlRecord record = studentModel->record(i);
                    		QString courseName = record.value("naziv").toString();
                    
                    		if (courseName.at(0) == 'U')
                    			record.setValue("naziv", "UIS");
                    		else if (courseName.at(0) == "T")
                    			record.setValue("naziv", "TISZPK");
                    		else if (courseName.at(0) == "R")
                    			record.setValue("naziv", "RIS");
                    		else if (courseName.at(0) == "W")
                    			record.setValue("naziv", "WEB");
                    
                    		studentModel->setRecord(i, record);
                    	}
                    	//studentModel->submitAll();
                    
                    	return studentModel;
                    }
                    

                    And it does not update required column.

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

                    @Zgembo

                    1. If that is the complete code, why are you using a QSqlRelationalTableModel? You are not setting any relation....

                    2. As I said, show me what is going wrong. You said something about 20 rows with the first row updating and not others. Reduce your table/loop to 2 rows, put in qDebug()s after the QSqlRecord record = studentModel->record(i); showing me the relevant column you retrieve and what you change it to with the studentModel->setRecord(i, record);, and therefore how you know that whatever does not work as you expect. For example, for all I know your studentModel->rowCount() only returns 1.

                    ZgemboZ 1 Reply Last reply
                    0
                    • JonBJ JonB

                      @Zgembo

                      1. If that is the complete code, why are you using a QSqlRelationalTableModel? You are not setting any relation....

                      2. As I said, show me what is going wrong. You said something about 20 rows with the first row updating and not others. Reduce your table/loop to 2 rows, put in qDebug()s after the QSqlRecord record = studentModel->record(i); showing me the relevant column you retrieve and what you change it to with the studentModel->setRecord(i, record);, and therefore how you know that whatever does not work as you expect. For example, for all I know your studentModel->rowCount() only returns 1.

                      ZgemboZ Offline
                      ZgemboZ Offline
                      Zgembo
                      wrote on last edited by Zgembo
                      #10

                      @JonB said in Update all rows in QSqlRelationalTableModel:

                      @Zgembo

                      1. If that is the complete code, why are you using a QSqlRelationalTableModel? You are not setting any relation....

                      2. As I said, show me what is going wrong. You said something about 20 rows with the first row updating and not others. Reduce your table/loop to 2 rows, put in qDebug()s after the QSqlRecord record = studentModel->record(i); showing me the relevant column you retrieve and what you change it to with the studentModel->setRecord(i, record);, and therefore how you know that whatever does not work as you expect. For example, for all I know your studentModel->rowCount() only returns 1.

                      1. I could use QSqlTableModel it does not change much of the code.
                      2. I can go step by step through the loop and I can see the data retrieved from the database but as I already said I am unable to modify data in specific column. I can only modify first row.
                      JonBJ 1 Reply Last reply
                      0
                      • ZgemboZ Zgembo

                        @JonB said in Update all rows in QSqlRelationalTableModel:

                        @Zgembo

                        1. If that is the complete code, why are you using a QSqlRelationalTableModel? You are not setting any relation....

                        2. As I said, show me what is going wrong. You said something about 20 rows with the first row updating and not others. Reduce your table/loop to 2 rows, put in qDebug()s after the QSqlRecord record = studentModel->record(i); showing me the relevant column you retrieve and what you change it to with the studentModel->setRecord(i, record);, and therefore how you know that whatever does not work as you expect. For example, for all I know your studentModel->rowCount() only returns 1.

                        1. I could use QSqlTableModel it does not change much of the code.
                        2. I can go step by step through the loop and I can see the data retrieved from the database but as I already said I am unable to modify data in specific column. I can only modify first row.
                        JonBJ Offline
                        JonBJ Offline
                        JonB
                        wrote on last edited by
                        #11

                        @Zgembo
                        That's fine, you don't want to put in the necessary to show what is going on, hopefully someone else can help you without that information then, best of luck.

                        ZgemboZ 1 Reply Last reply
                        0
                        • JonBJ JonB

                          @Zgembo
                          That's fine, you don't want to put in the necessary to show what is going on, hopefully someone else can help you without that information then, best of luck.

                          ZgemboZ Offline
                          ZgemboZ Offline
                          Zgembo
                          wrote on last edited by
                          #12

                          @JonB said in Update all rows in QSqlRelationalTableModel:

                          @Zgembo
                          That's fine, you don't want to put in the necessary to show what is going on, hopefully someone else can help you without that information then, best of luck.

                          a3352a5c-3f9d-4c6c-ad9d-cbe564c5c723-image.png

                          JonBJ 1 Reply Last reply
                          0
                          • ZgemboZ Zgembo

                            @JonB said in Update all rows in QSqlRelationalTableModel:

                            @Zgembo
                            That's fine, you don't want to put in the necessary to show what is going on, hopefully someone else can help you without that information then, best of luck.

                            a3352a5c-3f9d-4c6c-ad9d-cbe564c5c723-image.png

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

                            @Zgembo
                            studentModel->setRecord(i, record); QSqlTableModel::setRecord() returns a bool, https://doc.qt.io/qt-5/qsqltablemodel.html#setRecord:

                            Returns true if all the values could be set; otherwise returns false.

                            Why don't you start by printing that out?

                            And while you are there you will have noticed:

                            For edit strategies OnFieldChange and OnRowChange, a row may receive a change only if no other row has a cached change.

                            Do you think this may be why you can change the first row but not subsequent ones?

                            1 Reply Last reply
                            2

                            • Login

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