How to make Qt aware of the QMYSQL driver

I am trying to access the MySql database from a Qt application, but I am getting the following error:

QSqlDatabase: QMYSQL driver not loaded QSqlDatabase: available drivers: QSQLITE QSQLITE2 

I find this very strange reason. I have libqsqlmysql.so in my Qt folder. I even tried to compile the MySql driver as a static plugin and add it to my .pro file as:

 QTPLUGIN += qsqlmysql 

But it also raises the same runtime error (she must have found that the plugin does not cause errors when compiling the application)

What am I missing? I would like to avoid having to compile Qt from the source, as this should work seamlessly on the deployment machines.

BTW: Although I am developing and testing Linux, I will need to support Windows. Will I get the same problem on Windows? How can I compile and link the MySql driver on Linux and Windows?

Decision:

After following @Sergey's recommendations, I made a strace of the application redirecting the output to grep so that I could search for "mysql" and, to my surprise, the application was not looking for the plugin in QTDIR / plugins / sqldrivers, where I had libqsqlmysql.so, was looking at QTDIR / lib. After copying the plugin to the lib folder, the MySql connection worked.

+8
qt qtsql
source share
7 answers

Try opening the shared library with dlopen () and see if it loads, and if not, what dlerror () says. I have always encountered similar problems on Windows. LoadLibrary () / GetLastError () saved me many times (the last time it was due to the wrong version of some libiconv / libintl library). Running ldd in the plugin can also help.

If the dlopen () function works fine, try downloading the plugin from QPluginLoader. If it does not load, check the plugin build file. I usually do this in a dirty way by running lines in the plugin and then looking for lines like "buildkey" or "QT_PLUGIN_VERIFICATION_DATA". Just looking at and around the assembly key may give you an idea. For example, you can understand that you compiled your plugin in release mode when the application was compiled in debug mode. In this case, the build key will not match and the plugin will not load. Everything in the build key should match your configuration. Please note that the version and assembly key are checked differently: the assembly key must match exactly (or match black magic called QT_BUILD_KEY_COMPAT), but in the version only the main version must match exactly, the younger version must be the Qt version, the plugin was compiled with or later , and the patch level is ignored. Therefore, if your plugin was compiled with Qt 4.xy, then it will work with versions of Qt 4.z. *, where z> = x. It really makes sense.

If the build key looks fine (which is unlikely if you get to this point), you can look at the source code of QLibraryPrivate :: isPlugin () to see whatโ€™s wrong, but it doesnโ€™t look easy (although doing this in the debugger may help).

If QPluginLoader loads the plugin, check if it is in the correct directory and has the correct permissions. If you still haven't solved the problem until now, it's time to take a look at the source code of the SQL module that actually loads these plugins. But this is extremely unlikely. I came across this problem many times, and it was always either a library that was not loading, or a build key that was not appropriate.

Another way to go after QPluginLoader loads the plugin successfully is to use strace to find out if the program is at least trying to open the plugin file. Searching for something like "sqldrivers" or "plugins" in the strace output should also return the directory in which Qt looks for its plugins and, in particular, SQL drivers.

Update

Is it possible to compile the driver as a static plugin and not worry about anything? Try:

 d:\Qt4\src\plugins\sqldrivers\psql>qmake CONFIG+=static LIBS+=-Ld:/programs/Post greSQL/lib INCLUDEPATH+=d:/programs/PostgreSQL/include d:\Qt4\src\plugins\sqldrivers\psql>make 

It compiles fine, and now I got libqsqlpsql.a (release) and libqsqlpsqld.a (debug) in QTDIR / plugins / sqldrivers (this is the right place on Windows). I use the PostgreSQL driver here, but I do not think that for MySQL it will be completely different, which I just did not install. Well, let him compile some real program with him:

 d:\alqualos\pr\archserv>qmake QTPLUGIN+=qsqlpsql PREFIX=d:/alqualos LIBS+=-Ld:/g nu/lib INCLUDEPATH+=d:/gnu/include LIBS+=-Ld:/programs/PostgreSQL/lib LIBS+=-lpq 

Please note that I had to manually link to libpq, otherwise the linker will complain about undefined links. The funny thing is: qmake knows that qsqlpsql is in QTDIR / plugins / sqldrivers and accordingly sets the compiler and linker options. Therefore, it should still be in the right place to work, only you do not need to worry about the fact that your users are faced with the same problem as during compilation. An alternative would be to simply use LIBS+=-Lpath/to/plugin LIBS+=-lqsqlpsql instead of QTPLUGIN+=qsqlpsql , at least the docs say it should work, but I haven't tested it.

