SQL Database Drivers▲
The Qt SQL module uses driver plugins to communicate with the different database APIs. Since Qt's SQL Module API is database-independent, all database-specific code is contained within these drivers. Several drivers are supplied with Qt, and other drivers can be added. The driver source code is supplied and can be used as a model for writing your own drivers.
Supported Databases▲
The table below lists the drivers included with Qt:
Driver name |
DBMS |
---|---|
IBM DB2 (version 7.1 and above) |
|
MySQL or MariaDB (version 5.6 and above) |
|
Oracle Call Interface Driver (version 12.1 and above) |
|
Open Database Connectivity (ODBC) - Microsoft SQL Server and other ODBC-compliant databases |
|
PostgreSQL (versions 7.3 and above) |
|
SQLite version 3 |
SQLite is the in-process database system with the best test coverage and support on all platforms. Oracle via OCI, PostgreSQL, and MySQL through either ODBC or a native driver are well-tested on Windows and Linux. The completeness of the support for other systems depends on the availability and quality of client libraries.
To build a driver plugin you need to have the appropriate client library for your Database Management System (DBMS). This provides access to the API exposed by the DBMS, and is typically shipped with it. Most installation programs also allow you to install "development libraries", and these are what you need. These libraries are responsible for the low-level communication with the DBMS. Also make sure to install the correct database libraries for your Qt architecture (32 or 64 bit).
When using Qt under Open Source terms but with a proprietary database, verify the client library's license compatibility with the LGPL.
Building the Drivers▲
Compile Qt with a specific driver▲
The Qt configure script tries to automatically detect the available client libraries on your machine. Run configure -help to see what drivers can be built. You should get an output similar to this:
[...]
Database options:
-
sql-&
lt;driver&
gt; ........ Enable SQL &
lt;driver&
gt; plugin. Supported drivers:
db2 ibase mysql oci odbc psql sqlite
[all auto
]
-
sqlite .............. Select used sqlite [system/
qt]
[...]
The configure script cannot detect the necessary libraries and include files if they are not in the standard paths, so it may be necessary to specify these paths using either driver-specific include and library path variables or CMAKE_INCLUDE_PATH and CMAKE_LIBRARY_PATH. For example, if your MySQL files are installed in C:\mysql-connector-c-6.1.11-winx64 on Windows, then pass the following parameter to double-dash part of configure line:
C
:
\Qt\6.0.0
\Src\configure.bat -
sql-
mysql --
-
DMySQL_INCLUDE_DIR=
"C:\mysql-8.0.22-winx64\include"
-
DMySQL_LIBRARY=
"C:\mysql-8.0.22-winx64\lib\libmysql.lib"
Configure summary:
...
Qt Sql Drivers:
DB2 (IBM) .............................. no
InterBase .............................. no
MySql .................................. yes
OCI (Oracle) ........................... no
ODBC ................................... yes
PostgreSQL ............................. no
SQLite ................................. yes
Using system provided SQLite ......... no
...
When you configure drivers in the manner described above, CMake skips any dependency checks and uses the provided paths as is. This is especially useful if the package provides its own set of system libraries that should not be recognized by the build routine.
In some cases it's more convenient to use CMAKE_INCLUDE_PATH and CMAKE_LIBRARY_PATH variables to locate required libraries. You should prefer this method if module needs to set properties for the provided target libraries (e.g. this is required for PostgreSQL and SQLite). For example, you can do this as follows, to locate MySQL:
C
:
\Qt\6.0.0
\Src\configure.bat -
sql-
mysql --
-
DCMAKE_INCLUDE_PATH=
"C:\mysql-8.0.22-winx64\include"
-
DCMAKE_LIBRARY_PATH=
"C:\mysql-8.0.22-winx64\lib"
Configure summary:
...
Qt Sql Drivers:
DB2 (IBM) .............................. no
InterBase .............................. no
MySql .................................. yes
OCI (Oracle) ........................... no
ODBC ................................... yes
PostgreSQL ............................. no
SQLite ................................. yes
Using system provided SQLite ......... no
...
The particulars for each driver are explained below.
If something goes wrong and you want CMake to recheck your available drivers, you might need to remove CMakeCache.txt from the build directory.
Compile only a specific sql driver▲
A typical qt-cmake run (in this case to configure for MySQL) looks like this:
C
:
\Qt\6.0.0
\mingw81_64\bin\qt-
cmake -
G"Ninja"
C:\Qt\6.0.0
\Src\qtbase\src\plugins\sqldrivers -
DMySQL_INCLUDE_DIR=
"C:\mysql-8.0.22-winx64\include"
-
DMySQL_LIBRARY=
"C:\mysql-8.0.22-winx64\lib\libmysql.lib"
-
DCMAKE_INSTALL_PREFIX=
"C:\Qt\6.0.0\mingw81_64"
Configure summary:
Qt Sql Drivers:
DB2 (IBM) .............................. no
InterBase .............................. no
MySql .................................. yes
OCI (Oracle) ........................... no
ODBC ................................... yes
PostgreSQL ............................. no
SQLite ................................. yes
Using system provided SQLite ......... no
--
Configuring done
--
Generating done
--
Build files have been written to: C:/
build-
qt6-
sqldrivers
As mentioned in Compile Qt with a specific driver, if the driver could not be found or is not enabled, start over by removing CMakeCache.txt.
Due to the practicalities of dealing with external dependencies, only the SQLite plugin is shipped with binary builds of Qt. Binary builds of Qt for Windows also include the ODBC plugin. To be able to add additional drivers to the Qt installation without re-building all of Qt, it is possible to configure and build the qtbase/src/plugins/sqldrivers directory outside of a full Qt build directory. Note that it is not possible to configure each driver separately, only all of them at once. Drivers can be built separately, though.
You need to specify CMAKE_INSTALL_PREFIX, if you want to install plugins after the build is finished.