Artificial truth

The more you see, the less you believe.

[archives] [latest] | [homepage] | [atom/rss/twitter]

MySQL and Qt under Windows
Mon 16 April 2012 — download

Using mySQL with Qt under Windows is a little bit tricky.


You need to install several softwares :

  • The Qt SDK, and Qt source code
  • MinGW (bundled with Qt SQK)
  • MinGW-utils
  • MySQL


To avoid path related headaches, you should create a C:/mysql directory, copy mysql headers (.h) from C:\Program Files\MySQL\MySQL Server 5.5\include/" to "C:\mysql\include\"* and copy "libmysql.lib from C:\Program Files\MySQL\MySQL Server 5.5\lib to C:\mysql\lib."


The next step is the creation of the required shared library.

  • Launch qtenv2.bat from "C:\QtSDK\Desktop\Qt\4.8.0\mingw\bin"
  • Go to "C:\mysql\lib"
  • Run reimp -d libmysql.lib
  • Run dlltool -d libmysql.def -D libmysql.dll -l libmysql.a -k to create libmysql.a .


Go to C:\QtSDK\QtSources\4.8.0\src\plugins\sqldrivers\mysql and run :

qmake "INCLUDEPATH+=C:\\mysql\\include" "LIBS+=-LC:\\mysql\\lib -lmysql" mingw32-make all

You should have 4 files by now :

  • libqsqlmysql4.a and qsqlmysql4.dll in the release sub-folder of mysql
  • libqsqlmysqld4.a and qsqlmysqld4.dll in the debug sub folder of mysql


Copy the libmysql.dll from C:\Program Files\MySQL\MySQL Server 5.5\lib to C:\QtSDK\Desktop\Qt\4.8.0\mingw\bin Make sure that the four files that we’ve created earlier are in C:\QtSDK\Desktop\Qt\4.8.0\mingw\plugins\sqldriver

Run !

Also, open up your current Qt project and edit your .pro file so that this line is in there: QT += sql That's it, you can now use MySQL in your Qt project !


This thread, and this blog.