QSqlDatabase: QMYSQL driver not loaded

Today, I ran a program that connects to a database, then a window popped up:

Meanwhile, the following error appeared on the console:

QSqlDatabase: QMYSQL driver not loaded

The error message is funny: the first line says QMYSQL is not loaded while the second line says it’s available. Search the error on google, you’ll be definitely led to this webpage and this page.  The content on those page is so long and so complicated. They teach you how to configure and build the sql drivers. Even I have some experience in playing with the qt configure script and qmake, I still cannot understand well  what they are talking about. I got the illusion that due to the license issue, qt won’t build the driver for mysql database and you need to build it yourself. The fact is: the drivers are already built and put in c:\Qt5.12.1\5.12.1\mingw73_64\plugins\sqldrivers: qsqlmysql.dll, qsqlmysqld.dll. Although the plugin path is not included in the PATH environment variable,  your program can find it without problem(amazing!). But the QMYSQL driver that was not loaded is not referring to this dll! It is another dll provided by MySQL, not by Qt. And the name of the dll is libmysql.dll, not mysql.dll as said in one of the mentioned posts. So the solution is simple: copy a libmysql.dll to the directory where your program resides and that will be ok. Your program will load the bridging Qt dll qsqlmysql.dll, which will load libmysql.dll, which communicates with the mysql server to query the database.

If you like my content, please consider buying me a coffee. Buy me a coffeeBuy me a coffee Thank you for your support!
Posted in

Leave a Reply