QMYSQL driver available but not loaded - mysql

How do I load qmysql driver in Qt? I have the following code that produces these results:
("QSQLITE", "QMYSQL", "QMYSQL3")
QSqlDatabase: QMYSQL driver not loaded
QSqlDatabase: available drivers: QSQLITE QMYSQL QMYSQL3
Any suggestions on how to load it?

We should check our driver first
$ cd /opt/Qt5.2.1/5.2.1/gcc_64/plugins/sqldrivers
then we can find some files
Use the command below to check library
$ ldd libqsqlmysql.so
if you find the problem libmysqlclient_r.so.16 => not found it may be the library-dependency problem.
After I did a little research on the Internet, there is a way would be easy.
$ cd /usr/lib/x86_64-linux-gnu
if you find libmysqlclient_r.so.18,
$ cp libmysqlclient_r.so.18 libmysqlclient_r.so.16

ok it worked just by copying the sqldrivers folder to my debug folder and it worked!

You could try diagnosing the issue with strace - it seems like the QMYSQL driver might need some run-time library dependencies to work.

On Windows (see as directory structure):
the_qt_app.exe
libmysql.dll
sqldrivers/qsqlmysql4.dll

You can use QPluginLoader to get some better error message.
When I had the same problem with the MySQL driver the message was something like "The version was compiled with other options than this Qt version".
It seemed like the Qt sources, that shipped with the Qt SDK at that time, were not compatible with its binaries.
After downloading the Qt sources and compiling my own version of Qt and the MySQL driver, the problem was gone.
EDIT: Some sample code.
QPluginLoader loader;
loader.setFileName("/Users/niklaswulf/QtSDK/Qt/4.8.4/plugins/sqldrivers/libqsqlite_debug.dylib");
qDebug() << loader.load();
qDebug() << loader.errorString();
loader.setFileName("/Users/niklaswulf/QtSDK/Qt/5.0.1/5.0.1/clang_64/plugins/sqldrivers/libqsqlite_debug.dylib");
qDebug() << loader.load();
qDebug() << loader.errorString();
When compiling against 5.0.1 this is the output:
false
"The file '/Users/niklaswulf/QtSDK/Qt/4.8.4/plugins/sqldrivers/libqsqlite_debug.dylib' is not a valid Qt plugin."
true
"Unknown error"
I also found the old message:
The plugin '/path/to/some/libqsqlmysql.dylib' uses incompatible Qt library. Expected build key "macosx macx-cocoa g++-4 full-config", got "macosx macx-cocoa g++-4 no-pkg-config"

QSqlDatabase: QMYSQL driver not loaded
QSqlDatabase: available drivers: QSQLITE QMYSQL QMYSQL3
The same problem I faced in fedora20 (64-bit) with Qt-5.2.0, and then follow steps:
$ cd /opt/Qt5.2.0/5.2.0/gcc_64/plugins/sqldrivers
$ ls
libqsqlite.so libqsqlmysql.so
Use the command below to check library dependency:
$ ldd libqsqlmysql.so
I find the problem:
libmysqlclient_r.so.16 => not found
It may be the library-dependency problem. so solve this problem:
Linking of the library file:
$ ln -s libmysqlclient_r.so.16.0.0 libmysqlclient_r.so
and again:
$ ln -s libmysqlclient_r.so.16.0.0 libmysqlclient_r.so.16
Now its work for me.
All the best

