PDA

View Full Version : Error in reading Datatime type from MySql (Static QT 5.15.7)



sr1s
14th October 2022, 11:11
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

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include "QtSql/QSql"
#include <QtSql/QSqlDatabase>
#include <QtSql/QSqlTableModel>
#include <QtSql/QSqlError>
#include <QtSql/QSqlQuery>
#include <QDebug>
#include <QtCore>
#include <QDate>
#include <QMessageBox>

QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE

class MainWindow : public QMainWindow
{
Q_OBJECT

public:
MainWindow(QWidget *parent = nullptr);
~MainWindow();

private:
Ui::MainWindow *ui;

};
#endif // MAINWINDOW_H
mainwindow.cpp:

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QtSql/QSqlQuery>

MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
QSqlDatabase eventDB;
eventDB = QSqlDatabase::addDatabase("QMYSQL");
eventDB.setHostName("localhost");
eventDB.setDatabaseName("event");
eventDB.setPort(3306);
eventDB.setUserName("user");
eventDB.setPassword("user_pwd123");
if (!eventDB.open()) {
QMessageBox::critical(this,"Error",eventDB.lastError().text());
return;
}

QSqlQuery * qryEvent= new QSqlQuery();
qryEvent->prepare("SELECT event_name,event_date FROM event WHERE id_event=0");
qryEvent->exec();
qryEvent->first();
QString event=qryEvent->value(0).toString();


QDateTime time_event=qryEvent->value(1).toDateTime();

QString Date=time_event.toString("dd-MM-yyyy");
QString Hour=time_event.toString("hh:mm");

ui->Event->setText(event);
ui->Date->setText(Date);
ui->Hour->setText(Hour);
}

MainWindow::~MainWindow()
{
delete ui;
}


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:
13762
while in Static Qt 5.15.7 (adding a qDebug for checking "Date" value:
13763
Any suggestion?
Thanks a lot!

d_stranz
15th October 2022, 15:43
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?