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

QMYSQL Insert a UTC time to datetime field

    XMLWordPrintable

Details

    • Bug
    • Resolution: Invalid
    • Not Evaluated
    • None
    • 6.8.0
    • SQL Support
    • None
    • Windows

    Description

      hi, i want to insert now time in my database by using qmysql, but i get a UTC Time not my system time, my mysql time zone set is with system.  I think it is wrong, please help me.

       

      D:\qtproject\qsqltimetest\build\Desktop_Qt_6_8_0_MinGW_64_bit-Release>qsqltimetest.exe
      QDateTime(2024-10-23 11:35:38.166 中国标准时间 Qt::LocalTime)
      Data successfully inserted into the database
      Retrieved record: server_time = QDateTime(2024-10-23 03:31:43.000 UTC Qt::UTC)
      Retrieved record: server_time = QDateTime(2024-10-23 03:35:38.000 UTC Qt::UTC)

       

      mysql time zone set:

       

      show variables like'%time_zone';
      SELECT now();

       

       

      test code:

       

      #include <QCoreApplication>
      #include <QSqlDatabase>
      #include <QSqlQuery>
      #include <QDateTime>
      #include <QDebug>
      #include <QSqlError>
      const QString TABLE_NAME = "time_test"; // Table name
      bool createDatabase() {
          // Open MySQL database connection
          QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL");
          db.setHostName("localhost");
          db.setDatabaseName("test");
          db.setUserName("root");
          db.setPassword("123456");
          if (!db.open())
      {         qDebug() << "Unable to open database:" << db.lastError().text();         return false;     }
          QSqlQuery query;
          // Create table with only the server_time field
          QString createTable = QString("CREATE TABLE IF NOT EXISTS %1 "
                                        "(id INT AUTO_INCREMENT PRIMARY KEY, "
                                        "server_time DATETIME)").arg(TABLE_NAME);
          if (!query.exec(createTable))
      {         qDebug() << "Failed to create table:" << query.lastError().text();         return false;     }
          return true;
      }
      bool insertData() {
          QSqlQuery query;
          // SQL insert statement using placeholders
          QString sql = "INSERT INTO " + TABLE_NAME + " (server_time) VALUES ";
          query.prepare(sql);
          // Get the current date and time
          QDateTime currentTime = QDateTime::currentDateTime();
          qDebug() << currentTime;
          // Bind the current time to the server_time field
          query.addBindValue(currentTime);
          // Execute the insert
          if (!query.exec())
      {         qDebug() << "Failed to insert data:" << query.lastError().text();         return false;     }
          qDebug() << "Data successfully inserted into the database";
          return true;
      }
      void checkInsertedData() {
          QSqlQuery query;
          // SQL query to select the inserted data
          QString selectSql = "SELECT server_time FROM " + TABLE_NAME;
          if (!query.exec(selectSql))
      {         qDebug() << "Failed to query data:" << query.lastError().text();         return;     }
          while (query.next())
      {         // Get the server_time field from the result         QDateTime serverTime = query.value(0).toDateTime();         qDebug() << "Retrieved record: server_time =" << serverTime;     }
      }
      int main(int argc, char *argv[])
      {
          QCoreApplication a(argc, argv);
          // Create the database and table
          if (!createDatabase())
      {         return -1;     }
      
          // Insert the data
          if (!insertData()) {         return -1;     }
          // Query and check the inserted data
          checkInsertedData();
          return a.exec();
      }
      

       

       

       

       

       

      Attachments

        Issue Links

          No reviews matched the request. Check your Options in the drop-down menu of this sections header.

          Activity

            People

              chehrlic Christian Ehrlicher
              xiazhanjian xiazhanjian
              Votes:
              0 Vote for this issue
              Watchers:
              3 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved:

                Gerrit Reviews

                  There are no open Gerrit changes