PDA

View Full Version : SetRecord change 2 record instead who I want to.



shadowroot
13th March 2011, 21:50
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


for (int i=0; i < querylocal.size(); i++){
if (localModel.record(i) != remotoModel.record(i)) {
if (remotoModel.record(i).value(0).isNull()){ // local record doesn't exist in remote table.
if (remotoModel.insertRecord(i,localModel.record(i))) {
remotoModel.submitAll();
qDebug() << "added...";
}
} else {
if (remotoModel.setRecord(i,localModel.record(i))){ // replace current record with localModel record.
remotoModel.submitAll();
qDebug() << "changed...";

}

}
}
}

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.

wysota
13th March 2011, 22:57
Your error is that there is no primary key in your table.