Results 1 to 1 of 1

Thread: Updating rows via QSqlTableModel - only last row really updates.

  1. #1
    Join Date
    Oct 2012
    Posts
    5
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: Updating rows via QSqlTableModel - only last row really updates.

    Hi, i couldn't find answer to my question, so here it is:

    I'am trying to update QSqlTableModel rows but only last row is updated, here is my code:

    Qt Code:
    1. QSqlQuery rquery(remotedb);
    2. rquery.prepare("Select * from wbn where insp_nip = :nip");
    3. rquery.bindValue(":nip", "9262485545"); //selecting values from remote database
    4. if(rquery.exec()){
    5. while(rquery.next()){
    6. QSqlQuery query;
    7. query.prepare("Select id, count(*) from wbn where database_id = :id");
    8. query.bindValue(":id", rquery.value(0).toInt()); //getting id and count from local database to see if we need to insert or update
    9. query.next();
    10. int id = 0;
    11. if (!query.exec() || !query.first()){
    12. qDebug() << query.lastError().text();
    13. }else if (query.value(1) == 0){
    14. //We need to insert, this part works great;
    15. id = accomodationsModel->rowCount();
    16. qDebug() << "id = " << id;
    17. accomodationsModel->insertRow(id);
    18. accomodationsModel->setData(accomodationsModel->index(id, 1), rquery.value(0).toInt());
    19. }else{
    20. //We need to update, from debuging everything seems fine
    21. int id = query.value(0).toInt();
    22. for(int row=0; row < accomodationsModel->rowCount(); ++row)
    23. {
    24. if (accomodationsModel->index(row, 0).data(Qt::DisplayRole).toInt() == id )
    25. id = row;
    26. }
    27. //primary key to QSqlTableModel row, any better way to do it ?
    28. }
    29.  
    30. for(int i = 2; i < 45; i++){
    31. QString field = rquery.value(i).toString();
    32. accomodationsModel->setData(accomodationsModel->index(id, i), field);
    33. //seting data to model
    34. }
    35.  
    36. }
    37. ret = accomodationsModel->submitAll();
    38. //submiting all, here it fails, there is no error, but only last QSqlTableModel row is really updated, from qDebug i can see that setData is setting good values, so problem is only with submitAll() i think ?
    To copy to clipboard, switch view to plain text mode 

    please read my comments in code. what can be the couse of updating only last row of model ?
    i have tried many things but none of them have worked for me.

    thanks for any help


    Added after 25 minutes:


    ok, so like always, after posting to forum i figured it out... please remove it as i can't find how to delete my post.
    sorry for any problems
    Last edited by unnamed; 11th October 2012 at 11:25.

Similar Threads

  1. Replies: 8
    Last Post: 30th March 2011, 21:06
  2. Sum of rows in QSqlTableModel
    By aekilic in forum Qt Programming
    Replies: 2
    Last Post: 24th October 2010, 21:05
  3. Replies: 2
    Last Post: 25th January 2010, 21:56
  4. QSqlTableModel inserts empty rows
    By Nesbitt in forum Qt Programming
    Replies: 2
    Last Post: 6th August 2008, 13:47
  5. Treeview scrollbar not updating when inserting rows
    By Khal Drogo in forum Qt Programming
    Replies: 11
    Last Post: 29th November 2007, 14:13

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.