Results 1 to 2 of 2

Thread: SetRecord change 2 record instead who I want to.

  1. #1
    Join Date
    Sep 2009
    Posts
    4
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Question SetRecord change 2 record instead who I want to.

    hi friends,
    I have one databases table with some fields, but this table (in a mysql database) doesn't have primary key. Table's example.

    | field_1 field_2 field_3
    record 1 | 12 hello 100
    record 2 | 11 hi 402
    record 3 | 12 bye 102

    so, I wanna sincronize this table with another like this. (same structure)

    | field_1 field_2 field_3
    record 1 | 10 hello 100
    record 2 | 11 hi 402
    record 3 | 12 bye 102

    I am using QSqlTableModel for read local and remote tables. Then I do

    querylocal.size() is equal to 3

    Qt Code:
    1. for (int i=0; i < querylocal.size(); i++){
    2. if (localModel.record(i) != remotoModel.record(i)) {
    3. if (remotoModel.record(i).value(0).isNull()){ // local record doesn't exist in remote table.
    4. if (remotoModel.insertRecord(i,localModel.record(i))){
    5. remotoModel.submitAll();
    6. qDebug() << "added...";
    7. }
    8. } else {
    9. if (remotoModel.setRecord(i,localModel.record(i))){ // replace current record with localModel record.
    10. remotoModel.submitAll();
    11. qDebug() << "changed...";
    12.  
    13. }
    14.  
    15. }
    16. }
    17. }
    To copy to clipboard, switch view to plain text mode 

    if no record in remote table,
    this add records correctly, but when in remote table exist 3 records like my example, then sincronization result is..

    | field_1 field_2 field_3
    record 1 | 12 bye 102
    record 2 | 11 hi 402
    record 3 | 12 bye 102


    and not (on field_1) 10,11,12...

    I guess setRecord is updating two record ( first one and last one), because if I debug this application, result is :

    (record 1,2,3 on field_1)

    on local table: 10,11,12
    on remote table : 12,11,12

    step 1: 10,11,10 **** here setRecord should to change only first record, not the last one too.
    step 2: 10,11,10
    step 3: 12,11,12 **** same problem here.

    SetRecord(row,QSqlRecord) help says:
    Sets the values at the specified row to the values of record.

    row is changed for i in my code and record getting from my local table in LocalTable.record(i)

    so, I can't see where is my error.
    Thank you all for your help.

    Gaston.
    Last edited by wysota; 13th March 2011 at 22:55.

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,365
    Thanks
    3
    Thanked 5,014 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: SetRecord change 2 record instead who I want to.

    Your error is that there is no primary key in your table.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. Replies: 7
    Last Post: 27th November 2010, 15:55
  2. Replies: 3
    Last Post: 26th March 2010, 04:32
  3. Record video
    By tanatos in forum Qt Programming
    Replies: 4
    Last Post: 17th March 2010, 05:33
  4. Selected Record...
    By Nefastious in forum Newbie
    Replies: 1
    Last Post: 27th October 2009, 09:54
  5. Inserting Record
    By Nefastious in forum Newbie
    Replies: 3
    Last Post: 20th October 2009, 04:28

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.