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

QDB2

IBM DB2 (version 7.1 and above)

QIBASE

Borland InterBase

QMYSQL

MySQL (version 5.0 and above)

QOCI

Oracle Call Interface Driver

QODBC

Open Database Connectivity (ODBC) - Microsoft SQL Server and other ODBC-compliant databases

QPSQL

PostgreSQL (versions 7.3 and above)

QSQLITE2

SQLite version 2

obsolete since Qt 5.14

QSQLITE

SQLite version 3

QTDS

Sybase Adaptive Server

obsolete since Qt 4.7

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.

Note: 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

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:

 
Sélectionnez
[...]

Database options:

  -sql-<driver> ........ Enable SQL <driver> plugin. Supported drivers:
                         db2 ibase mysql oci odbc psql sqlite2 sqlite tds
                         [all auto]
  -sqlite .............. Select used sqlite3 [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 the *_INCDIR=, *_LIBDIR=, or *_PREFIX= command-line options. For example, if your MySQL files are installed in /usr/local/mysql (or in C:/Program Files/MySQL/MySQL Connector C 6.1 on Windows), then pass the following parameter to configure: MYSQL_PREFIX=/usr/local/mysql (or MYSQL_PREFIX="C:/Program Files/MySQL/MySQL Connector C 6.1" for Windows). The particulars for each driver are explained below.

If something goes wrong and you want qmake to recheck your available drivers, you must remove config.cache in <QTDIR>/qtbase/src/plugins/sqldrivers - otherwise qmake will not search for the available drivers again. If you encounter an error during the qmake stage, open config.log to see what went wrong.

A typical qmake run (in this case to configure for MySQL) looks like this:

 
Sélectionnez
C:\Qt5\5.13.2\Src\qtbase\src\plugins\sqldrivers&gt;qmake -version
QMake version 3.1
Using Qt version 5.13.2 in C:/Qt5/5.13.2/mingw73_64/lib
C:\Qt5\5.13.2\Src\qtbase\src\plugins\sqldrivers&gt;qmake -- MYSQL_INCDIR="C:/Program Files/MySQL/MySQL Connector C 6.1/include" MYSQL_LIBDIR="C:/Program Files/MySQL/MySQL Connector C 6.1/lib"
Info: creating stash file C:\Qt5\5.13.2\Src\qtbase\src\plugins\sqldrivers\.qmake.stash

Running configuration tests...
Checking for DB2 (IBM)... no
Checking for InterBase... no
Checking for MySQL... yes
Checking for OCI (Oracle)... no
Checking for ODBC... yes
Checking for PostgreSQL... no
Checking for SQLite (version 2)... no
Checking for TDS (Sybase)... no
Done running configuration tests.

Configure summary:

Qt Sql Drivers:
  DB2 (IBM) .............................. no
  InterBase .............................. no
  MySql .................................. yes
  OCI (Oracle) ........................... no
  ODBC ................................... yes
  PostgreSQL ............................. no
  SQLite2 ................................ no
  SQLite ................................. yes
    Using system provided SQLite ......... no
  TDS (Sybase) ........................... no

Qt is now configured for building. Just run 'mingw32-make'.
Once everything is built, you must run 'mingw32-make install'.
Qt will be installed into 'C:\Qt5\5.13.2\mingw73_64'.

Prior to reconfiguration, make sure you remove any leftovers from the previous build.

Due to the practicalities of dealing with external dependencies, only the SQLite3 plugin is shipped with binary builds of Qt. 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. If the Qt build is configured with -prefix, it is necessary to install the plugins after building them, too. For example:

 
Sélectionnez
cd $QTDIR/qtbase/src/plugins/sqldrivers/mysql
make install

Driver Specifics

   

QMYSQL for MySQL 5 and higher

 
QMYSQL Stored Procedure Support

MySQL 5 has stored procedure support at the SQL level, but no API to control IN, OUT, and INOUT parameters. Therefore, parameters have to be set and read using SQL commands instead of QSqlQuery::bindValue().

Example stored procedure:

 
Sélectionnez
create procedure qtestproc (OUT param1 INT, OUT param2 INT)
BEGIN
    set param1 = 42;
    set param2 = 43;
END

Source code to access the OUT values:

 
Sélectionnez
QSqlQuery q;
q.exec("call qtestproc (@outval1, @outval2)");
q.exec("select @outval1, @outval2");
q.next();
qDebug() &lt;&lt; q.value(0) &lt;&lt; q.value(1); // outputs "42" and "43"

Note: @outval1 and @outval2 are variables local to the current connection and will not be affected by queries sent from another host or connection.

Embedded MySQL Server

The MySQL embedded server is a drop-in replacement for the normal client library. With the embedded MySQL server, a MySQL server is not required to use MySQL functionality.

To use the embedded MySQL server, simply link the Qt plugin to libmysqld instead of libmysqlclient. This can be done by adding MYSQL_LIBS=-lmysqld to the configure command line.

Please refer to the MySQL documentation, chapter "libmysqld, the Embedded MySQL Server Library" for more information about the MySQL embedded server.

How to Build the QMYSQL Plugin on Unix and macOS

You need the MySQL header files, as well as the shared library libmysqlclient.so. Depending on your Linux distribution, you may need to install a package which is usually called "mysql-devel".

Tell qmake where to find the MySQL header files and shared libraries (here it is assumed that MySQL is installed in /usr/local) and run make:

 
Sélectionnez
cd $QTDIR/qtbase/src/plugins/sqldrivers
qmake -- MYSQL_PREFIX=/usr/local
make sub-mysql
How to Build the QMYSQL Plugin on Windows

Get the MySQL installer (e.g. mysql-installer-web-community-8.0.18.0.msi). Run the installer, select "Custom" installation, and install the MySQL C Connector which matches your Qt installation (x86 or x64). After installation check that the needed files are there:

  • <MySQL dir>/lib/libmysql.lib

  • <MySQL dir>/lib/libmysql.dll

  • <MySQL dir>/include/mysql.h

As of MySQL 8.0.19, the C Connector is no longer offered as a standalone installable component. Instead, you can get mysql.h and libmysql.* by installing the full MySQL Server (x64 only) or the MariaDB C Connector.

Build the plugin as follows (here it is assumed that <MySQL dir> is C:/Program Files/MySQL/MySQL Connector C 6.1):

 
Sélectionnez
cd %QTDIR%\qtbase\src\plugins\sqldrivers
qmake -- MYSQL_INCDIR="C:/Program Files/MySQL/MySQL Connector C 6.1/include" MYSQL_LIBDIR="C:/Program Files/MySQL/MySQL Connector C 6.1/lib"
nmake sub-mysql
nmake install

If you are not using a Microsoft compiler, replace nmake with mingw32-make above.

When you distribute your application, remember to include libmysql.dll in your installation package. It must be placed in the same folder as the application executable. libmysql.dll additionally needs the MSVC runtime libraries which can be installed with vcredist.exe

 

QOCI for the Oracle Call Interface (OCI)

The Qt OCI plugin supports Oracle 9i, 10g and higher. After connecting to the Oracle server, the plugin will auto-detect the database version and enable features accordingly.

It's possible to connect to a Oracle database without a tnsnames.ora file. This requires that the database SID is passed to the driver as the database name, and that a hostname is given.

OCI User Authentication

The Qt OCI plugin supports authentication using external credentials (OCI_CRED_EXT). Usually, this means that the database server will use the user authentication provided by the operating system instead of its own authentication mechanism.

Leave the username and password empty when opening a connection with QSqlDatabase to use the external credentials authentication.

OCI BLOB/LOB Support

Binary Large Objects (BLOBs) can be read and written, but be aware that this process may require a lot of memory. You should use a forward only query to select LOB fields (see QSqlQuery::setForwardOnly()).

Inserting BLOBs should be done using either a prepared query where the BLOBs are bound to placeholders or QSqlTableModel, which uses a prepared query to do this internally.

How to Build the OCI Plugin on Unix and macOS

For Oracle 10g, all you need is the "Instant Client Package - Basic" and "Instant Client Package - SDK". For Oracle prior to 10g, you require the standard Oracle client and the SDK packages.

Oracle library files required to build the driver:

  • libclntsh.so (all versions)

  • libwtc9.so (only Oracle 9)

Tell qmake where to find the Oracle header files and shared libraries and run make:

For Oracle version 9:

 
Sélectionnez
cd $QTDIR/qtbase/src/plugins/sqldrivers
qmake -- OCI_INCDIR="$ORACLE_HOME/rdbms/public" OCI_LIBDIR="$ORACLE_HOME/lib" OCI_LIBS="-lclntsh -lwtc9"
make sub-oci

For Oracle version 10, we assume that you installed the RPM packages of the Instant Client Package SDK (you need to adjust the version number accordingly):

 
Sélectionnez
cd $QTDIR/qtbase/src/plugins/sqldrivers
qmake -- OCI_INCDIR=/usr/include/oracle/10.1.0.3/client OCI_LIBDIR=/usr/lib/oracle/10.1.0.3/client/lib
make sub-oci

Note: If you are using the Oracle Instant Client package, you will need to set LD_LIBRARY_PATH when building the OCI SQL plugin, and when running an application that uses the OCI SQL plugin. You can avoid this requirement by setting RPATH, and listing all of the libraries to link to. Here is an example:

 
Sélectionnez
configure OCI_INCDIR=/usr/include/oracle/10.1.0.3/client OCI_LIBDIR=/usr/lib/oracle/10.1.0.3/client/lib -R /usr/lib/oracle/10.1.0.3/client/lib OCI_LIBS="-lclntsh -lnnz10"
make

If you wish to build the OCI plugin manually with this method, the procedure looks like this:

 
Sélectionnez
cd $QTDIR/qtbase/src/plugins/sqldrivers
qmake -- OCI_INCDIR=/usr/include/oracle/10.1.0.3/client OCI_LIBDIR=/usr/lib/oracle/10.1.0.3/client/lib OCI_LIBS="-Wl,-rpath,/usr/lib/oracle/10.1.0.3/client/lib -lclntsh -lnnz10"
make sub-oci
How to Build the OCI Plugin on Windows

Choosing the option "Programmer" in the Oracle Client Installer from the Oracle Client Installation CD is generally sufficient to build the plugin. For some versions of Oracle Client, you may also need to select the "Call Interface (OCI)" option if it is available.

Build the plugin as follows (here it is assumed that Oracle Client is installed in C:\oracle):