Results 1 to 3 of 3

Thread: QSqlQuery and rounding off of data

  1. #1
    Join Date
    Oct 2009
    Location
    South Africa
    Posts
    94
    Thanks
    4
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default QSqlQuery and rounding off of data

    I have created a QSqlQuery to access a MySQL database.
    When I run the query against the database, it rounds to required 2 digits, however the QSqlQuery returns 17 digits.
    Any idea why this happens? I don't necessarily want to format the returned value, I want the database to return the formatted value, hence I include it in the query.

    This is my query... (and following that is my function to access the database)
    The 2 fields returned are supposed to be 0.26 & 0.46.
    Instead I get 0.26000000000000001 & 0.46000000000000002

    SELECT ROUND(c1/TIME_TO_SEC(TIMEDIFF(now(), '2016/05/16 06:00:00')),2) AS p1,
    ROUND(c2/TIME_TO_SEC(TIMEDIFF(now(), '2016/05/16 06:00:00')),2) AS p2 FROM
    (SELECT SUM(TIME_TO_SEC(TIMEDIFF(cycle_end, cycle_start))) AS c1 FROM daq_iv.dae005_cycles WHERE pallet_no = 1 AND cycle_start >= '2016/05/16 06:00:00')t1,
    (SELECT SUM(TIME_TO_SEC(TIMEDIFF(cycle_end, cycle_start))) AS c2 FROM daq_iv.dae005_cycles WHERE pallet_no = 2 AND cycle_start >= '2016/05/16 06:00:00')t2
    Qt Code:
    1. QStringList DBMySQL::getData(QString queryString)
    2. {
    3. if (m_DBOpen) {
    4. QSqlQuery query(QSqlDatabase::database(m_ConnectionName)); //create QSqlQuery object using given database
    5. query.setForwardOnly(true); //sets mode to traverse forwards only through result set, potentially improves speed
    6. if (!(query.exec(queryString))) { //if succesful, returns true else false
    7. sl << query.lastError().text(); //error executing query, return the error
    8. }
    9. else {
    10. int fieldCount = query.record().count();
    11. while (query.next()) {
    12. QString str("");
    13. for (int i = 1; i <= fieldCount ; ++i) { //return each row as a comma deliminated string
    14. if (i == fieldCount)
    15. str = str + query.value(i-1).toString();
    16. else
    17. str = str + query.value(i-1).toString() + "|";
    18. }
    19. sl << str;
    20. }
    21. }
    22. }
    23. else {
    24. sl << "DB not open";
    25. }
    26. return sl;
    27. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QSqlQuery and rounding off of data

    My guess would be that "ROUND" just rounds the value and does not convert it to a string.
    So the type of the value is still a floating point number and the values you get are the closest possible to the rounded values.

    Again assuming that ROUND does not create a string, then that is missing from your query.

    Cheers,
    _

  3. #3
    Join Date
    Oct 2009
    Location
    South Africa
    Posts
    94
    Thanks
    4
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QSqlQuery and rounding off of data

    That makes sense.
    I formatted it in my code anyway now, so doesn't matter anymore.
    Thanks for the reply.

Similar Threads

  1. Replies: 5
    Last Post: 26th April 2016, 18:59
  2. Replies: 5
    Last Post: 6th April 2016, 20:31
  3. QSqlQuery poor performance retrieving data
    By croscato in forum Qt Programming
    Replies: 7
    Last Post: 18th November 2014, 17:22
  4. Replies: 1
    Last Post: 20th May 2009, 20:36
  5. Replies: 2
    Last Post: 27th April 2009, 15:15

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
  •  
Qt is a trademark of The Qt Company.