Got the same problem and some Google research and intuition finally solved it.
Using Qt5.9.1 and Ubuntu 17.10
First, check if the error of libmysqlclient.so.18 => not found is present
:~/Qt5.9.1/5.9.1/gcc_64/plugins/sqldrivers$ ldd libqsqlmysql.so
Second, search where is libmysqlclient
:/$ locate libmysqlclient
Third, go to the folder where libmysqlclient is present and there make the link
:/usr/lib/x86_64-linux-gnu$ sudo ln -s libmysqlclient.so.20 libmysqlclient.so.18
and check the link made before with
ls -alh | grep libmysql
At that moment, none of those solved for me, and i decided to look further in synaptic packages, and realize that libqt5sql5-mysql version 5.9.1 was not installed, so installing it solved the problem but i still have a message when doing ldd
./libqsqlmysql.so: /usr/lib/x86_64-linux-gnu/libmysqlclient.so.18: version `libmysqlclient_18' not found (required by ./libqsqlmysql.so)
after that, found some links that guide me to a real solution, here are them, if you wanna know whats happening
i downloaded the library stated in the third link and worked like a charm. hope it helps!
https://www.unix.com/unix-for-advanced-and-expert-users/107611-difference-between-libsqlclient-so-libsqlclient_r-so.html
http://www.tango-controls.org/community/forum/c/general/installation/ubuntu-1604-problem-installing-from-source-code-libmysqlclient-replaces-libmysqlclient_r/
https://superuser.com/questions/1101426/installing-libmysqlclient18-on-ubuntu-16-04?utm_medium=organic&utm_source=google_rich_qa&utm_campaign=google_rich_qa&newreg=9c558283c488461aaf597ef1132e4ca0

Here a couple of very nice links on this issue:
making the plugins manually on Unix-based systems (from Qt documentation):
http://qt-project.org/doc/qt-5/sql-driver.html
a very well-written link specifically on this issue (Do not forget to install the Qt from source, this can be done by either checking the corresponding (hidden) box in the first step while installing from the .run executable or by downloading the 'qt-everywhere-opensource-src' version):
http://adamcavendish.is-programmer.com/posts/40431.html
Enjoy,
Peyman

My answer:
QSqlDatabase db(QSqlDatabase::addDatabase("QMYSQL"));
db.setDatabaseName("dbname");
db.setHostName("localhost");
db.setUserName("usernm");
db.setPassword("password");
if (db.open())
{
qDebug() << "SUCCESS!";
db.close();
}

Related

Qt Library 'mysql' is not defined

I have a problem with Qt connecting with MySql, when i run this code
QSqlDatabase DBObject = QSqlDatabase::addDatabase("QMYSQL");
DBObject.setHostName("localhost");
DBObject.setDatabaseName("SingleDB");
DBObject.setUserName("root");
DBObject.setPassword("abc123");
bool ok = DBObject.open();
and I got this... QSqlDatabase: QMYSQL driver not loaded
I Have already done this also:
sudo apt-get install libmysqlclient
and
/home/wrm/Qt/5.12.3/gcc_64/bin/qmake "INCLUDEPATH+=/usr/local/include" "LIBS+=-L/usr/local/lib -lmysqlclient_r" mysql.pro
and here i have this error: Project ERROR: Library 'mysql' is not defined
Any idea?
Perhaps you need to install mysql-devel.
According to the Qt Docs QMYSQL for MySQL 4 and higher:
How to Build the QMYSQL Plugin on Unix and macOS
You need the MySQL header files, as well as the shared library libmysqlclient.so. Depending on your Linux distribution, you may need to install a package which is usually called "mysql-devel".
Google doesn't have a readily available answer, so answering this old question:
Aside from needing development files as pointed above (like apt install libmysqlclient-dev), you need to generate a config:
# Just for making my snippet work. Feel free to hardcode paths.
export QTDIR=/home/you/Qt/
export QTVERSION=5.9.5
cd $QTDIR/$QTVERSION/Src/qtbase/src/plugins/sqldrivers
$QTDIR/$QTVERSION/gcc_64/bin/qmake sqldrivers.pro
cd mysql
make
make install # if you want; it installs it in the bin dir of $QTVERSION
In the past, this was not necessary for Qt 5.5 (where I did this last time).
On a side note, there is no longer a special thread-safe version of libmysqlclient (libmysqlclient_r). It's just one one. Last time I ran into that link error, I just edited the generate Makefile to use the non-_r.

Cross-compile Qt5 (Mingw) with MySQL driver issue

I tried a lot of things I read on documentations or tutorials... But I still can't cross-compile Qt with the MySQL library driver. Story begins:
I have to cross-compile Qt from Linux to Windows (32bits) using MinGW (i686-w64-mingw32- prefix to be precise). I downloaded qtbase-opensource-src-5.9.4 and started to work like that:
$ ./configure -prefix [...]/qt-5.9.4 -xplatform win32-g++ -device-option CROSS_COMPILE=/usr/bin/i686-w64-mingw32- -opengl desktop -no-sql-odbc -sql-sqlite -enable-shared -nomake examples -nomake tests -release
And it worked. Then, make and make install worked too. But I figured out that there was only the SQLite ".dll" in the plugins/sqldrivers repository. So I tried to compile the MySQL driver and... How can I do that?? If I add -sql-mysql to my ./configure ... call, I have to following error:
ERROR: Feature 'sql-mysql' was enabled, but the pre-condition 'libs.mysql' failed.
I downloaded MySQL library from the official website to get mysql-5.7.21-win32.zip. But how to link it with qtbase sources? Adding MYSQL_INCDIR=[...]/mysql-5.7.21-win32/include and MYSQL_LIBDIR=[...]/mysql-5.7.21-win32/lib or just MYSQL_PREFIX=[...]/mysql-5.7.21-win32 to my ./configure ... command results to the same error message.
Any idea please?
EDIT:
I tried to compile the driver like that (with the qmake I built previously):
$ cd qtbase-opensource-src-5.9.4/src/plugins/sqldrivers/mysql
$ [/*prefix of my previous build*/]/qt-5.9.4/bin/qmake .
And the result is:
Project ERROR: Library 'mysql' is not defined.
Ok everyone, I found the solution: do a clean checkout or add -recheck-all to the ./configure command when you want to add a lib like MySQL in my case. Easy isn't it? To make it clear, here is the command I used:
./configure -prefix [...] -xplatform win32-g++ -device-option CROSS_COMPILE=/usr/bin/i686-w64-mingw32- -opengl desktop -no-sql-odbc -sql-sqlite -plugin-sql-mysql MYSQL_PREFIX=[...] -enable-shared -nomake examples -nomake tests -release -recheck-all
That was just a refresh problem, but I prefer to let this answer because it's pretty hard to realize.
Instead of what you tried in your EDIT, should I think be:
$ cd qtbase-opensource-src-5.9.4/src/plugins/sqldrivers
qmake -- MYSQL_PREFIX= [...]/mysql-5.7.21-win32/include

Unable to install a previous version of Sphinx

I'm trying to install a previous version of Sphinx i.e. 2.1.7, and here is what I did to accomplish the goal:
Downloaded the old version from this link, and extracted it at Desktop.
Entered in to the project directory, and cd ~/Downloads/sphinx/sphinx/api/libsphinxclient, and did ./configure --with-mysql
Did make & make install.
Included ~/Downloads/sphinx/sphinx/bin in $PATH, and wrote this functionality in .bash_profile.
Now, when I do searchd --version, it gives me the following error:
dyld: Library not loaded: /opt/local/lib/mysql55/mysql/libmysqlclient.18.dylib
Referenced from: /Users/arslanali/Downloads/sphinx/bin/searchd
Reason: image not found
Trace/BPT trap: 5
Now, I've tried every solution to get rid of this error, but unable to get it resolved.
Edit:
I'd had the latest version of MySQL, and when returned to previous, the mentioned error went away, but now I'm facing the following error each time I do: searchd --version,
dyld: Library not loaded: /opt/local/lib/libexpat.1.dylib
Referenced from: /Users/arslanali/Downloads/sphinx/bin/searchd
Reason: Incompatible library version: searchd requires version 8.0.0 or later, but libexpat.1.dylib provides version 7.0.0
Trace/BPT trap: 5
I was facing same issue, I was trying to compile the binary release, Its already compiled (which is why you have a searchd command despite not compiling).
So we need source of thinking sphinx in order to compile it successfully.
1) Downloaded source from
http://sphinxsearch.com/downloads/sphinx-2.1.7-release.tar.gz/thankyou.html
2) Un-zip downloaded file.
3) Cd into unzipped folder.
4) Run following commands.
./configure --with-mysql
make
make install
5) Done :)
Try this command:
sudo install_name_tool -change libmysqlclient.18.dylib
/usr/local/mysql/lib/libmysqlclient.16.dylib /usr/local/bin/indexer
you might have to change the paths to match yours.

Building a C project on Ubuntu linux has error 'mysql/mysql.h no such file'

I know it's a stupid question but I'm new to Ubuntu. The C files in the project build ok but then I get the error
mysql/mysql.h no such file or directory.
I've tried installing mysql server using apt-get but I still get the same error.
is there something else I should install to see the mysql files ?
You need to install the libmysqlclient-dev package.
On Windows eclipse mingw32, the include path mysql/mysql.h is not working either. I have to use mysql.h without path but with Properties > C/C++ General > Path and Symbols > Includes adding the exact directory where mysql.h resides.
Its probably a problem of path delimiters on windows though I have tested both \ and /.

MySQL for Qt on Mac

I have wasted around 6 hours trying to get MySQL working with Qt following all sorts of instructions from the web. I want to cut my wrist off now!
Does anyone have a simple and a verbose explanation of how to install QMYSQL driver into Qt?
I have Mac 10.6 and I am a beginner n00b.
Your help will be appreciated from the bottom of my heart!
Sana.
EDIT:
I get the following files when I do the grep, so among these just for kicks I copied libqsqlmysql.dylib into all of the folders, but still I don't get to compile... I get an error saying that QSqlDatabase: QMYSQL driver not loaded
/Library/Application Support/DivX/QtPlugins/sqldrivers/libqsqlite.dylib
/Users/pfn368/QtSDK/Assistant.app/Contents/PlugIns/sqldrivers/libqsqlite.dylib
/Users/pfn368/QtSDK/Designer.app/Contents/PlugIns/sqldrivers/libqsqlite.dylib
/Users/pfn368/QtSDK/Desktop/Qt/4.8.0/gcc/plugins/sqldrivers/libqsqlite.dylib
/Users/pfn368/QtSDK/Desktop/Qt/4.8.0/gcc/plugins/sqldrivers/libqsqlite_debug.dylib
/Users/pfn368/QtSDK/Desktop/Qt/474/gcc/plugins/sqldrivers/libqsqlite.dylib
/Users/pfn368/QtSDK/Desktop/Qt/474/gcc/plugins/sqldrivers/libqsqlite_debug.dylib
/Users/pfn368/QtSDK/Madde/sysroots/harmattan-arm-sysroot/usr/lib/qt4/plugins/sqldrivers/libqsqlite.so
/Users/pfn368/QtSDK/Madde/sysroots/harmattan-nokia-arm-sysroot/usr/lib/qt4/plugins/sqldrivers/libqsqlite.so
/Users/pfn368/QtSDK/Maemo/4.6.2/sysroots/fremantle-arm-sysroot-20.2010.36-2-slim/usr/lib/qt4/plugins/sqldrivers/libqsqlite.so
/Users/pfn368/QtSDK/Qt Creator.app/Contents/MacOS/qmlpuppet.app/Contents/PlugIns/sqldrivers/libqsqlite.dylib
/Users/pfn368/QtSDK/Qt Creator.app/Contents/PlugIns/sqldrivers/libqsqlite.dylib
/Users/pfn368/QtSDK/QtSources/4.8.0/plugins/sqldrivers/libqsqlite.dylib
/Users/pfn368/QtSDK/QtSources/4.8.0/plugins/sqldrivers/libqsqlite_debug.dylib
/Users/pfn368/QtSDK/Simulator/Application/simulator.app/Contents/PlugIns/sqldrivers/libqsqlite.dylib
/Users/pfn368/QtSDK/Simulator/Qt/gcc/plugins/sqldrivers/libqsqlite.dylib
/Users/pfn368/QtSDK/Simulator/Qt/gcc/plugins/sqldrivers/libqsqlite_debug.dylib
This is my .pro file
QT += sql core gui\
network
TARGET = mini-stock-exchange
TEMPLATE = app
SOURCES += ./src/main.cpp\
./src/mainwindow.cpp
HEADERS += ./header/mainwindow.h
FORMS += ./ui/mainwindow.ui
My includes
#include "./header/mainwindow.h"
#include "ui_mainwindow.h"
#include <QtSql/QSqlDatabase>
#include <QtSql/QSqlQuery>
#include <QFile>
#include <QtSql/QSqlError>
#include <qsqldatabase.h>
#include <QtCore>
#include <QtSql>
Code to call the database
QSqlDatabase defaultDB = QSqlDatabase::addDatabase("QMYSQL3");
if ( !defaultDB.isValid() ) {
qWarning( "Failed to connect to the database driver" );
}
defaultDB.setDatabaseName( "nicu" );
defaultDB.setUserName( "root" );
defaultDB.setPassword( "root" );
defaultDB.setHostName( "http://localhost:8889" );
First download the Qt SDK sources and a version of the mysql server sources, extract them both.
Create Symlinks to MySQL's lib files:
sudo ln -s /Users/simon/Downloads/mysql-5.6.11-osx10.7-x86_64/lib/libmysqlclient.18.dylib /usr/lib/libmysqlclient.dylib
sudo ln -s /Users/simon/Downloads/mysql-5.6.11-osx10.7-x86_64/lib/libmysqlclient.18.dylib /usr/lib/libmysqlclient_r.dylib
sudo ln -s /Users/simon/Downloads/mysql-5.6.11-osx10.7-x86_64/lib/libmysqlclient.18.dylib /usr/lib/libmysqlclient.18.dylib
sudo ln -s /Users/simon/Downloads/mysql-5.6.11-osx10.7-x86_64/lib/libmysqlclient.18.dylib /usr/lib/libmysqlclient_r.18.dylib
After that cd to your extracted Qt SDK into the folder /Users/simon/Downloads/qt-everywhere-opensource-src-4.8.4/src/plugins/sqldrivers/mysql
Build the Libraries:
qmake -spec macx-g++ -o Makefile "INCLUDEPATH+=/Users/simon/Downloads/mysql-5.6.11-osx10.7-x86_64/include" "LIBS+=-L/usr/lib -lmysqlclient_r" mysql.pro
make
mv libqsqlmysql_debug.dylib libqsqlmysql.dylib
cp -R libqsqlmysql.dylib /Developer/Applications/Qt/plugins/sqldrivers/
After that you should be able to use the QMYSQL plugin. Check if the library was loaded correctly with this line of code (put it in some constructor so that you'll the the output right after starting the app):
qDebug() << QCoreApplication::libraryPaths();
qDebug() << QSqlDatabase::drivers();
For e.g. my output looks like this now:
("/Developer/Applications/Qt/plugins", "/Users/simon/Coding/qt4c/build-SQLtable-Desktop-Debug/SQLtable.app/Contents/MacOS")
("QSQLITE", "QMYSQL3", "QMYSQL", "QODBC3", "QODBC", "QPSQL7", "QPSQL")
The Qt 4 packages from Mac Homebrew have an option to install mysql-drivers as a Qt Plugin for default (it's not a default option, that's why you are missing this).
Download and install Mac OS X Homebrew software as described here: http://brew.sh.
After installing homebrew, remove the previously Qt4 installation.
If you have installed it using brew just type on the terminal:
$ brew remove qt4
To install it with mysql support run:
$ brew install qt4 --with-mysql
And everytime you need to install a package with some options in brew but you don't know the supported options just type:
$ brew options FORMULA_NAME
And it will show all the build options available for the given formula.
Sana, I'm going to try save that hand, alrighty.
First you need the Qt everywhere opensource package from the internets. I've got no idea what version of Qt you're running, but you need the same version as your core install.
Second, you get into the source package and go through the plugins until you find the sql drivers and the mysql plugin. At this stage make sure you know what path your mysql libs and includes are on, as you'll need them.
Third, modify the .pro for the mysql plugin and add your MySql include path to INCLUDEPATH+= and the libs to....LIBS+=
Fourth, run qmake on the .pro to generate a Makefile, then make the make file and you'll have some pretty little .dylib files looking at you with eyes full of joy.
Fifth, the fun part, you need to find where the other plugins are, and this varies system by system. Quickest way to find them is in Terminal type 'sudo find / | grep libqsqlite' and it'll give you the location of that, and that's your Qt sqldrivers plugin directory. Copy the dylib you just built into there.
And that should do it. It's worked for me on quite a few builds and rebuilds.