MySQL for Qt on Mac

I spent about 6 hours trying to get MySQL to work with Qt, following all sorts of instructions from the Internet. I want to cut my wrist now!

Does anyone have a simple and detailed explanation of how to install the QMYSQL driver in Qt?

I have a Mac 10.6 and I'm new to n00b.

Your help will be appreciated from below my heart!

Sana'a.

EDIT:

I get the following files when I do grep, so among them only for kicks, I copied libqsqlmysql.dylib to all folders, but still I'm not going to compile ... I get the error message 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" ); 
+6
source share
3 answers

Mac Homebrew’s Qt 4 packages have the option of installing mysql drivers as the default Qt plugin (this is not the default option, so you are missing it).

Download and install Mac OS X Homebrew software as described here: http://brew.sh .

After installing homebrew, uninstall the previous Qt4 installation.

If you installed it using brew, just type in the terminal:

 $ brew remove qt4 

To install it with mysql support:

 $ brew install qt4 --with-mysql 

And each time you need to install a package with some parameters in brew, but you don’t know which supported parameters just enter:

 $ brew options FORMULA_NAME 

And he will show all the assembly options available for this formula.

+3
source

First, download the Qt SDK sources and the mysql server source version, extract both of them.

Create symlinks in mysql 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 the extracted Qt SDK in the /Users/simon/Downloads/qt-everywhere-opensource-src-4.8.4/src/plugins/sqldrivers/mysql folder

Build 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 can use the QMYSQL plugin. Check if the library with this line of code was loaded correctly (put it in some constructor so that you immediately after starting the application):

 qDebug() << QCoreApplication::libraryPaths(); qDebug() << QSqlDatabase::drivers(); 

For example, my output is as follows:

 ("/Developer/Applications/Qt/plugins", "/Users/simon/Coding/qt4c/build-SQLtable-Desktop-Debug/SQLtable.app/Contents/MacOS") ("QSQLITE", "QMYSQL3", "QMYSQL", "QODBC3", "QODBC", "QPSQL7", "QPSQL") 
+2
source

Sana, I'm going to try to keep that hand, okay.

First you need Qt everywhere the openource package from the internet. I have no idea which version of Qt you are using, but you need the same version as for your basic installation.

Secondly, you get into the source package and look at the plugins until you find the sql drivers and the mysql plugin. At this point, make sure you know which path your mysql libraries and included components are in, since you will need them.

Third, change the .pro for the mysql plugin and add your path to include MySql in INCLUDEPATH + = and libs .... LIBS + =

Fourth, run qmake on .pro to generate the Makefile, then create a make file and you will get pretty small .dylib files looking at you with eyes full of joy.

Fifth, the interesting part, you need to find where other plugins are located, and it depends on the system. The quickest way to find them in the terminal is 'sudo find / | grep libqsqlite' and this will give you the location of this and your Qt sqldrivers plugin directory. Copy the dylib that you just built into it.

And that should do it. This worked for me on several builds and rebuilds.

0
source

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


All Articles