30
Jan
0

C++ – Qt (trolltech) and Mysql, load the driver

You can easily find tutorials about how to use Mysql with QT. But most of time, it is difficult to find how to create the Mysql driver (which can cause the error : “Driver not loaded”).
So here is some help to do that.

Please, note that if the tutorial is done correctly, you DO NOT NEED TO COMPILE QT COMPLETELY (compiling QT takes 1 hours – here 10 minutes are needed).

What you need

QT opensource for windows:
http://www.qtsoftware.com/downloads/sdk-windows-cpp
(“Windows MSI Installer”)

Mysql (community server, for example mysql-5.1.49-win32.msi):
http://dev.mysql.com/downloads/

Mingw-Utils – mingw-utils-0.3.tar.gz:
http://sourceforge.net/projects/mingw/files/
(Select MinGW Utilities – mingw-utils-0.3 – mingw-utils-0.3.tar.gz)

Install

Install QT full and with the initial parameters.

Install MySql, do what you want (maybe you already have a Mysql server running on your computer) but select “custom install” and check Development Components and all sub-features. Also, install “Client Programs” and all sub-features. Please select C:\MYSQL as install directory.

Copy the Bin/files from mingw-utils-0.3.tar.gz to C:\Qt\2009.?\mingw\bin

Let’s Go

in ‘C:\Qt\2009.03\bin’ edit qtenv.bat
in ‘PATH=%PATH%;C:\Qt\2009.03\bin;C:\Qt\2009.03\mingw\bin’ add ‘;C:\MySQL\bin’

copy c:\mysql\bin libmysql.dll to C:\Qt\2009.?\bin

copy c:\mysql\include mysql.h to C:\Qt\2009.?\mingw\include

copy c:\mysql\lib\opt libmysql.lib to C:\Qt\2009.?\mingw\lib

Open QT command prompt:
Edit : You can copy the code in your Qt Command prompt

cd src\plugins\sqldrivers\mysql

reimp -d C:\mysql\lib\opt\libmysql.lib

dlltool -k --input-def LIBMYSQL.def --dllname libmysql.dll --output-lib libmysql.a

qmake "INCLUDEPATH+=C:\mysql\include" "LIBS+=-L. -lmysql" mysql.pro

mingw32-make

Or you can follow the commands :

C:\Qt\2009.03\qt> cd src\plugins\sqldrivers\mysql
C:\Qt\2009.03\qt\src\plugins\sqldrivers\mysql> reimp -d C:\mysql\lib\opt\libmysql.lib
C:\Qt\2009.03\qt\src\plugins\sqldrivers\mysql> dlltool -k –input-def LIBMYSQL.def –dllname libmysql.dll –output-lib libmysql.a
C:\Qt\2009.03\qt\src\plugins\sqldrivers\mysql> qmake “INCLUDEPATH+=C:\mysql\include” “LIBS+=-L. -lmysql” mysql.pro

From Qt doc, you can find more option:

cd %QTDIR%\src\plugins\sqldrivers\mysql
qmake \”INCLUDEPATH+=C:\MySQL\include\” \”LIBS+=C:\MYSQL\MySQL Server mysql.pro

Then:

in %QTDIR%\src\plugins\sqldrivers\mysql
just compile : mingw32-make (or mingw32-make release)

Finally, in your Qt project, in the .pro file add the line

QT += sql

And compile your project

mingw32-make

References

http://christopher.rasch-olsen.no/2009/04/14/qt-45-and-mysql-plugin-with-mingw-on-windows-xp/
http://doc.trolltech.com/4.4/sql-driver.html#how-to-build-the-qmysql-plugin-on-windows
http://www.developpez.net/forums/d794028/c-cpp/bibliotheques/qt/sgbd/utiliser-qt-mysql/

Enjoyed reading this post?
Subscribe to the RSS feed and have all new posts delivered straight to you.

Comments are closed.

Celadon theme by the Themes Boutique