How to load a sql driver correctly
[toc align_right="yes" depth="2"]
Introduction
This article describes the steps that must be taken for your MySQL driver to be loaded by an application on Windows when using the Visual Studio compiler.
Verify that the MySQL driver is built
First of all, you need to make sure you have the client MySQL libraries installed on your machine and that your INCLUDEPATH and LIB environment variables are set up correctly so that the headers and lib files can be found.
Then you need to check that the qsqlmysql4 files exist under yourQtVersion/plugins/sqldrivers. If the qsqlmysql4 files don't exist under, yourQtVersion/plugins/sqldrivers, then you need to build them by going to yourQtVersion/SRC/plugins/sqldrivers/mysql and typing
qmake
nmake
Also, everything needs to have the same configuration. Make sure you have built your application, Qt and your plugin with the same configuration (i.e. debug vs release)
Ensure that libmysql.dll is found
libmysql.dll needs to be located somewhere where it can be found by the application. To make this happen, you can list it in your global PATH in Control Panel/System/Advanced System Settings/Environment variables or put libmysql.dll in the same folder as the Qt dlls in yourQtDirectory/bin.
QT_DEBUG_PLUGINS
If the plugin fails to load, then in order to get information from Qt about the loading of the plugin, you can use the "QT_DEBUG_PLUGINS":http://doc.qt.nokia.com/latest/deployment-plugins.html#debugging-plugins environment variable. This might help you to find the cause of the problem. Set this variable to a non-zero value in the environment from which your application is launched.
Dependency Walker
If you are still having problems, then run "Dependency Walker":http://www.dependencywalker.com/ on the plugin to check if the dependencies are correct. You need to run Dependency Walker on the plugin from the application directory, this way it will get the same environment as it would when starting up.
Caching
If the driver is still not loaded, make sure you clear the cache. In order to perform better when loading plugins Qt uses the registry to cache the result of the first time it attempts to load the plugin. This ensures that time is saved by not having to do all the necessary checks when running a Qt application with that particular version of Qt in a future run. However, this means that if a plugin fails on that first attempt that Qt will assume it can never load it.
If this is the reason why the plugin is not loading, then you can simply remove the relevant entry from the registry, just delete the folders:
HKEY_CURRENT_USER\Software\Trolltech\OrganizationDefaults\Qt Plugin Cache 4.x.debug
HKEY_CURRENT_USER\Software\Trolltech\OrganizationDefaults\Qt Plugin Cache 4.x.false
See the documentation for further information on the "caching":http://doc.qt.nokia.com/latest/deployment-plugins.html#the-plugin-cache.
Deployment
When deploying the application, the MySQL driver needs to be located in a folder named sqldrivers within your application folder, otherwise it will not load. You can find further information on this "here":http://doc.qt.nokia.com/latest/deployment.html.