Question

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".

Was it helpful?

Solution

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.

OTHER TIPS

for example:

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

const int row=0;

qDebug()<<userModel->index(row,9).data().toInt();
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top