PDA

View Full Version : How to extract all datas from a column of a table and add those?



RamrajCh
14th July 2019, 05:16
I tried to extract data from Amount column of my table and add all those and save them to string. But only first row is extracted,could anyone help?

Here is my codes:


QList<QString> Dbase_Cashier::getAmount()
{
QList<QString> amountdetails;
QSqlQuery qry;
qry.prepare(QString("SELECT Amount FROM Bill"));
if(!qry.exec())
{
qDebug()<<"Error in retriving data"<<qry.lastError();
}
else
{

if(qry.next())
{
QString i=qry.value(0).toString();
amountdetails.push_front(i);

}
else
{
qDebug()<<"not executing qyery";
}

}
qDebug()<<amountdetails.count();
return amountdetails;
}

And I have called above function as follows:


void CashierWindow::showAmount()
{
Dbase_Cashier db("SBS.db");
QList<QString> amountdetails=db.getAmount();

if(amountdetails.isEmpty())
{
qDebug()<<"empty";
}
else
{
qDebug()<<amountdetails.count();
}
}

Here,I got the console output as 1.

anda_skoa
14th July 2019, 08:57
You are only reading the first row, i.e. you are only calling QSqlQuery::next() once.

Imperative programming languages such as C++ use a concept called loops to run pieces of code multiple times.
See https://www.tutorialspoint.com/cplusplus/cpp_loop_types for example.

Part of this concept is to decide the continuation of the loop's execution based on a condition.
In your case a candidate for such a condition is the return value of QSelQuery::next().

Cheers,
_

RamrajCh
14th July 2019, 10:12
I actually don't understand what you mention?

How many times do I execute my loop for?

How do I know how many rows are there in that column,could you please help me with it?

How can I count no. of rows in that column?

anda_skoa
14th July 2019, 12:58
while(qry.next())
{
QString i=qry.value(0).toString();
amountdetails.push_front(i);
}


Cheers,
_