Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

QSqlDatabase: QMYSQL driver not loaded: What am I doing wrong?



  • Hello Folks,

    Kindly help me please.

    I am learning to connect to MySQL on XAMPP with QT Creator. I followed a video tutorial and did exactly as they did to make it work, but it did not work for me. Instead, I got the error message below. I have made effort to fix it myself by searching the web, but with no luck yet. Here is the error message.

    "QSqlDatabase: QMYSQL driver not loaded
    QSqlDatabase: available drivers: QSQLITE QODBC QODBC3 QPSQL QPSQL7
    Unable to connect to the database check your connection details."

    I downloaded the MySQL connector - c, put it on a folder on my desktop and added it as an external library before running it.

    The files below are my two files for the code.

    1. ----------------The main.cpp-----------------------

    #include <QCoreApplication>
    #include <QtSql/QtSql>
    #include <QtSql/QSqlDatabase>
    #include <QtSql/QSqlQuery>

    //Add the i/o stream
    #include <iostream>
    using namespace std;

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

    //Create a database connection
    

    QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL");
    db.setHostName("localhost");
    db.setUserName("root");
    db.setPassword("");
    db.setDatabaseName("qtdatabase");

    //Test the connection
    if(db.open()) {
    cout << "Database connected successfully" << endl;

    } else {

    cout << "Unable to connect to the database check your connection details." << endl;
    

    }

    return a.exec();
    

    }

    1. -----------The mysqlconnect.pro file--------------

    QT += core
    QT += sql
    QT -= gui

    CONFIG += c++11 console

    TARGET = mysqlconnect

    CONFIG -= app_bundle

    SOURCES +=
    main.cpp

    Default rules for deployment.

    qnx: target.path = /tmp/$${TARGET}/bin
    else: unix:!android: target.path = /opt/$${TARGET}/bin
    !isEmpty(target.path): INSTALLS += target

    win32: LIBS += -L$$PWD/../../../../Users/user/Desktop/lib/ -llibmysql

    INCLUDEPATH += $$PWD/../../../../Users/user/Desktop
    DEPENDPATH += $$PWD/../../../../Users/user/Desktop

    I will really appreciate your help please.


  • Lifetime Qt Champion

    Hi,

    This question has been asked many times already. Did you take the time to search and go through the numerous forum threads about this topic ?



  • @SGaist I have read many posts about this issue and some get one more confused than one was before reading them. Even doing some of their suggestions about downloading some .dll files and copying them to a folder do not help. Do you have a particular post or advice in mind that helps?


  • Lifetime Qt Champion

    So did you actually run Dependency Walker to see of the Qt mysql plugins finds all needed dependencies?



  • @Christian-Ehrlicher No, I did not run it. How do I do that? All the posts that I have been reading have QMYSQL as one of the available drivers, but mine doesn't have it. Will running Dependency Walker check that? What I have as available drivers is the line below please.
    'QSqlDatabase: available drivers: QSQLITE QODBC QODBC3 QPSQL QPSQL7'.


  • Lifetime Qt Champion

    I downloaded the MySQL connector - c, put it on a folder on my desktop and added it as an external library before running it.

    You did you actually follow https://doc.qt.io/qt-5/sql-driver.html#qmysql to build the plugin?



  • @Christian-Ehrlicher I am now asking myself why is something so simple (as per your response) so dificult for me to do. I have visited the link you sent. What is the right way to implement the line below with my code please?
    "To use the embedded MySQL server, simply link the Qt plugin to libmysqld instead of libmysqlclient. This can be done by adding MYSQL_LIBS=-lmysqld to the configure command line."


  • Lifetime Qt Champion

    @Menre said in QSqlDatabase: QMYSQL driver not loaded: What am I doing wrong?:

    implement the line below with my code please?

    so what did you do until now? How did you configure the plugin? Please show use the output (as text, no pictures please)


Log in to reply