Qt offers a great abstraction to connect to different databases (MySQL, SQLite, etc.). For each database a driver is required which handles the communication with the database system. In Qt, the drivers are located at
plugins/sqldrivers (in the directory of the Qt version). For some databases there are already some pre-compiled drivers available (e.g.
qsqlmysql to connect to a MySQL database). Unfortunately, IBM's DB2 is not on that list, so it needs to be compiled manually. In this article, I want to show the corresponding building instructions.
I use Qt 5.7.1 and IBM's DB2 Express-C edition at version 11.1. To build the driver you need the sources for Qt. They can, for instance, be fetched with the maintenance tool (Add or remove components → Qt 5.7 → check Sources). Here, I am building the driver with Visual Studio 2015 Update 3 (using the compiler for the 64 bit target architecture) on a machine with Windows 10 x64 installed. The following paths are used:
C:\Qt\5.7\msvc2015_64\bin: location of the build tool
C:\Qt\5.7\Src: location of Qt's source code (default location when the sources are fetched with the maintenance tool)
C:\Program Files\IBM\SQLLIB: location inside IBM's install directory where the sql library is located
If you use different paths, adjust them accordingly in the following build instructions.
There is a bug in Qt 5.7.1 (which seems to be fixed in Qt 5.8.0) so that the sources need to be adjusted. Open the file
C:\Qt\5.7\Src\qtbase\src\sql\drivers\db2\qsql_db2.cppand change the lines 1190 and 1191 to
d->hEnv = reinterpret_cast<SQLHANDLE>(env); d->hDbc = reinterpret_cast<SQLHANDLE>(con);
- Open the Visual Studio command line prompt (e.g. VS2015 x64 Native Tools Command Prompt) with administration privileges
cdto the directory
- Run the command
C:\Qt\5.7\msvc2015_64\bin\qmake "INCLUDEPATH+=C:/progra~1/IBM/SQLLIB/include" "LIBS+=C:/progra~1/IBM/SQLLIB/lib/db2cli.lib"
- Adjust the path, if you have installed DB2 to a different directory
- Make sure you replace
progra~1(otherwise there might be some problems due to the space)
nmaketo build the library. There might be some warnings, but it should work nevertheless. If successful, the produced libs should be located at
nmake installso that the produced libs will be copied to
- Start Qt Creator, open the
sqlbrowserexample project, build and run it. If successful, you should now be able to select the
QDB2driver from the list and connect to your database
If the application is started from inside Qt Creator, the dynamic libraries are automatically loaded, if they are located in
C:\Qt\5.7\msvc2015_64\plugins\sqldrivers (this is also true for the other libraries in the
plugins folder). When you deploy your Qt application, you need to make sure that the dynamic library
qsqldb2.dll is located in a folder named
sqldrivers alongside your application's executable, i.e. a directory structure like:
ApplicationFolder |-- sqldrivers | `-- qsqldb2.dll |-- Application.exe `-- <<other stuff>>
This process can be automated by using the Windows Deployment Tool.