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. How to correctly read DECIMAL values from MySQL with QSqlQuery

How to correctly read DECIMAL values from MySQL with QSqlQuery

Scheduled Pinned Locked Moved Unsolved General and Desktop
mysqlqsqlquery
4 Posts 2 Posters 2.0k 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.
  • D Offline
    D Offline
    deleted602
    wrote on last edited by deleted602
    #1

    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
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      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
      0
      • D Offline
        D Offline
        deleted602
        wrote on last edited by
        #3

        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
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          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
          0

          • Login

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