7

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" );
Sana
  • 9,895
  • 15
  • 59
  • 87

3 Answers3

3

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") 
dersimn
  • 805
  • 9
  • 15
  • Perfect. Thank you very much, your post helped a lot. Just one thing where I had trouble with Mac OS X Sierra: In the `qmake` command I needed `"LIBS+=/usr/lib/libmysqlclient_r.dylib"` instead of your LIBS modification to make it work. – Johanna Apr 11 '17 at 17:25
3

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.

mannysz
  • 951
  • 8
  • 19
0

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.

Nicholas Smith
  • 11,642
  • 6
  • 37
  • 55
  • Which MySQL version should I be installing as I don't see libmysql.lib file under my MySQL folder! – Sana Sep 28 '11 at 16:07
  • I do havce the libqsqlmysql_debug.dylib and libqsqlmysql.dylib present under ~/QtSDK/QtSources/4.8.0/plugins/sqldrivers. My question is where do I copy these files into? My Qt Library for the program or somewhere else? – Sana Sep 28 '11 at 17:50
  • Please see my EDIT part of my post... I get too many folders and I have copied the `libqsqlmysql.dylib` into all of the folders and I still get the error! :| – Sana Sep 28 '11 at 18:03
  • They just need to be 'visible' to Qt which generally means whichever working path Qt is in (So QtSDK//). If you have them there and it's still being a pain try loading it as a SQLITE to see if it's a global problem with plugins/sqldrivers or if it's an issue with the MySQL plugin. – Nicholas Smith Sep 29 '11 at 08:14
  • nope I don't get it still after placing the .dylib files in all of the folder. Damn Qt! – Sana Oct 03 '11 at 16:14
  • Well that is just *weird*, last resort option is to try including them in the .app bundle and see if that does it. – Nicholas Smith Oct 04 '11 at 08:19