Results 1 to 7 of 7

Thread: double sum from query.value()

  1. #1
    Join Date
    Jun 2015
    Posts
    28
    Qt products
    Qt5
    Platforms
    Unix/X11

    Default double sum from query.value()

    Hello,

    I have this situation:

    Qt Code:
    1. QSqlQuery query;
    2. query.prepare("SELECT * FROM mytable");
    3. query.exec();
    4. query.first();
    5.  
    6. QString value = "";
    7. while (query.next())
    8. {
    9. value = query.value(0).toString();
    10. cout << value.toStdString() << "\n";
    11. }
    12. in my output i get the 2 values below:
    13. 50.450,50
    14. 85.336,50
    To copy to clipboard, switch view to plain text mode 
    with SQL i can do it as below:
    Qt Code:
    1. "SELECT TOTAL(myfield) FROM mytable"
    To copy to clipboard, switch view to plain text mode 
    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

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default 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().
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  3. #3
    Join Date
    Jun 2015
    Posts
    28
    Qt products
    Qt5
    Platforms
    Unix/X11

    Default Re: double sum from query.value()

    Quote Originally Posted by d_stranz View Post
    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

  4. #4
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: double sum from query.value()

    Qt Code:
    1. double value;
    2. while (query.next())
    3. {
    4. value = query.value(0).toDouble();
    5. cout << value << "\n";
    6. }
    7.  
    8.  
    9. // Or (although it is dumb to do this if the value is already stored as a floating point REAL)
    10.  
    11. QString value = "";
    12. while (query.next())
    13. {
    14. value = query.value(0).toString();
    15. cout << value.toDouble() << "\n";
    16. }
    To copy to clipboard, switch view to plain text mode 

    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.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  5. #5
    Join Date
    Jun 2015
    Posts
    28
    Qt products
    Qt5
    Platforms
    Unix/X11

    Default Re: double sum from query.value()

    Quote Originally Posted by d_stranz View Post
    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:

    Qt Code:
    1. double a = 0;
    2. while (query2.next())
    3. {
    4. a = a + query2.value(5).toDouble();
    5. cout << a;
    6. }
    To copy to clipboard, switch view to plain text mode 
    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)
    Last edited by juliano.gomes; 16th December 2016 at 01:13.

  6. #6
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: double sum from query.value()

    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.
    Last edited by d_stranz; 16th December 2016 at 17:24.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  7. #7
    Join Date
    Jun 2015
    Posts
    28
    Qt products
    Qt5
    Platforms
    Unix/X11

    Default Re: double sum from query.value()

    Quote Originally Posted by d_stranz View Post
    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!

Similar Threads

  1. Replies: 7
    Last Post: 16th April 2015, 17:11
  2. QList<double> => QVariant => QList<double>
    By stefan in forum Qt Programming
    Replies: 3
    Last Post: 12th October 2013, 11:17
  3. Program not finding an existing double in a QList<double>
    By aarelovich in forum Qt Programming
    Replies: 2
    Last Post: 9th May 2011, 20:59
  4. MS SQL Query
    By baray98 in forum General Programming
    Replies: 0
    Last Post: 14th July 2009, 04:25
  5. Replies: 2
    Last Post: 24th June 2009, 15:38

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.