Results 1 to 2 of 2

Thread: Error in reading Datatime type from MySql (Static QT 5.15.7)

  1. #1
    Join Date
    Mar 2016
    Posts
    22
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Error in reading Datatime type from MySql (Static QT 5.15.7)

    Hello to everyone,
    I have a problem reading from a MySql database some values (DataTime type). Using the dynamic installation (Qt 5.15.7 "out of the box") I have no problem, but trying to compile it with the static version, Qt can't resolve its value.
    I created a very simple database, just to show this behaviour:
    id_event , event_name, event_date (format: DATETIME)
    '0', 'Beethoven Concert' '2022-08-15 21:15:00'
    '1' 'Vivaldi Concert' '2022-08-22 21:30:00'
    '2 'Bach Concert' '2022-08-08 21:00:00'
    Now I run this simple code:
    mainwindow.h
    Qt Code:
    1. #ifndef MAINWINDOW_H
    2. #define MAINWINDOW_H
    3.  
    4. #include <QMainWindow>
    5. #include "QtSql/QSql"
    6. #include <QtSql/QSqlDatabase>
    7. #include <QtSql/QSqlTableModel>
    8. #include <QtSql/QSqlError>
    9. #include <QtSql/QSqlQuery>
    10. #include <QDebug>
    11. #include <QtCore>
    12. #include <QDate>
    13. #include <QMessageBox>
    14.  
    15. QT_BEGIN_NAMESPACE
    16. namespace Ui { class MainWindow; }
    17. QT_END_NAMESPACE
    18.  
    19. class MainWindow : public QMainWindow
    20. {
    21. Q_OBJECT
    22.  
    23. public:
    24. MainWindow(QWidget *parent = nullptr);
    25. ~MainWindow();
    26.  
    27. private:
    28. Ui::MainWindow *ui;
    29.  
    30. };
    31. #endif // MAINWINDOW_H
    To copy to clipboard, switch view to plain text mode 
    mainwindow.cpp:
    Qt Code:
    1. #include "mainwindow.h"
    2. #include "ui_mainwindow.h"
    3. #include <QtSql/QSqlQuery>
    4.  
    5. MainWindow::MainWindow(QWidget *parent)
    6. : QMainWindow(parent),
    7. ui(new Ui::MainWindow)
    8. {
    9. ui->setupUi(this);
    10. QSqlDatabase eventDB;
    11. eventDB = QSqlDatabase::addDatabase("QMYSQL");
    12. eventDB.setHostName("localhost");
    13. eventDB.setDatabaseName("event");
    14. eventDB.setPort(3306);
    15. eventDB.setUserName("user");
    16. eventDB.setPassword("user_pwd123");
    17. if (!eventDB.open()) {
    18. QMessageBox::critical(this,"Error",eventDB.lastError().text());
    19. return;
    20. }
    21.  
    22. QSqlQuery * qryEvent= new QSqlQuery();
    23. qryEvent->prepare("SELECT event_name,event_date FROM event WHERE id_event=0");
    24. qryEvent->exec();
    25. qryEvent->first();
    26. QString event=qryEvent->value(0).toString();
    27.  
    28.  
    29. QDateTime time_event=qryEvent->value(1).toDateTime();
    30.  
    31. QString Date=time_event.toString("dd-MM-yyyy");
    32. QString Hour=time_event.toString("hh:mm");
    33.  
    34. ui->Event->setText(event);
    35. ui->Date->setText(Date);
    36. ui->Hour->setText(Hour);
    37. }
    38.  
    39. MainWindow::~MainWindow()
    40. {
    41. delete ui;
    42. }
    To copy to clipboard, switch view to plain text mode 

    I have a simple form to show the result of "event","Date" and "Hour" variables and this is the result.
    With Dynamic Qt 5.15.7:
    dynamic_QT5.jpg
    while in Static Qt 5.15.7 (adding a qDebug for checking "Date" value:
    Static_QT.png
    Any suggestion?
    Thanks a lot!

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Error in reading Datatime type from MySql (Static QT 5.15.7)

    QSqlQuery::prepare(), QSqlQuery::exec(), and QSqlQuery::first() all return Boolean values indicating success or failure. You don't check them, so how do you know if the query succeeded at all?
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

Similar Threads

  1. Replies: 4
    Last Post: 8th March 2016, 15:27
  2. Replies: 10
    Last Post: 15th January 2016, 05:29
  3. Replies: 1
    Last Post: 29th June 2013, 17:23
  4. MySql Reading Html Text Problem
    By un9tsandeep in forum Qt Programming
    Replies: 3
    Last Post: 16th May 2012, 00:48
  5. How to know what configuration type shared|static
    By cafu1007 in forum Qt Programming
    Replies: 2
    Last Post: 31st March 2011, 17:46

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.