PDA

View Full Version : double sum from query.value()



juliano.gomes
14th December 2016, 22:45
Hello,

I have this situation:



QSqlQuery query;
query.prepare("SELECT * FROM mytable");
query.exec();
query.first();

QString value = "";
while (query.next())
{
value = query.value(0).toString();
cout << value.toStdString() << "\n";
}
in my output i get the 2 values below:
50.450,50
85.336,50

with SQL i can do it as below:

"SELECT TOTAL(myfield) FROM mytable"
but i need to do it in my C++ code, so

How could I sum these type of values in my while clause?

Thanks
Juliano

d_stranz
15th December 2016, 00:30
If the numbers are really stored in your table as strings and not floating point numbers, then QString::toDouble() should help. Otherwise if they are stored as floating point, just use QVariant::toDouble() on your QSqlQuery::value().

juliano.gomes
15th December 2016, 01:37
If the numbers are really stored in your table as strings and not floating point numbers, then QString::toDouble() should help. Otherwise if they are stored as floating point, just use QVariant::toDouble() on your QSqlQuery::value().

I'm using Sqlite3
my table field is REAL type

sorry, but I'm trying here, without success

can you give me and example?

thanks
Juliano

d_stranz
15th December 2016, 19:27
double value;
while (query.next())
{
value = query.value(0).toDouble();
cout << value << "\n";
}


// Or (although it is dumb to do this if the value is already stored as a floating point REAL)

QString value = "";
while (query.next())
{
value = query.value(0).toString();
cout << value.toDouble() << "\n";
}


I'm not going to tell you how to add up the values to get the sum. If you don't know how to do that, you need to go back and hit the C++ books.

juliano.gomes
15th December 2016, 20:31
I'm not going to tell you how to add up the values to get the sum. If you don't know how to do that, you need to go back and hit the C++ books.

Ok, thanks for your support.

"the solution" after a day cracking my brain:



double a = 0;
while (query2.next())
{
a = a + query2.value(5).toDouble();
cout << a;
}

Note: the numbers stored in the table can not be in the format "000.000,00" or "000000,00" , but "000,000" or "000000" (the point/comma gave me problems in that case)

d_stranz
16th December 2016, 18:20
Note: the numbers stored in the table can not be in the format "000.000,00" or "000000,00" , but "000,000" or "000000"

Wrong. If the SQL data type for the column in your table is "REAL", then they are stored in binary floating point, not in any of the string formats you list here. It is the conversion your code does after retrieving the value from the database that adds the formatting.

When your code converts the return value from the query to a string (using QSqlQuery::value() and QVariant::toString()) and displays the resulting QString as you showed in your original post, Qt converts the double to a string according to its default floating point format and your locale. It is your locale that inserts the "," and "." characters into the string, and the default format that decides how many digits of precision after the decimals separator ("," in your locale, "." in mine).


Ok, thanks for your support.

Look, we aren't here to teach you how to write basic C++. I gave you code that retrieves the values from your table as floating point numbers (doubles in this case). If you seriously don't know how to write a loop that adds those numbers up, then you do need to learn the basics of C++ before you try to write Qt code.

juliano.gomes
16th December 2016, 19:28
Look, we aren't here to teach you how to write basic C++... I gave you code that retrieves the values from your table as floating point numbers (doubles in this case). If you seriously don't know how to write a loop that adds those numbers up, then you do need to learn the basics of C++ before you try to write Qt code.

All right, thank you for your great wisdom, kindness, and humility. you are the man!