mysql connects but Qt6 plugin does not

  c++, mysql, qt6

I have a Windows 10 64-bit machine that runs MySQL Community Server 8.0.26.
On another machine I have Ubuntu Desktop 20.04 64-bit and I’m trying to connect to the database using Qt6.2.0.

I built the plugin (almost) following these instructions.
Then I ran this code:

#include <QCoreApplication>
#include <QSqlDatabase>
#include <QSqlError>
#include <QDebug>

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);

    QSqlDatabase db = QSqlDatabase::addDatabase("QODBC");
    QString connectString = QStringLiteral(
        "DRIVER=/home/mark/Qt/6.2.0/gcc_64/plugins/sqldrivers/libqsqlodbc.so;"
        "SERVERNODE=192.168.1.28:3306;"
        "UID=admin;"
        "PWD=<password>;"
        "SCROLLABLERESULT=true");
    db.setDatabaseName(connectString);

    qDebug() << db.open();
    qDebug() << db.lastError().text();

    return a.exec();
}

Here the output:

false

"[unixODBC][Driver Manager]Driver’s SQLAllocHandle on SQL_HANDLE_HENV failed QODBC: Unable to connect"

But if I type on my terminal:

mysql -h 192.168.1.28 -u admin -p

I can successfully connect.
Is there an error in my code?

If it could be useful I report here the odbcinst -j output:

$ odbcinst -j
unixODBC 2.3.6
DRIVERS............: /etc/odbcinst.ini
SYSTEM DATA SOURCES: /etc/odbc.ini
FILE DATA SOURCES..: /etc/ODBCDataSources
USER DATA SOURCES..: /home/mark/.odbc.ini
SQLULEN Size.......: 8
SQLLEN Size........: 8
SQLSETPOSIROW Size.: 8

Source: Windows Questions C++

LEAVE A COMMENT