Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Unsolved How to correctly read DECIMAL values from MySQL with QSqlQuery

    General and Desktop
    mysql qsqlquery
    2
    4
    1518
    Loading More Posts
    • 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.
    • D
      deleted602 last edited by deleted602

      Having a table in MySQL with a column of type DECIMAL, I am wondering how to read this value properly in my Qt application. To me it seems like it is handled like a double when you retrieve it using QSqlQuery. Is this correct? Doesn't this risk losing precision?

      To quote https://spin.atomicobject.com/2014/08/14/currency-rounding-errors/

      Now that the database is storing the monetary values accurately, you don’t want to do anything to lose that accuracy when retrieving values from it. [...] when using Java’s Hibernate ORM, you need to specify the variable as a BigDecimal not a Float or Double

      1 Reply Last reply Reply Quote 0
      • SGaist
        SGaist Lifetime Qt Champion last edited by

        Hi,

        Indeed, DECIMAL is mapped to double in the MySQL plugin.

        You should do some testing with the values you are going to use.

        Interested in AI ? www.idiap.ch
        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

        1 Reply Last reply Reply Quote 0
        • D
          deleted602 last edited by

          What does this mean exactly? That the SQL classes of Qt cannot be used if you want the precision of DECIMAL?

          1 Reply Last reply Reply Quote 0
          • SGaist
            SGaist Lifetime Qt Champion last edited by

            As it is now, double is used.

            Depending on how your DECIMAL filed is configured, it might be enough. If not, then you can modify the plugin to read the field as described here.

            Interested in AI ? www.idiap.ch
            Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

            1 Reply Last reply Reply Quote 0
            • First post
              Last post