PDA

View Full Version : Updating rows via QSqlTableModel - only last row really updates.



unnamed
11th October 2012, 10:25
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:



QSqlQuery rquery(remotedb);
rquery.prepare("Select * from wbn where insp_nip = :nip");
rquery.bindValue(":nip", "9262485545"); //selecting values from remote database
if(rquery.exec()){
while(rquery.next()){
QSqlQuery query;
query.prepare("Select id, count(*) from wbn where database_id = :id");
query.bindValue(":id", rquery.value(0).toInt()); //getting id and count from local database to see if we need to insert or update
query.next();
int id = 0;
if (!query.exec() || !query.first()){
qDebug() << query.lastError().text();
}else if (query.value(1) == 0){
//We need to insert, this part works great;
id = accomodationsModel->rowCount();
qDebug() << "id = " << id;
accomodationsModel->insertRow(id);
accomodationsModel->setData(accomodationsModel->index(id, 1), rquery.value(0).toInt());
}else{
//We need to update, from debuging everything seems fine
int id = query.value(0).toInt();
for(int row=0; row < accomodationsModel->rowCount(); ++row)
{
if (accomodationsModel->index(row, 0).data(Qt::DisplayRole).toInt() == id )
id = row;
}
//primary key to QSqlTableModel row, any better way to do it ?
}

for(int i = 2; i < 45; i++){
QString field = rquery.value(i).toString();
accomodationsModel->setData(accomodationsModel->index(id, i), field);
//seting data to model
}

}
ret = accomodationsModel->submitAll();
//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 ?


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 :)