double sum from query.value()
Hello,
I have this situation:
Code:
query.prepare("SELECT * FROM mytable");
query.exec();
query.first();
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:
Code:
"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
Re: double sum from query.value()
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().
Re: double sum from query.value()
Quote:
Originally Posted by
d_stranz
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
Re: double sum from query.value()
Code:
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)
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.
Re: double sum from query.value()
Quote:
Originally Posted by
d_stranz
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:
Code:
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)
Re: double sum from query.value()
Quote:
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).
Quote:
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.
Re: double sum from query.value()
Quote:
Originally Posted by
d_stranz
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!