Uploaded image for project: 'Qt'
  1. Qt
  2. QTBUG-138642

ODBC possible data loss with 'real' column

XMLWordPrintable

    • All
    • 7f24d8952 (dev), 20b5d3d0e (6.10), 6084f1d45 (6.9), c083f18bd (tqtc/lts-6.8)

      When using QSqlDatabase / QSqlQuery to retrieve data from an MS SQL database through the ODBC driver, the data type real with the value 0.015625 is not preserved exactly and is instead retrieved as 0.01562 or 0.015619.

      To reproduce the issue, run the following commands in Microsoft SQL Server:

      int main(int argc, char *argv[])
      {
          qDebug() << "Qt version: " << QT_VERSION_STR;
          QApplication a(argc, argv);
      
          QSqlDatabase database = QSqlDatabase::addDatabase("QODBC");
          database.setDatabaseName("qt_test");
          database.setUserName("sa");
          database.setPassword("testuser");
          database.open();
      
          QSqlQuery q(database);
          q.exec("DROP TABLE IF EXISTS test_table");
          q.exec(R"(
      CREATE TABLE test_table (
          Id INT IDENTITY(1,1) PRIMARY KEY,
          Column1 REAL,
          Column2 FLOAT
      ); 
      )");
          q.exec("INSERT INTO test_table(Column1, Column2) Values (0.015625, 0.015625), (0.09375, 0.09375);");
          q.exec("SELECT Column1, Column2 FROM test_table");
          while (q.next()) {
              auto val1 = q.value(0).toDouble();
              auto val2 = q.value(1).toDouble();
              qDebug() << val1 << val2;
          }
          return 0;
      }
      
      • Edit the username and password in the attached example project to connect to your database, then run the project.

      Expected behavior:
      The value in Column1 (of type real) in the first row should return exactly 0.015625, as it is stored in the database.

      Actual behavior:
      The value in Column1 of the first row does not return the exact stored value; instead, it returns an approximation such as 0.01562 or 0.015619.

        1. QTBUG-138642.zip
          3.79 MB
          Mehdi Belila
        2. mssql_table.png
          5 kB
          Mehdi Belila
        No reviews matched the request. Check your Options in the drop-down menu of this sections header.

            chehrlic Christian Ehrlicher
            mahdi.belila Mehdi Belila
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated:
              Resolved:

                There are no open Gerrit changes