PDA

View Full Version : QSqlQuery: remove a record



Evgenij
15th May 2014, 14:04
I tried removing some records from QSqlQuery using query.record().remove(pos) but it doesn't work at all. Is there any way to remove the record from QSqlQuery? I know about QSqlQueryModel, but I would like to use QSqlQuery, because it works fine for my application.

There is the code:

#include "QtSql/QtSql"
#include "QtSql/QSqlRecord"

QString sql_str;
sql_str = "select * from param";
QSqlQuery query;
query.prepare(sql_str);
query.exec();

int index=0;

query.seek(-1);

while(query.next()){
query.record().clear();
query.record().clearValues();
query.record().remove(0);
ui->tableWidget_2->setItem(index,0,new QTableWidgetItem(query.record().value(0).toString( )));
ui->tableWidget_2->setItem(index,1,new QTableWidgetItem(query.value(1).toString()));
ui->tableWidget_2->setItem(index,2,new QTableWidgetItem(query.value(2).toString()));
ui->tableWidget_2->setItem(index,3,new QTableWidgetItem(query.value(3).toString()));
index++;
}


I use Qt Creator 1.3.0 based on Qt 4.6.0 (32-bit)
Nov 27 2009 at 14:53:38
Revision c0e849ecc3

ChrisW67
16th May 2014, 23:26
It is difficult to work out what you are trying to achieve, but none of your calls to QSqlRecord::clear() and friends will have any effect on the data seen in the code following. These calls are all operating on a temporary QSqlRecord that you discard immediately.
If you do not want the first column in the result set then do not select it in the first place ("SELECT *" is a construct that should generally be avoided anyway). Alternatively, just ignore index zero and load your from query.value(1) on.