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

QSqlQuery (QMYSQL) cannot read valid datetime in multi rows result

    XMLWordPrintable

Details

    • Bug
    • Resolution: Incomplete
    • Not Evaluated
    • None
    • 6.9.0
    • SQL Support
    • None
    • Ubuntu 22.04, mariadb, gcc
    • Linux/X11

    Description

      When reading datetime from multi rows result that contains valid and null datetime, I get sometimes `QDateTime(Invalid)` for all the records. This never happen when reading a single row.

      Below is a code to reproduce this issue.

      int main(int argc, char **argv)
      {
        QCoreApplication app{argc, argv};
      
        // setting database
        QSqlDatabase db = QSqlDatabase::database();
        db = QSqlDatabase::addDatabase("QMYSQL");
        db.setUserName("root");
        db.setPassword("root");
        db.open();
      
        // db & table
        QSqlQuery query_init{db};
        query_init.exec("DROP DATABASE IF EXISTS testing;");
        query_init.exec("CREATE DATABASE testing;");
        query_init.exec(
            "CREATE TABLE testing.DateBug ("
            "uuid CHAR(36) NOT NULL COMMENT '(DC2Type:guid)',"
            "deletedAt DATETIME(3) DEFAULT NULL,"
            "PRIMARY KEY (uuid)) ENGINE = `InnoDB` DEFAULT CHARSET = `utf8`;");
      
        // inserting with valid date
        QSqlQuery query_insert_with_date;
        query_insert_with_date.prepare("INSERT INTO testing.DateBug VALUES (?,?)");
        const QString uuid = QUuid::createUuid().toString(QUuid::WithoutBraces);
        query_insert_with_date.addBindValue(uuid);
        const QDateTime date = QDateTime::currentDateTime();
        qDebug() << date;
        query_insert_with_date.addBindValue(date);
        query_insert_with_date.exec();
      
        // inserting without date
        QSqlQuery query_insert_no_date;
        query_insert_no_date.prepare("INSERT INTO testing.DateBug VALUES (?, NULL)");
        query_insert_no_date.addBindValue(QUuid::createUuid().toString(QUuid::WithoutBraces));
        query_insert_no_date.exec();
      
        // retrieving one => Date always ok
        QSqlQuery query_select_one;
        query_select_one.prepare("SELECT * FROM testing.DateBug WHERE uuid = ?");
        query_select_one.addBindValue(uuid);
        query_select_one.exec();
        query_select_one.next();
        const auto uuid1 = query_select_one.value(0).toString();
        const auto date1 = query_select_one.value(1).toDateTime();
        qDebug() << uuid1 << ", " << date1;
        assert(date == date1); // Always valid
      
        // retrieving all => Date sometimes ok, sometimes Invalid
        QSqlQuery query_select_all;
        query_select_all.prepare("SELECT * FROM testing.DateBug");
        query_select_all.exec();
        query_select_all.next();
        const auto uuid2 = query_select_all.value(0).toString();
        const auto date2 = query_select_all.value(1).toDateTime();
        qDebug() << uuid2 << ", " << date2;
        if (uuid2 == uuid) assert(date == date2); // Can fail
        query_select_all.next();
        const auto uuid3 = query_select_all.value(0).toString();
        const auto date3 = query_select_all.value(1).toDateTime();
        qDebug() << uuid3 << ", " << date3;
        if (uuid3 == uuid) assert(date == date3); // Can fail
      }
      

      Attachments

        1. mysql.zip
          2 kB
        2. test-results.png
          test-results.png
          59 kB
        3. db-data.png
          db-data.png
          16 kB
        4. sqldriver-config-output.txt
          2 kB
        5. mysql2.zip
          239 kB
        No reviews matched the request. Check your Options in the drop-down menu of this sections header.

        Activity

          People

            chehrlic Christian Ehrlicher
            felix.ledee LEDEE Félix
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved:

              Gerrit Reviews

                There are no open Gerrit changes