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
{
Q_OBJECT
public:
MainWindow
(QWidget *parent
= nullptr
);
~MainWindow();
private:
Ui::MainWindow *ui;
};
#endif // 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
To copy to clipboard, switch view to plain text mode
mainwindow.cpp:
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QtSql/QSqlQuery>
MainWindow
::MainWindow(QWidget *parent
) ui(new Ui::MainWindow)
{
ui->setupUi(this);
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;
}
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;
}
#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;
}
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!
Bookmarks