Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MySQL for Qt on Mac

Tags:

mysql

macos

qt

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" );
like image 543
Sana Avatar asked Sep 28 '11 06:09

Sana


2 Answers

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") 
like image 66
dersimn Avatar answered Oct 13 '22 22:10

dersimn


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.

like image 31
mannysz Avatar answered Oct 13 '22 23:10

mannysz