PDA

View Full Version : How to Connect to MySQL database



johnL
13th June 2014, 05:25
I am trying to connect to a MySQL database but QT doesn't load the MySQL Driver.
Both QSqlDatabase::drivers().contains("QMYSQL") and QSqlDatabase::isDriverAvailable("QMYSQL") return true but

QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL") fails with error message

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

The API documentation for addDatabase() implies that the addDatabase("DRIVER") call will open a connection and therefore should load the necessary driver.
Certainly QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE") succesfully opens a connection to SQLITE without any supplementary calls to "Load" a driver.

Some someone please explain how to get a connection to MySQL? or Load the MySQL driver?

Note: MySQL Server version: 5.5.37-0ubuntu0.14.04.1 is installed and running. Database can be accessed from Terminal so problem is not on MySQL side.

Regards
johnL

anda_skoa
13th June 2014, 10:33
Try setting QT_DEBUG_PLUGINS=1 in the environment from where you run the program.

Maybe that gives more hints on what is going wrong when Qt attempts to load the plugin.

Cheers,
_

folibis
13th June 2014, 12:50
Probably libmysqlclient.dll/so is not accesible by the plugin.

adutzu89
13th June 2014, 14:14
Check this thread (http://www.qtcentre.org/threads/55365-QSqlDatabase-QMYSQL-driver-not-loaded-but-available).

johnL
13th June 2014, 14:34
OK - I've set QT_DEBUG_PLUGINS=1 in /etc/environment
Now what - does this initiate logging? If so, what file in what location?

I've spent most of today investigating this and implementing various "solutions" from other trails in the forum - installing mysql-connector, adding symlinks to libmysqlconn, copying libmysqlclient_r.so etc - to no avail

A log printout may well identify the problem.

Regards

adutzu89
13th June 2014, 15:54
Have you installed libmysqlclient-dev?

anda_skoa
13th June 2014, 16:04
Now what - does this initiate logging? If so, what file in what location?

Not to a file, to the applications STDERR if I remember correctly.

Cheers,
_

stoyanps
13th June 2014, 18:51
One possible problem can be that the version of installed MySQL is 64-bit, while the version of Qt (and QMYSQL plugin) is 32-bit.

johnL
14th June 2014, 04:41
With QT_DEBUG_PLUGINS=1 Qt prints verbose comments to stderr.
after following line
if (!(QSqlDatabase::drivers().contains("QMYSQL")))
QMessageBox::critical(this, "Unable to load database", "This demo needs the MYSQL driver");
output to stderr includes

QFactoryLoader::QFactoryLoader() looking at "/home/john/Qt5.2.1/5.2.1/gcc_64/plugins/sqldrivers/libqsqlmysql.so"
Found metadata in lib /home/john/Qt5.2.1/5.2.1/gcc_64/plugins/sqldrivers/libqsqlmysql.so, metadata=
{
"IID": "org.qt-project.Qt.QSqlDriverFactoryInterface",
"MetaData": {
"Keys": [
"QMYSQL3",
"QMYSQL"
]
},
"className": "QMYSQLDriverPlugin",
"debug": false,
"version": 328193
}


Got keys from plugin meta data ("QMYSQL3", "QMYSQL")

and similar for the other included sqldrivers.

Then, after following line

QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL");

output to stderr includes

loaded library "gconf-2"
loaded library "/home/john/Qt5.2.1/5.2.1/gcc_64/plugins/sqldrivers/libqsqlmysql.so"
QLibraryPrivate::loadPlugin failed on "/home/john/Qt5.2.1/5.2.1/gcc_64/plugins/sqldrivers/libqsqlmysql.so" : "Cannot load library /home/john/Qt5.2.1/5.2.1/gcc_64/plugins/sqldrivers/libqsqlmysql.so: (/usr/lib/libmysqlclient_r.so.16: version `libmysqlclient_16' not found (required by /home/john/Qt5.2.1/5.2.1/gcc_64/plugins/sqldrivers/libqsqlmysql.so))"
QSqlDatabase: QMYSQL driver not loaded

Following comments on other threads on this forum, I have created a symlink /usr/lib/libmysqlclient_r.so.16 to the actual driver which is /usr/lib/x86_64-linux-gnu/libmysqlclient.so.18.0.0 so a file with the correct name is in the correct location.
I have then deleted the symlink and copied the actual driver to /usr/lib and changed its name to /usr/lib/libmysqlclient_r.so.16 so the file which qt reports that it cannot find IS there.

see directory list below -

john@john-Satellite-C660 /usr/lib $ ls libmysql*
libmysqlclient_r.so.16 libmysqlcppconn.so.7 libmysqlcppconn.so.7.1.1.3

Any suggestions. I'm guessing trying to locate and install version 16 of the mysqlclient is to difficult and likely to break mysql.

Regarding stoyanps comment on 64bit vs 32 bit, I'm assuming mysql is 64 bit as the client files are in the x86_64-linux-gnu directory and the QT about box reports that QT Creator 3.0.1 is Based on Qt5.2.1(GCC4.6.1, 64 bit) so it looks like everything is 64 bit.

Added after 1 17 minutes:

running ldd gives following output...

john@john-Satellite-C660 ~/Qt5.2.1/5.2.1/gcc_64/plugins/sqldrivers $ ldd libqsqlmysql.so
./libqsqlmysql.so: /usr/lib/libmysqlclient_r.so.16: version `libmysqlclient_16' not found (required by ./libqsqlmysql.so)
linux-vdso.so.1 => (0x00007fffca892000)
libmysqlclient_r.so.16 => /usr/lib/libmysqlclient_r.so.16 (0x00007f11057e9000)
libQt5Sql.so.5 => /home/john/Qt5.2.1/5.2.1/gcc_64/plugins/sqldrivers/./../../lib/libQt5Sql.so.5 (0x00007f11055a9000)
libQt5Core.so.5 => /home/john/Qt5.2.1/5.2.1/gcc_64/plugins/sqldrivers/./../../lib/libQt5Core.so.5 (0x00007f1104eda000)
libstdc++.so.6 => /usr/lib/x86_64-linux-gnu/libstdc++.so.6 (0x00007f1104bd6000)
libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f1104810000)
libz.so.1 => /lib/x86_64-linux-gnu/libz.so.1 (0x00007f11045f6000)
libdl.so.2 => /lib/x86_64-linux-gnu/libdl.so.2 (0x00007f11043f2000)
libpthread.so.0 => /lib/x86_64-linux-gnu/libpthread.so.0 (0x00007f11041d4000)
libm.so.6 => /lib/x86_64-linux-gnu/libm.so.6 (0x00007f1103ecd000)
libicui18n.so.51 => /home/john/Qt5.2.1/5.2.1/gcc_64/plugins/sqldrivers/./../../lib/libicui18n.so.51 (0x00007f1103ab4000)
libicuuc.so.51 => /home/john/Qt5.2.1/5.2.1/gcc_64/plugins/sqldrivers/./../../lib/libicuuc.so.51 (0x00007f110372d000)
libgthread-2.0.so.0 => /usr/lib/x86_64-linux-gnu/libgthread-2.0.so.0 (0x00007f110352b000)
librt.so.1 => /lib/x86_64-linux-gnu/librt.so.1 (0x00007f1103323000)
libglib-2.0.so.0 => /lib/x86_64-linux-gnu/libglib-2.0.so.0 (0x00007f110301b000)
libgcc_s.so.1 => /lib/x86_64-linux-gnu/libgcc_s.so.1 (0x00007f1102e04000)
/lib64/ld-linux-x86-64.so.2 (0x00007f1105f53000)
libicudata.so.51 => /home/john/Qt5.2.1/5.2.1/gcc_64/plugins/sqldrivers/./../../lib/libicudata.so.51 (0x00007f11016bb000)
libpcre.so.3 => /lib/x86_64-linux-gnu/libpcre.so.3 (0x00007f110147c000)
...
the first line says /usr/lib/libmysqlclient_r.so.16: version `libmysqlclient_16' ... not found but the 3rd line notes the file is found.
I guess QT is too smart and is checking the version of the file, not just its file name.

Added after 16 minutes:

http://qt-project.org/doc/qt-4.8/deployment-plugins.html#the-build-key confirms that QT is checking the content of the driver, not just its file name.

The same document also advises that "it is possible to override the run-time build key checks by configuring Qt with the QT_NO_PLUGIN_CHECK preprocessor macro defined."

Can anyone advise how to configure Qt with the QT_NO_PLUGIN_CHECK preprocessor macro defined?

Thanks
johnL

adutzu89
17th June 2014, 10:16
Have you done the following?

cd /usr/lib
sudo ln -s x86_64-linux-gnu/libmysqlclient_r.so libmysqlclient_r.so.16

It worked for me everytime on the exact same issue.

johnL
19th June 2014, 07:16
Yes I've created the link - see my previous post with results.
Because QT looks inside the file, it knows its not really version 16 and refuses to load it.
ldd also reports the dependency is not met even though it lists the link file with the correct name as found.

JohnL

adutzu89
19th June 2014, 13:07
Sorry didn't went through all post properly.

landondonovan
16th May 2015, 17:43
Hi Jhon,
I am going through the same problem and I am not able to solve it. Were you able to make it at the end?

Thanks

Marcos