简体   繁体   中英

Qt: QSqlDatabase: QMYSQL driver not loaded

I've just done developing a QT CRUD Application. I am using SQLite to store the information locally in my disk where the application is running. This way my application works fine.

But the app lack the the a central database server. Therefor, I started by installing MySql-server and MySql-workbench. I imported my DB in the workbench and made sure the server is running.

Now on Qt, I wrote the following lines to conenct to my server on the "localhost " using port "3306" as follow:

db = QSqlDatabase::addDatabase("QMYSQL");
db.setHostName("localhost");
db.setPort(3306);
db.setDatabaseName(databaseName);
db.setUserName(userName);
db.setPassword(userPass);

qDebug() << appDataPath;

if (!db.open()){
    return db.lastError().text();
}

However, I get the following error on QT "Application Outout ":

QSqlDatabase: QMYSQL driver not loaded QSqlDatabase: available drivers: QSQLITE QMYSQL QMYSQL3 QODBC QODBC3 QPSQL QPSQL7

I tried to make sure that libmysql.dll is located in "C:\\Program Files\\MySQL\\MySQL Server 5.7\\lib" ..

I copied it also to "C:\\Users\\Abubakr\\Documents\\GitHub\\DEBUG\\build-Muwassa-Desktop_Qt_5_4_2_MinGW_32bit2-debug"

but it is not working at all.

Please help !!

You need to build the QMYSQL driver by yourself first.

When you download Qt you have also the source that are provided with it.

In my case, it is in : C:\\Qt\\5.9\\Src\\qtbase\\src\\plugins\\sqldrivers\\mysql Here you have a *.pro (that is a kind of makefile generator). You just have to open it with QtCreator, Compile it and you will get a dll. This dll is the MySQL driver and must be in the same dirent of your application.

To do that, if you have a problem with the compilation (like mysql.h not found), you must "install" mysql library also.

In Linux you can have the same issue as well.

It's late to answer, but this may be useful for future readers.

When QMYSQL is available but not loaded, you should find libqsqlmysql.a and libqsqlmysqld.a files corresponding to qsqlmysql.dll and qsqlmysqld.dll files and copy them (.a files) into the \\mingw73_xx\\lib folder. (replace xx by 64 or 32)

Note that for project kit 64 bit use MySql 64 bit, and for project kit 32 bit use MySql 32 bit.

Details are available at QT : QSqlDatabase: QMYSQL driver not loaded [WINDOWS]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM