Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [SOLVED] How to convert a double to a QString for writing into a SQL table (QString::number not working properly)?
QtWS25 Last Chance

[SOLVED] How to convert a double to a QString for writing into a SQL table (QString::number not working properly)?

Scheduled Pinned Locked Moved General and Desktop
mysqldoubleconversionqstring
3 Posts 2 Posters 11.2k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Offline
    R Offline
    RolBri
    wrote on last edited by RolBri
    #1

    Hi,

    I would like to write double values into a MySQL table.
    In general writing works and I also have declared the fields in the MySQL table as double values.

    I tried to bind the doubles directly and this works half.
    If I use something like this:

    double angle= 29.6481;
    double duration=222.333;
    
    QSqlQuery insert;
            insert.prepare("INSERT INTO messung (winkel, millisekunden)" "VALUES (:winkel, :millisekunden)");
            insert.bindValue(":winkel", angle);
            insert.bindValue(":millisekunden", QString::number(duration,'f', 2));
            insert.exec();
    

    Then I have the values 29 and 222 in my MySQL table.

    If I try this

     qDebug() << QString::number(angle,'f', 2);
    

    I get 29.00 in the console.

    How can I send the real values without rounding to my MySQL table?

    Thank you very much :-)

    1 Reply Last reply
    0
    • R Offline
      R Offline
      Rondog
      wrote on last edited by
      #2

      This line

      qDebug() << QString::number(angle,'f', 2);
      

      should not show '29.00' on the output unless the value has been altered since assigned the value 29.6481 when declared. It could have been truncated if assigned or cast to an integer somewhere prior to this qDebug() line perhaps.

      One option to eliminate rounding, if possible, is to have the table setup with floating point types as opposed to using a string. If the table already exists and cannot be changed then this may not be an option unless the related software is adjusted.

      On a side note another variation of number conversion to a string is this:

      qDebug() << QString("%1").arg(angle,'f', 2);
      

      It is handy if you want to add additional text with the number (this includes a new line character or possibly a tab or comma).

      1 Reply Last reply
      0
      • R Offline
        R Offline
        RolBri
        wrote on last edited by
        #3

        Thank you very much :-)

        You gave me the right hint.

        There was a cast before which I did not see.
        And since everything was casted to int MySQL deleted the Zeros after the '.'

        Now everything works fine and thanks for the other variation :-)

        1 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved