Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Update all rows in QSqlRelationalTableModel



  • 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();
    	}
    


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



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



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



  • @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();?



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



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



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



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



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


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



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



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


Log in to reply