mercredi 16 octobre 2019

QSqlDatabase: QMYSQL driver not loaded Ubuntu 19.04 and Qt5.12

I am working with Ubuntu 19.04 and Qt5.12 and am trying to write a small GUI that connects to a SQL Server database, but no matter what I tried I continuously get the following error:

QSqlDatabase: QMYSQL driver not loaded

QSqlDatabase: available drivers: QSQLITE QMYSQL QMYSQL3 QPSQL QPSQL7

I have been doing a lot of research about this topic and it seems from this source that it seems to be a library problem, to be specific libqsqlmysql.so but it is clearly installed. In addition the same post suggests that the following library should also be installed libmysqlclient_r. This last one I didn't have but after looking for a solution I came across this post that suggests to create a symlink in the following way:

sudo ln -s /usr/lib/x86_64-linux-gnu/libmysqlclient.so /usr/lib/x86_64-linux-gnu/libmysqlclient_r.so

But that also had no effect.

In the image below a sceenshot of the libraries I can confirm are installed: 1) libqsqlmysql.so 2)libmysqlclient.so and 3) I create a symlink to the libmysqlclient_r using the post I mentioned above.

mysql

In case is useful the following is my .pro file:

QT  += core gui
QT  += sql

greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

TARGET = GeneralDBModuleEntry
TEMPLATE = app
DEFINES += QT_DEPRECATED_WARNINGS
CONFIG += c++11
QMAKE_CXXFLAGS += -std=gnu++11

SOURCES += \
        main.cpp \
        mainwindow.cpp
HEADERS += \
        mainwindow.h
FORMS += \
        mainwindow.ui

INCLUDEPATH+=/usr/include/mysql
LIBS += -L/home/emanuele/Qt5.11.2/5.11.2/gcc_64/plugins/sqldrivers

And a small snippet of the code I am using:

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    model = new QSqlQueryModel;
    connectToSql();
}

void MainWindow::connectToSql()
{
    db = QSqlDatabase::addDatabase("QMYSQL", "myDbTest");
    db.setUserName("root");
    db.setDatabaseName("TestDB");
    db.setPassword("mypassword");
    db.setHostName("localhost");
    db.setPort(3306);
    if(db.open())
    {
        std::cout<<"Success Db is open"<<std::endl;
    }
    else
    {
        QMessageBox::critical(this, "Error", model->lastError().text());
    }
}

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

If there is something that I am missing please point in the right direction to solve this matter.

Aucun commentaire:

Enregistrer un commentaire