How to get the value in QSqlTableModel?

6.1k views Asked by At

My database:

enter image description here

My code:

QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
db.setDatabaseName("sqlite.db");
db.open();

QSqlQuery query;
query.exec("SELECT * from Expenses");

QSqlTableModel *model = new QSqlTableModel(0, db);
model->setTable("Expenses");
model->select();

How I can to get the value of TOTAL for "January 2014"? I.e. "1".

2

There are 2 answers

1
phyatt On BEST ANSWER

http://doc.qt.io/qt-4.8/qsqltablemodel.html#details

 QSqlQueryModel model;
 model.setQuery("SELECT * FROM employee");
 int salary = model.record(4).value("salary").toInt();

http://doc.qt.io/qt-4.8/qsqltablemodel-members.html

http://doc.qt.io/qt-4.8/qsqlquerymodel.html#record

http://doc.qt.io/qt-4.8/qsqltablemodel.html#rowCount

int second_record_total = model->record(1).value("total").toInt();

or

int row_count = model->rowCount();
int latest_total = model->record(row_count - 1).value("total").toInt();

Hope that helps.

0
Gavin On

for example:

QSqlTableModel *userModel=new QSqlTableModel(this);
userModel->setTable("person");
userModel->select();

const int row=0;

qDebug()<<userModel->index(row,9).data().toInt();