QT is not able to connect to MySQL.
-
Hello everyone, this is my first time in the forum. I'm trying work with a database in phpmyadmin but it seems I'm unable to do so. I'm a beginner and I know I ignore lots of things but I'm willing to learn.
This is the code in mainwindow.cpp:
#include "mainwindow.h"
#include "ui_mainwindow.h"#include<QTimer>
#include<QDateTime>
#include<QDate>
#include<QDebug>
#include<QTime>#include<QSqlDatabase>
#include<QtSql>
#include<QSqlQuery>
#include<QtNetwork>
#include<QSqlError>QSqlDatabase baseDatos;
void MainWindow::cronometro(){
//Obtener tiempo real
QDateTime fechaActual = QDateTime::currentDateTime();
tiempoEpoch=fechaActual.toTime_t();
//long int tiempoUnix =fechaActual.toTime_t();
//qDebug() << "Time: " <<tiempoUnix;
ui->label_8->setText(fechaActual.toString());
QTime tiempoActual = QTime::currentTime();
//qDebug() << "Tiempo actual: " << tiempoActual;
int horas = tiempoActual.hour();
int minutos = tiempoActual.minute();
int segundos = tiempoActual.second();
ui->lcdNumber->display(horas);
ui->lcdNumber_2->display(minutos);
ui->lcdNumber_3->display(segundos);
}MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
QTimer *temporizador = new QTimer(this);
connect(temporizador, SIGNAL(timeout()), this, SLOT(cronometro()));
temporizador->start(1000);
baseDatos = QSqlDatabase::addDatabase("QMYSQL");
baseDatos.setHostName("localhost");
baseDatos.setPort(3306);
baseDatos.setDatabaseName("tarea");
baseDatos.setUserName("admin51");
baseDatos.setPassword("hola1234");if(baseDatos.open()){ qDebug() << "Data base is open"; } else{ qDebug() << "Data base is NOT open"; }
}
MainWindow::~MainWindow()
{
delete ui;
}void MainWindow::on_pushButton_clicked()
{
codigo = ui->lineEdit->text();
evento = "Ent";if(baseDatos.open()){ //Insertar 4 columnas QString comandoMYSQL = "INSERT INTO ejemplo3_checador_tiempo(fecha,codigo,evento,imagen) VALUES(?,?,?,?)"; QSqlQuery comando; comando.prepare(comandoMYSQL); comando.addBindValue(codigo); comando.addBindValue(evento); QVariant valorlong = QVariant::fromValue(tiempoEpoch); //qDebug() << "Tiempo: " << valorLong; comando.addBindValue(valorlong); comando.addBindValue(imagen); if(comando.exec()){ qDebug() << "El dato se inserto correctamente"; } else{ qDebug() << "Error"; } }
}
void MainWindow::on_pushButton_2_clicked()
{
codigo = ui->lineEdit->text();
evento = "Sal";if(baseDatos.open()){ //Insertar 4 columnas QString comandoMYSQL = "INSERT INTO ejemplo3_checador_tiempo(fecha,codigo,evento,imagen) VALUES(?,?,?,?)"; QSqlQuery comando; comando.prepare(comandoMYSQL); comando.addBindValue(codigo); comando.addBindValue(evento); QVariant valorlong = QVariant::fromValue(tiempoEpoch); //qDebug() << "Tiempo: " << valorLong; comando.addBindValue(valorlong); comando.addBindValue(imagen); if(comando.exec()){ qDebug() << "El dato se inserto correctamente"; } else{ qDebug() << "Error"; } }
}
I will appreciate your support.
-
Hi and welcome to devnet,
Which version of Qt are you using ?
On which OS ?
Which version of MySQL ?
How did you install Qt ? -
Hi and welcome to devnet,
Which version of Qt are you using ?
On which OS ?
Which version of MySQL ?
How did you install Qt ?@SGaist
My QT version is 5.12.8, I'm using Ubuntu20.04. MySQL is Ver 8.0.23. QT installed by downloading the file form its official web. -
Hello everyone, this is my first time in the forum. I'm trying work with a database in phpmyadmin but it seems I'm unable to do so. I'm a beginner and I know I ignore lots of things but I'm willing to learn.
This is the code in mainwindow.cpp:
#include "mainwindow.h"
#include "ui_mainwindow.h"#include<QTimer>
#include<QDateTime>
#include<QDate>
#include<QDebug>
#include<QTime>#include<QSqlDatabase>
#include<QtSql>
#include<QSqlQuery>
#include<QtNetwork>
#include<QSqlError>QSqlDatabase baseDatos;
void MainWindow::cronometro(){
//Obtener tiempo real
QDateTime fechaActual = QDateTime::currentDateTime();
tiempoEpoch=fechaActual.toTime_t();
//long int tiempoUnix =fechaActual.toTime_t();
//qDebug() << "Time: " <<tiempoUnix;
ui->label_8->setText(fechaActual.toString());
QTime tiempoActual = QTime::currentTime();
//qDebug() << "Tiempo actual: " << tiempoActual;
int horas = tiempoActual.hour();
int minutos = tiempoActual.minute();
int segundos = tiempoActual.second();
ui->lcdNumber->display(horas);
ui->lcdNumber_2->display(minutos);
ui->lcdNumber_3->display(segundos);
}MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
QTimer *temporizador = new QTimer(this);
connect(temporizador, SIGNAL(timeout()), this, SLOT(cronometro()));
temporizador->start(1000);
baseDatos = QSqlDatabase::addDatabase("QMYSQL");
baseDatos.setHostName("localhost");
baseDatos.setPort(3306);
baseDatos.setDatabaseName("tarea");
baseDatos.setUserName("admin51");
baseDatos.setPassword("hola1234");if(baseDatos.open()){ qDebug() << "Data base is open"; } else{ qDebug() << "Data base is NOT open"; }
}
MainWindow::~MainWindow()
{
delete ui;
}void MainWindow::on_pushButton_clicked()
{
codigo = ui->lineEdit->text();
evento = "Ent";if(baseDatos.open()){ //Insertar 4 columnas QString comandoMYSQL = "INSERT INTO ejemplo3_checador_tiempo(fecha,codigo,evento,imagen) VALUES(?,?,?,?)"; QSqlQuery comando; comando.prepare(comandoMYSQL); comando.addBindValue(codigo); comando.addBindValue(evento); QVariant valorlong = QVariant::fromValue(tiempoEpoch); //qDebug() << "Tiempo: " << valorLong; comando.addBindValue(valorlong); comando.addBindValue(imagen); if(comando.exec()){ qDebug() << "El dato se inserto correctamente"; } else{ qDebug() << "Error"; } }
}
void MainWindow::on_pushButton_2_clicked()
{
codigo = ui->lineEdit->text();
evento = "Sal";if(baseDatos.open()){ //Insertar 4 columnas QString comandoMYSQL = "INSERT INTO ejemplo3_checador_tiempo(fecha,codigo,evento,imagen) VALUES(?,?,?,?)"; QSqlQuery comando; comando.prepare(comandoMYSQL); comando.addBindValue(codigo); comando.addBindValue(evento); QVariant valorlong = QVariant::fromValue(tiempoEpoch); //qDebug() << "Tiempo: " << valorLong; comando.addBindValue(valorlong); comando.addBindValue(imagen); if(comando.exec()){ qDebug() << "El dato se inserto correctamente"; } else{ qDebug() << "Error"; } }
}
I will appreciate your support.
@Jesus-Ocegueda said in QT is not able to connect to MySQL.:
but it seems I'm unable to do so
What exactly happens?
Set QT_DEBUG_PLUGINS environment variable before starting your app and post its output (https://doc.qt.io/qt-5/debug.html).
-
Hello everyone, this is my first time in the forum. I'm trying work with a database in phpmyadmin but it seems I'm unable to do so. I'm a beginner and I know I ignore lots of things but I'm willing to learn.
This is the code in mainwindow.cpp:
#include "mainwindow.h"
#include "ui_mainwindow.h"#include<QTimer>
#include<QDateTime>
#include<QDate>
#include<QDebug>
#include<QTime>#include<QSqlDatabase>
#include<QtSql>
#include<QSqlQuery>
#include<QtNetwork>
#include<QSqlError>QSqlDatabase baseDatos;
void MainWindow::cronometro(){
//Obtener tiempo real
QDateTime fechaActual = QDateTime::currentDateTime();
tiempoEpoch=fechaActual.toTime_t();
//long int tiempoUnix =fechaActual.toTime_t();
//qDebug() << "Time: " <<tiempoUnix;
ui->label_8->setText(fechaActual.toString());
QTime tiempoActual = QTime::currentTime();
//qDebug() << "Tiempo actual: " << tiempoActual;
int horas = tiempoActual.hour();
int minutos = tiempoActual.minute();
int segundos = tiempoActual.second();
ui->lcdNumber->display(horas);
ui->lcdNumber_2->display(minutos);
ui->lcdNumber_3->display(segundos);
}MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
QTimer *temporizador = new QTimer(this);
connect(temporizador, SIGNAL(timeout()), this, SLOT(cronometro()));
temporizador->start(1000);
baseDatos = QSqlDatabase::addDatabase("QMYSQL");
baseDatos.setHostName("localhost");
baseDatos.setPort(3306);
baseDatos.setDatabaseName("tarea");
baseDatos.setUserName("admin51");
baseDatos.setPassword("hola1234");if(baseDatos.open()){ qDebug() << "Data base is open"; } else{ qDebug() << "Data base is NOT open"; }
}
MainWindow::~MainWindow()
{
delete ui;
}void MainWindow::on_pushButton_clicked()
{
codigo = ui->lineEdit->text();
evento = "Ent";if(baseDatos.open()){ //Insertar 4 columnas QString comandoMYSQL = "INSERT INTO ejemplo3_checador_tiempo(fecha,codigo,evento,imagen) VALUES(?,?,?,?)"; QSqlQuery comando; comando.prepare(comandoMYSQL); comando.addBindValue(codigo); comando.addBindValue(evento); QVariant valorlong = QVariant::fromValue(tiempoEpoch); //qDebug() << "Tiempo: " << valorLong; comando.addBindValue(valorlong); comando.addBindValue(imagen); if(comando.exec()){ qDebug() << "El dato se inserto correctamente"; } else{ qDebug() << "Error"; } }
}
void MainWindow::on_pushButton_2_clicked()
{
codigo = ui->lineEdit->text();
evento = "Sal";if(baseDatos.open()){ //Insertar 4 columnas QString comandoMYSQL = "INSERT INTO ejemplo3_checador_tiempo(fecha,codigo,evento,imagen) VALUES(?,?,?,?)"; QSqlQuery comando; comando.prepare(comandoMYSQL); comando.addBindValue(codigo); comando.addBindValue(evento); QVariant valorlong = QVariant::fromValue(tiempoEpoch); //qDebug() << "Tiempo: " << valorLong; comando.addBindValue(valorlong); comando.addBindValue(imagen); if(comando.exec()){ qDebug() << "El dato se inserto correctamente"; } else{ qDebug() << "Error"; } }
}
I will appreciate your support.
@Jesus-Ocegueda said in QT is not able to connect to MySQL.:
if(baseDatos.open()){ qDebug() << "Data base is open"; } else{ qDebug() << "Data base is NOT open"; }
I would suggest you to change this to:
if(baseDatos.open()){ qDebug() << "Data base is open"; } else{ qDebug() << "Data base is NOT open:" << baseDatos.lastError().text(); }
How did you install Qt-Kit on your PC, with
MaintenanceTool
or with the packet manager of your Linux distribution?
AFAIK, starting with Qt 5.12.x, the MySQL plugin is only distributed withMaintenanceTool
as sources and must be build by yourself, because of Oracle/MySQL licencing changes.=> https://doc.qt.io/qt-5.12/sql-driver.html#how-to-build-the-qmysql-plugin-on-unix-and-macos
-
Since you are using a pretty recent Linux distribution, you might want to consider using the Qt version provided by it rather than building the plugin yourself (not that it is something hard though).
-
@Jesus-Ocegueda said in QT is not able to connect to MySQL.:
but it seems I'm unable to do so
What exactly happens?
Set QT_DEBUG_PLUGINS environment variable before starting your app and post its output (https://doc.qt.io/qt-5/debug.html).
This post is deleted! -
@Jesus-Ocegueda said in QT is not able to connect to MySQL.:
if(baseDatos.open()){ qDebug() << "Data base is open"; } else{ qDebug() << "Data base is NOT open"; }
I would suggest you to change this to:
if(baseDatos.open()){ qDebug() << "Data base is open"; } else{ qDebug() << "Data base is NOT open:" << baseDatos.lastError().text(); }
How did you install Qt-Kit on your PC, with
MaintenanceTool
or with the packet manager of your Linux distribution?
AFAIK, starting with Qt 5.12.x, the MySQL plugin is only distributed withMaintenanceTool
as sources and must be build by yourself, because of Oracle/MySQL licencing changes.=> https://doc.qt.io/qt-5.12/sql-driver.html#how-to-build-the-qmysql-plugin-on-unix-and-macos
This post is deleted! -
@Jesus-Ocegueda said in QT is not able to connect to MySQL.:
if(baseDatos.open()){ qDebug() << "Data base is open"; } else{ qDebug() << "Data base is NOT open"; }
I would suggest you to change this to:
if(baseDatos.open()){ qDebug() << "Data base is open"; } else{ qDebug() << "Data base is NOT open:" << baseDatos.lastError().text(); }
How did you install Qt-Kit on your PC, with
MaintenanceTool
or with the packet manager of your Linux distribution?
AFAIK, starting with Qt 5.12.x, the MySQL plugin is only distributed withMaintenanceTool
as sources and must be build by yourself, because of Oracle/MySQL licencing changes.=> https://doc.qt.io/qt-5.12/sql-driver.html#how-to-build-the-qmysql-plugin-on-unix-and-macos
@KroMignon said in QT is not able to connect to MySQL.:
@Jesus-Ocegueda said in QT is not able to connect to MySQL.:
if(baseDatos.open()){ qDebug() << "Data base is open"; } else{ qDebug() << "Data base is NOT open"; }
I would suggest you to change this to:
if(baseDatos.open()){ qDebug() << "Data base is open"; } else{ qDebug() << "Data base is NOT open:" << baseDatos.lastError().text(); }
How did you install Qt-Kit on your PC, with
MaintenanceTool
or with the packet manager of your Linux distribution?
AFAIK, starting with Qt 5.12.x, the MySQL plugin is only distributed withMaintenanceTool
as sources and must be build by yourself, because of Oracle/MySQL licencing changes.=> https://doc.qt.io/qt-5.12/sql-driver.html#how-to-build-the-qmysql-plugin-on-unix-and-macos
This is what I got: "Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2) QMYSQL: Unable to connect"
-
@Jesus-Ocegueda said in QT is not able to connect to MySQL.:
"Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2) QMYSQL: Unable to connect"
So you should make sure your MySQL server is running (on the given port).
-
@KroMignon said in QT is not able to connect to MySQL.:
@Jesus-Ocegueda said in QT is not able to connect to MySQL.:
if(baseDatos.open()){ qDebug() << "Data base is open"; } else{ qDebug() << "Data base is NOT open"; }
I would suggest you to change this to:
if(baseDatos.open()){ qDebug() << "Data base is open"; } else{ qDebug() << "Data base is NOT open:" << baseDatos.lastError().text(); }
How did you install Qt-Kit on your PC, with
MaintenanceTool
or with the packet manager of your Linux distribution?
AFAIK, starting with Qt 5.12.x, the MySQL plugin is only distributed withMaintenanceTool
as sources and must be build by yourself, because of Oracle/MySQL licencing changes.=> https://doc.qt.io/qt-5.12/sql-driver.html#how-to-build-the-qmysql-plugin-on-unix-and-macos
This is what I got: "Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2) QMYSQL: Unable to connect"
@Jesus-Ocegueda said in QT is not able to connect to MySQL.:
This is what I got: "Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2) QMYSQL: Unable to connect"
It looks like that the MySQL plugin is there.
Are you sure that you setup the right connection options?
You could test it with the console client:mysql --host=localhost --port=3306 --user=admin51 --password=hola1234 tarea
-
Since you are using a pretty recent Linux distribution, you might want to consider using the Qt version provided by it rather than building the plugin yourself (not that it is something hard though).
@SGaist said in QT is not able to connect to MySQL.:
Since you are using a pretty recent Linux distribution, you might want to consider using the Qt version provided by it rather than building the plugin yourself (not that it is something hard though).
You recommend uninstall QT and install it from the terminal?
LIke this: https://www.osradar.com/install-qtcreator-on-ubuntu-20-04-18-04/ -
@Jesus-Ocegueda said in QT is not able to connect to MySQL.:
This is what I got: "Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2) QMYSQL: Unable to connect"
It looks like that the MySQL plugin is there.
Are you sure that you setup the right connection options?
You could test it with the console client:mysql --host=localhost --port=3306 --user=admin51 --password=hola1234 tarea
@KroMignon said in QT is not able to connect to MySQL.:
mysql --host=localhost --port=3306 --user=admin51 --password=hola1234 tarea
This is the output I got after running the command line your gave me:
jesus@jesus-Inspiron-5559:~$ mysql --host=localhost --port=3306 --user=admin51 --password=hola1234 tarea
mysql: [Warning] Using a password on the command line interface can be insecure.
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -AWelcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 8.0.23-0ubuntu0.20.04.1 (Ubuntu)Copyright (c) 2000, 2021, Oracle and/or its affiliates.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql>
-
@Jesus-Ocegueda said in QT is not able to connect to MySQL.:
but it seems I'm unable to do so
What exactly happens?
Set QT_DEBUG_PLUGINS environment variable before starting your app and post its output (https://doc.qt.io/qt-5/debug.html).
@jsulm said in QT is not able to connect to MySQL.:
@Jesus-Ocegueda said in QT is not able to connect to MySQL.:
but it seems I'm unable to do so
What exactly happens?
Set QT_DEBUG_PLUGINS environment variable before starting your app and post its output (https://doc.qt.io/qt-5/debug.html).
How do I set that? Would you mind give me an example?
I'm guessing that I only need to place it right after my libraries on my mainwindow.cpp file. Is that correct?
LIke: QT_DEBUG_PLUGINS;I'm not sure because this is my first time with C++
-
@jsulm said in QT is not able to connect to MySQL.:
@Jesus-Ocegueda said in QT is not able to connect to MySQL.:
but it seems I'm unable to do so
What exactly happens?
Set QT_DEBUG_PLUGINS environment variable before starting your app and post its output (https://doc.qt.io/qt-5/debug.html).
How do I set that? Would you mind give me an example?
I'm guessing that I only need to place it right after my libraries on my mainwindow.cpp file. Is that correct?
LIke: QT_DEBUG_PLUGINS;I'm not sure because this is my first time with C++
@Jesus-Ocegueda
I'm guessing you run your program from within Qt Creator? In which case you need to set it as a runtime environment variable for when your program is executed.If you know how to run your executable from a terminal you need to type
export QT_DEBUG_PLUGINS=1
there. -
Since the mysql plugin is properly loaded there is no problem with Qt at all. Please read e.g. here: https://www.dailyrazor.com/blog/cant-connect-to-local-mysql-server-through-socket/ - the first idea would be to use 127.0.0.1 as suggested there so no local socket but tcp/ip is used.
-
Since the mysql plugin is properly loaded there is no problem with Qt at all. Please read e.g. here: https://www.dailyrazor.com/blog/cant-connect-to-local-mysql-server-through-socket/ - the first idea would be to use 127.0.0.1 as suggested there so no local socket but tcp/ip is used.
@Christian-Ehrlicher said in QT is not able to connect to MySQL.:
Since the mysql plugin is properly loaded there is no problem with Qt at all. Please read e.g. here: https://www.dailyrazor.com/blog/cant-connect-to-local-mysql-server-through-socket/ - the first idea would be to use 127.0.0.1 as suggested there so no local socket but tcp/ip is used.
I follow the article and doing the first instruction I got this:
jesus@jesus-Inspiron-5559:~$ mysqladmin -u root -p status
Enter password:
mysqladmin: connect to server at 'localhost' failed
error: 'Access denied for user 'root'@'localhost''After I try the option u provided I got this in QT:
Data base is NOT open "SSL connection error: unknown error number QMYSQL: Unable to connect"
I found intriguing that 'root'@__'localhost''__ it has another ' after host. Should I consider that too?
I also run sudo mysql and here are the users I have:
jesus@jesus-Inspiron-5559:~$ sudo mysql
[sudo] password for jesus:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 13
Server version: 8.0.23-0ubuntu0.20.04.1 (Ubuntu)Copyright (c) 2000, 2021, Oracle and/or its affiliates.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> SELECT user, authentication_string,plugin,host FROM msql.user;
ERROR 1049 (42000): Unknown database 'msql'
mysql> SELECT user, authentication_string,plugin,host FROM mysql.user;
+------------------+------------------------------------------------------------------------+-----------------------+-----------+
| user | authentication_string | plugin | host |
+------------------+------------------------------------------------------------------------+-----------------------+-----------+
| admin51 | $A$005$9T
T^&)o@TW^Q-h_&oT.MY9ehrMXFyNzv8k33JesTDZXuHjqUMWeSdvI14f22 | caching_sha2_password | % |
| debian-sys-maint | $A$005$(t[Bxr/[VJ_%KIDDL2c8KVbWsqR4cm7eSWTfZtbj1Y7HHnMl3IrkUhJr5 | caching_sha2_password | localhost |
| mysql.infoschema | $A$005$THISISACOMBINATIONOFINVALIDSALTANDPASSWORDTHATMUSTNEVERBRBEUSED | caching_sha2_password | localhost |
| mysql.session | $A$005$THISISACOMBINATIONOFINVALIDSALTANDPASSWORDTHATMUSTNEVERBRBEUSED | caching_sha2_password | localhost |
| mysql.sys | $A$005$THISISACOMBINATIONOFINVALIDSALTANDPASSWORDTHATMUSTNEVERBRBEUSED | caching_sha2_password | localhost |
| phpmyadmin | $A$005$:;]J;=H]l9 }E|QYK8HLtTg7fPJ7r3DC.syBTVait3ZiLHSfUaGEKiKkF0 | caching_sha2_password | localhost |
| root | | auth_socket | localhost |
+------------------+------------------------------------------------------------------------+-----------------------+-----------+
7 rows in set (0.01 sec)mysql>
-
@Jesus-Ocegueda
I'm guessing you run your program from within Qt Creator? In which case you need to set it as a runtime environment variable for when your program is executed.If you know how to run your executable from a terminal you need to type
export QT_DEBUG_PLUGINS=1
there.@JonB said in QT is not able to connect to MySQL.:
@Jesus-Ocegueda
I'm guessing you run your program from within Qt Creator? In which case you need to set it as a runtime environment variable for when your program is executed.If you know how to run your executable from a terminal you need to type
export QT_DEBUG_PLUGINS=1
there.How do I run it from terminal?
Should I just open a terminal on the folder my .pro file is? -
Just do the export, it will be valid for the terminal session until you unset it or close the terminal.
Then go to the folder where your application is and execute it.
-
Just do the export, it will be valid for the terminal session until you unset it or close the terminal.
Then go to the folder where your application is and execute it.
@SGaist said in QT is not able to connect to MySQL.:
Just do the export, it will be valid for the terminal session until you unset it or close the terminal.
Then go to the folder where your application is and execute it.
I bet this is easy but I don't get how to proceed when you say export, will you mind explain that or how to look it up that on google to search how to do that