PDA

View Full Version : Qt database connection: QMYSQL driver not loaded



nomanbinhussein
18th December 2015, 19:18
I'm using Qt 5.5 with MSVC plugin. I want to simply connect to a database. The code is -

QString hostName = "127.0.0.1";
QString databaseName = "imagedatabase";
QString userName = "demouser";
QString userPass = "demopass";

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

bool ok = db.open();

But the following error occurs -


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

Then following some suggestion in the web - I copied the qsqlmysql.dll, qsqlmysqld.dll,libmysql.dll, libmysqld.dll files to the project folder. -that didn't work. According to another thread here - I have to put mysqlclient.lib where my application executable is. But I use a wampserver mysql where I don't have a mysqlclient.lib file.
How can I complete this simple task?

d_stranz
19th December 2015, 04:53
Sounds like you haven't read the documentation on How to Create Plugins (http://doc.qt.io/qt-5/plugins-howto.html) and Deploying Plugins (http://doc.qt.io/qt-5/deployment-plugins.html). And you probably haven't installed the MySQL Connector for C++ (http://dev.mysql.com/downloads/connector/cpp/) either.

gustavorg
14th April 2016, 23:54
Well, just for people arriving here in despair, you can ignore this rant, I'm sure you already read the documentation and yes, you don't need MySQL Connector for C++.
You need this in the executable folder: libmysql.dll and qsqlmysql.dll

d_stranz
15th April 2016, 16:33
Thank you for your polite and instructive comments. We look forward to your next 2,247 helpful posts over the next 8 years

ChrisW67
15th April 2016, 22:48
The Qt Mysql plugin should be placed in a directory "sqldrivers" that lives next to the main project executable. This is the app-specific default location searched for SQL plugins. The Mysql runtime library, which is not part of Qt, can be placed anywhere the standard Windows DLL search would look when it loads the plugin dll (on a dev machine this is often on the %PATH). For deployment it is usually most convenient to put it in the "sqldrivers" directory too.
http://doc.qt.io/qt-5/windows-deployment.html#qt-plugins

If you do not have a full Mysql install to work from the Mysql Connector for C should contain the run time library:
http://dev.mysql.com/downloads/connector/c/