In order for the application to really use the plugin, I had to put the following into my main block (CPP file):

 #include <QtPlugin> Q_IMPORT_PLUGIN(qsqlpsql) 

It works! In addition, from what I was able to find out from the sources, the build key and version are checked only when the plugin is dynamically loaded (all relevant materials are in the private QLibrary class, not even QPluginLoader). Thus, the resulting executable file may (or may not, depending on binary compatibility) work even with different versions and strings of Qt, although using it with older versions may cause some errors that were fixed later.

It is also worth noting that the order of loading the SQL drivers is as follows: use the driver statically linked to Qt, if available, then find the driver registered manually with QSqlDatabase :: registerSqlDriver (), then find the driver statically imported into the application (the method described above), and finally try to download the generic plugin. Therefore, when you link statically, your users will not be able to use dynamically linked drivers that they already have, but they will be able to use drivers linked statically in Qt (for example, in Ubuntu).

+10
source share

I compiled QT first, and then realized that I also needed mysql. Therefore, I compiled the mysql plugin by running the following command in the QT-DIR \ src \ plugins \ sqldrivers \ mysql folder.

Mysql plugin compilation command

qmake "INCLUDEPATH + = $$ quote (C: \ Program Files \ MySQL \ MySQL Server 5.5 \ include)" "LIBS + = $$ quote (C: \ Program Files \ MySQL \ MySQL Server 5.5 \ lib \ libmysql.lib) "mysql.pro

Then plugins are created that are created in the QT-DIR \ plugins \ sqldrivers folder. However, when I tried to use it in my code. The following error failed.

Msg error

QSqlDatabase: QMYSQLDriver driver not loaded

Decision

After doing some searching and checking the Path variable, I realized that the Mysql lib server (C: \ Program Files \ MySQL \ MySQL Server 5.5 \ lib) was not in my Path variable. I expect the dll in this folder to be used by the plugin at runtime. After turning on the Mysql lib server in the Path variable, everything worked smoothly. Hope this information saves some hair on other scalp programmers since I uprooted a lot .: D

+3
source share

The last time I looked at this, you had to rebuild Qt from the source code and enable the appropriate MySQL source.

Building Qt from sources is not difficult, it will take some time. You probably already have the necessary tools.

A possible workaround might be to access background content via ODBC.

+2
source share

For your application to pick up the plugin at runtime, the shared library that implements the MySQL plugin must be placed in the correct directory. The best way to determine this directory is to check the output of QCoreApplication::libraryPaths . You can also force specific paths using the qt.conf file .

Please note that plugins must be placed in subdirectories in the plugin path, and the final part of the path name (i.e. the parent directory of shared libraries) cannot be changed. SQL drivers must be in a directory called sqldrivers , i.e. <pluginpath>/sqldrivers . For more information on plugin directories, see How to Create Qt Plugins .

+2
source share

I also experienced this problem. I have installed and experimented with many different Python tools and user interfaces. Then I uninstalled everything related to python. I did a fresh install of Python 3.2, PyQT 3.2, and Eric5. No more bugs with the QMySQL driver.

+1
source share

Well, I had this problem, and after a lot of time and various tools, I found that QT (on windows that could not be tested on Linux.) Loads "QSQLMYSQL .." on request, but before running the lib file ("QSQLMYSQL. . ") must be on one of the found paths (QApp.libraryPaths ()) inside the folder named" sqldrivers ".. otherwise QT will simply ignore the file, even if it is at some other point inside the found path. what I did was keep an eye on the dependency of the sample application, and when I removed the dll "QSQLMYSQL .." from "plugins \ sqldrivers", it failed, but when I typed a folder inside the application folder called "sqldrivers" and placed "QSQLMYSQL there ... ", it booted. I have mysql 5.5, qt 4.7.4.

hope someone can use it, and if anyone knows about it, I would like to know where to find it (http://doc.qt.nokia.com/stable/sql-driver.html, is the closest you can get information about structur folder) .: P

+1
source share

This can also happen if your QMYSQL plugin is associated with the "wrong" mysql_client.a or if it is not in LD_LIBRARY_PATH. I had this problem in OSX because mysql was installed through the ports and I fixed it with

 install_name_tool -change libmysqlclient.18.dylib /usr/local/mysql/lib/libmysqlclient_r.18.dylib libqsqlmysql.dylib 
0
source share

Source: https://habr.com/ru/post/650272/


All Articles