SQL Database Drivers | Qt SQL 6.7.3
Description: How to configure and install Qt SQL drivers for supported databases.

- Qt 6.7
- Qt SQL
- SQL Database Drivers
- Qt 6.7.3 Reference Documentation

# 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 / Firebird
QMYSQL / MARIADB | MySQL or MariaDB (version 5.6 and above)
QOCI | Oracle Call Interface Driver (version 12.1 and above)
QODBC | Open Database Connectivity (ODBC) - Microsoft SQL Server and other ODBC-compliant databases
QPSQL | PostgreSQL (versions 7.3 and above)
QSQLITE | SQLite version 3
QMIMER | Mimer SQL (version 11 and above)

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).

Note: 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-<driver> ........ Enable SQL <driver> 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
       Mimer SQL .............................. yes
       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
       Mimer SQL .............................. yes
       MySql .................................. yes
       OCI (Oracle) ........................... no
       ODBC ................................... yes
       PostgreSQL ............................. no
       SQLite ................................. yes
         Using system provided SQLite ......... no
     ...

The particulars for each driver are explained below.

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

It's possible to only compile a specific SQL driver when Qt is already built or installed as binary version. But you have to make sure to install the exact same version of the Qt sources (for example through the Qt Maintenance Tool) - otherwise you might get compile errors due to changed apis. Also make sure to properly set up the build environment by executing the appropriate Qt command prompt in the Windows Start menu.

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
       Mimer SQL .............................. yes
       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

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

Note: You need to specify `CMAKE_INSTALL_PREFIX`, if you want to install plugins after the build is finished.

## Driver Specifics

### QMYSQL for MySQL or MariaDB 5.6 and higher

MariaDB is a fork of MySQL intended to remain free and open-source software under the GNU General Public License. MariaDB intended to maintain high compatibility with MySQL, ensuring a drop-in replacement capability with library binary parity and exact matching with MySQL APIs and commands. Therefore the plugin for MySQL and MariaDB are combined into one Qt plugin.

#### QMYSQL Stored Procedure Support

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

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

Source code to access the OUT values:

     QSqlQuery q;
     q.exec("call qtestproc (@outval1, @outval2)");
     q.exec("select @outval1, @outval2");
     if (q.next())
         qDebug() << q.value(0) << 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 `-DMySQL_LIBRARY=<path/to/mysqld/>libmysqld.<so|lib|dylib>` 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.

#### Connection options

The Qt MySQL/MariaDB plugin honors the following connection options:

Attribute | Possible value
--- | ---
CLIENT_COMPRESS | If set, switches to compressed protocol after successful authentication
CLIENT_FOUND_ROWS | If set, send found rows instead of affected rows
CLIENT_IGNORE_SPACE | If set, ignore spaces before '('
CLIENT_NO_SCHEMA | If set, don't allow database.table.column
CLIENT_INTERACTIVE | If set, client is treated as interactive
MYSQL_OPT_PROTOCOL | explicitly specify the protocol to use: MYSQL_PROTOCOL_TCP: use tcp connection (ip/hostname specified through setHostname()) MYSQL_PROTOCOL_SOCKET: connect through a socket specified in UNIX_SOCKET MYSQL_PROTOCOL_PIPE: connect through a named pipe specified in UNIX_SOCKET MYSQL_PROTOCOL_MEMORY: connect through shared memory specified in MYSQL_SHARED_MEMORY_BASE_NAME
UNIX_SOCKET | Specifies the socket or named pipe to use, even it's called UNIX_SOCKET it can also be used on windows
MYSQL_SHARED_MEMORY_BASE_NAME | Specified the shared memory segment name to use
MYSQL_OPT_RECONNECT | TRUE or 1: Automatically reconnect after connection loss FALSE or 0: No automatic reconnect after connection loss (default) See Automatic Reconnection Control
MYSQL_OPT_CONNECT_TIMEOUT | The connect timeout in seconds
MYSQL_OPT_READ_TIMEOUT | The timeout in seconds for each attempt to read from the server
MYSQL_OPT_WRITE_TIMEOUT | The timeout in seconds for each attempt to write to the server
MYSQL_OPT_LOCAL_INFILE | Set to 1 to enable the support for local LOAD_DATA, disabled if not set or 0
MYSQL_OPT_SSL_MODE | The security state to use for the connection to the server: SSL_MODE_DISABLED, SSL_MODE_PREFERRED, SSL_MODE_REQUIRED, SSL_MODE_VERIFY_CA, SSL_MODE_VERIFY_IDENTITY.
MYSQL_OPT_TLS_VERSION | A list of protocols the client permits for encrypted connections. The value can be a combination of 'TLSv1' ,' TLSv1.1', 'TLSv1.2' or 'TLSv1.3' depending on the used MySQL server version.
MYSQL_OPT_SSL_KEY / SSL_KEY (deprecated) | The path name of the client private key file
MYSQL_OPT_SSL_CERT / SSL_CERT (deprecated) | The path name of the client public key certificate file
MYSQL_OPT_SSL_CA / SSL_CA (deprecated) | The path name of the Certificate Authority (CA) certificate file
MYSQL_OPT_SSL_CAPATH / SSL_CAPATH (deprecated) | The path name of the directory that contains trusted SSL CA certificate files
MYSQL_OPT_SSL_CIPHER / SSL_CIPHER (deprecated) | The list of permissible ciphers for SSL encryption
MYSQL_OPT_SSL_CRL | The path name of the file containing certificate revocation lists
MYSQL_OPT_SSL_CRLPATH | The path name of the directory that contains files containing certificate revocation lists

For more detailed information about the connect options please refer to the mysql_options() MySQL documentation.

#### How to Build the QMYSQL Plugin on Unix and macOS

You need the MySQL / MariaDB header files, as well as the shared library `libmysqlclient.<so|dylib>` / `libmariadb.<so|dylib>`. Depending on your Linux distribution, you may need to install a package which is usually called "mysql-devel" or "mariadb-devel".

Tell `qt-cmake` where to find the MySQL / MariaDB header files and shared libraries (here it is assumed that MySQL / MariaDB is installed in `/usr/local`) and build:

     mkdir build-sqldrivers
     cd build-sqldrivers
     qt-cmake -G Ninja <qt_source_directory>/qtbase/src/plugins/sqldrivers -DCMAKE_INSTALL_PREFIX=<qt_installation_path>/<platform> -DMySQL_INCLUDE_DIR="/usr/local/mysql/include" -DMySQL_LIBRARY="/usr/local/mysql/lib/libmysqlclient.<so|dylib>"
     cmake --build .
     cmake --install .

#### How to Build the QMYSQL Plugin on Windows

You need to get the MySQL installation files (e.g. MySQL web installer or MariaDB C Connector). 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`

and for MariaDB

- `<MariaDB dir>/lib/libmariadb.lib`
- `<MariaDB dir>/lib/libmariadb.dll`
- `<MariaDB dir>/include/mysql.h`

Note: 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:\mysql-8.0.22-winx64`):

     mkdir build-sqldrivers
     cd build-sqldrivers
     qt-cmake -G Ninja <qt_installation_path>\Src\qtbase\src\plugins\sqldrivers -DCMAKE_INSTALL_PREFIX=<qt_installation_path>\<platform> -DMySQL_INCLUDE_DIR="C:\mysql-8.0.22-winx64\include" -DMySQL_LIBRARY="C:\mysql-8.0.22-winx64\lib\libmysql.lib"
     cmake --build .
     cmake --install .

When you distribute your application, remember to include libmysql.dll / libmariadb.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 connecting to Oracle database as determined by the version of the instant client used. This is dependent on what Oracle indicates it supports. 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.

#### Connection options

The Qt OCI plugin honors the following connection options:

Attribute | Possible value
--- | ---
OCI_ATTR_PREFETCH_ROWS | Sets the OCI attribute OCI_ATTR_PREFETCH_ROWS to the specified value
OCI_ATTR_PREFETCH_MEMORY | Sets the OCI attribute OCI_ATTR_PREFETCH_MEMORY to the specified value
OCI_AUTH_MODE | OCI_SYSDBA: authenticate for SYSDBA access OCI_SYSOPER: authenticate for SYSOPER access OCI_DEFAULT: authenticate with normal access see OCISessionBegin for more information about the access modes

#### How to Build the OCI Plugin on Unix and macOS

All you need is the " - Basic" and "Instant Client Package - SDK".

Oracle library files required to build the driver:

- `libclntsh.<so|dylib>` (all versions)

Tell `qt-cmake` where to find the Oracle header files and shared libraries and build.

We assume that you installed the RPM packages of the Instant Client Package SDK (you need to adjust the version number accordingly):

     mkdir build-sqldrivers
     cd build-sqldrivers
     qt-cmake -G Ninja <qt_source_directory>/qtbase/src/plugins/sqldrivers -DCMAKE_INSTALL_PREFIX=<qt_installation_path>/<platform> -DOracle_INCLUDE_DIR="/usr/include/oracle/21/client64" -DOracle_LIBRARY="/usr/lib/oracle/21/client64/lib/libclntsh.<so|dylib>"
     cmake --build .
     cmake --install .

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.

#### 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` and SDK is installed in `C:\oracle\sdk`):

     mkdir build-sqldrivers
     cd build-sqldrivers
     qt-cmake -G Ninja <qt_installation_path>\Src\qtbase\src\plugins\sqldrivers -DCMAKE_INSTALL_PREFIX=<qt_installation_path>\<platform> -DOracle_INCLUDE_DIR="C:\oracle\sdk\include" -DOracle_LIBRARY="C:\oracle\oci.lib"
     cmake --build .
     cmake --install .

When you run your application, you will also need to add the `oci.dll` path to your `PATH` environment variable:

     set PATH=%PATH%;C:\oracle

### QODBC for Open Database Connectivity (ODBC)

ODBC is a general interface that allows you to connect to multiple DBMSs using a common interface. The QODBC driver allows you to connect to an ODBC driver manager and access the available data sources. Note that you also need to install and configure ODBC drivers for the ODBC driver manager that is installed on your system. The QODBC plugin then allows you to use these data sources in your Qt applications.

Note: You should use the native driver, if it is available, instead of the ODBC driver. ODBC support can be used as a fallback for compliant databases if no native driver is available.

On Windows, an ODBC driver manager is installed by default. For Unix systems, there are some implementations which must be installed first. Note that every end user of your application is required to have an ODBC driver manager installed, otherwise the QODBC plugin will not work.

When connecting to an ODBC datasource, you should pass the name of the ODBC datasource (DSN) to the QSqlDatabase::setDatabaseName() function, rather than the actual database name. It's also possible to pass a FILEDSN (*.dsn) filename or a complete ODBC driver string. When passing a driver string you must make sure, that all parameters (username, password, ...) are properly escaped. Passing the username or password through the QSqlDatabase functions, the escaping is done by the QODBC plugin.

The QODBC Plugin needs an ODBC compliant driver manager version 2.0 or later. Some ODBC drivers claim to be version-2.0-compliant, but do not offer all the necessary functionality. The QODBC plugin therefore checks whether the data source can be used after a connection has been established, and refuses to work if the check fails. If you do not like this behavior, you can remove the `#define ODBC_CHECK_DRIVER` line from the file `qsql_odbc.cpp`. Do this at your own risk!

By default, Qt instructs the ODBC driver to behave as an ODBC 2.x driver. However, for some driver-manager/ODBC 3.x-driver combinations (e.g., unixODBC/MaxDB ODBC), telling the ODBC driver to behave as a 2.x driver can cause the driver plugin to have unexpected behavior. To avoid this problem, instruct the ODBC driver to behave as a 3.x driver by setting the connect option `"SQL_ATTR_ODBC_VERSION=SQL_OV_ODBC3"` before you open your database connection. Note that this will affect multiple aspects of ODBC driver behavior, e.g., the SQLSTATEs. Before setting this connect option, consult your ODBC documentation about behavior differences you can expect.

If you experience very slow access of the ODBC datasource, make sure that ODBC call tracing is turned off in the ODBC datasource manager.

Some drivers do not support scrollable cursors. In that case, only queries in QSqlQuery::setForwardOnly() mode can be used successfully.

#### ODBC Stored Procedure Support

With Microsoft SQL Server the result set returned by a stored procedure that uses the return statement, or returns multiple result sets, will be accessible only if you set the query's forward only mode to forward using QSqlQuery::setForwardOnly().

     // STORED_PROC uses the return statement or returns multiple result sets
     QSqlQuery query;
     query.setForwardOnly(true);
     query.exec("{call STORED_PROC}");

Note: The value returned by the stored procedure's return statement is discarded.

#### ODBC Unicode Support

The QODBC Plugin will use the Unicode API if UNICODE is defined. On Windows based systems, this is the default. Note that the ODBC driver and the DBMS must also support Unicode.

For the Oracle 9 ODBC driver (Windows), it is necessary to check "SQL_WCHAR support" in the ODBC driver manager otherwise Oracle will convert all Unicode strings to local 8-bit representation.

#### Connection options

The Qt ODBC plugin honors the following connection options:

Attribute | Possible value
--- | ---
SQL_ATTR_ACCESS_MODE | SQL_MODE_READ_ONLY: open the database in read-only mode SQL_MODE_READ_WRITE: open the database in read-write mode (default)
SQL_ATTR_LOGIN_TIMEOUT | Number of seconds to wait for the database connection during login (a value of 0 will wait forever)
SQL_ATTR_CONNECTION_TIMEOUT | Number of seconds to wait for any request to the database (a value of 0 will wait forever)
SQL_ATTR_CURRENT_CATALOG | The catalog (database) to use for this connection
SQL_ATTR_METADATA_ID | SQL_TRUE: the string argument of catalog functions are treated as identifiers SQL_FALSE: the string arguments of catalog functions are not treated as identifiers
SQL_ATTR_PACKET_SIZE | Specifies the network packet size in bytes
SQL_ATTR_TRACEFILE | A string containing the name of the trace file
SQL_ATTR_TRACE | SQL_OPT_TRACE_ON: Enable database query tracing SQL_OPT_TRACE_OFF: Disable database query tracing (default)
SQL_ATTR_CONNECTION_POOLING | Enable or disable connection pooling at the environment level. SQL_CP_DEFAULT, SQL_CP_OFF: Connection pooling is turned off (default) SQL_CP_ONE_PER_DRIVER: A single connection pool is supported for each driver SQL_CP_ONE_PER_HENV: A single connection pool is supported for each environment
SQL_ATTR_ODBC_VERSION | SQL_OV_ODBC3: The driver should act as a ODBC 3.x driver SQL_OV_ODBC2: The driver should act as a ODBC 2.x driver (default)

For more detailed information about the connect options please refer to the SQLSetConnectAttr() ODBC documentation.

#### How to Build the ODBC Plugin on Unix and macOS

It is recommended that you use unixODBC. You can find the latest version and ODBC drivers at http://www.unixodbc.org. You need the unixODBC header files and shared libraries.

Tell `qt-cmake` where to find the unixODBC header files and shared libraries (here it is assumed that unixODBC is installed in `/usr/local/unixODBC`) and build:

     mkdir build-sqldrivers
     cd build-sqldrivers
     qt-cmake -G Ninja <qt_source_directory>/qtbase/src/plugins/sqldrivers -DCMAKE_INSTALL_PREFIX=<qt_installation_path>/<platform> -DODBC_INCLUDE_DIR="/usr/local/unixODBC/include" -DODBC_LIBRARY="/usr/local/unixODBC/lib/libodbc.<so|dylib>"
     cmake --build .
     cmake --install .

#### How to Build the ODBC Plugin on Windows

The ODBC header and include files should already be installed in the right directories. You just have to build the plugin as follows:

     mkdir build-sqldrivers
     cd build-sqldrivers
     qt-cmake -G Ninja <qt_installation_path>\Src\qtbase\src\plugins\sqldrivers -DCMAKE_INSTALL_PREFIX=<qt_installation_path>\<platform>
     cmake --build .
     cmake --install .

### QPSQL for PostgreSQL (Version 7.3 and above)

The QPSQL driver supports version 7.3 and higher of the PostgreSQL server.

For more information about PostgreSQL visit http://www.postgresql.org.

#### QPSQL Unicode Support

The QPSQL driver automatically detects whether the PostgreSQL database you are connecting to supports Unicode or not. Unicode is automatically used if the server supports it. Note that the driver only supports the UTF-8 encoding. If your database uses any other encoding, the server must be compiled with Unicode conversion support.

Unicode support was introduced in PostgreSQL version 7.1 and it will only work if both the server and the client library have been compiled with multibyte support. More information about how to set up a multibyte enabled PostgreSQL server can be found in the PostgreSQL Administrator Guide, Chapter 5.

#### QPSQL Case Sensitivity

PostgreSQL databases will only respect case sensitivity if the table or field name is quoted when the table is created. So for example, a SQL query such as:

     CREATE TABLE "testTable" ("id" INTEGER);

will ensure that it can be accessed with the same case that was used. If the table or field name is not quoted when created, the actual table name or field name will be lower-case. When QSqlDatabase::record() or QSqlDatabase::primaryIndex() access a table or field that was unquoted when created, the name passed to the function must be lower-case to ensure it is found. For example:

     QString tableString("testTable");
     QSqlQuery q;
     // Create table query is not quoted, therefore it is mapped to lower case
     q.exec(QString("CREATE TABLE %1 (id INTEGER)").arg(tableString));
     // Call toLower() on the string so that it can be matched
     QSqlRecord rec = database.record(tableString.toLower());

#### QPSQL Forward-only query support

To use forward-only queries, you must build the QPSQL plugin with PostreSQL client library version 9.2 or later. If the plugin is built with an older version, then forward-only mode will not be available - calling QSqlQuery::setForwardOnly() with `true` will have no effect.

Warning: If you build the QPSQL plugin with PostgreSQL version 9.2 or later, then you must distribute your application with libpq version 9.2 or later. Otherwise, loading the QPSQL plugin will fail with the following message:

     QSqlDatabase: QPSQL driver not loaded
     QSqlDatabase: available drivers: QSQLITE QMYSQL QMARIADB QODBC QPSQL
     Could not create database object

While navigating the results in forward-only mode, the handle of QSqlResult may change. Applications that use the low-level handle of SQL result must get a new handle after each call to any of QSqlResult fetch functions. Example:

     QSqlQuery query;
     QVariant v;
     query.setForwardOnly(true);
     query.exec("SELECT * FROM table");
     while (query.next()) {
         // Handle changes in every iteration of the loop
         v = query.result()->handle();

         if (qstrcmp(v.typeName(), "PGresult*") == 0) {
             PGresult *handle = *static_cast<PGresult **>(v.data());
             if (handle) {
                 // Do something...
             }
         }
     }

While reading the results of a forward-only query with PostgreSQL, the database connection cannot be used to execute other queries. This is a limitation of libpq library. Example:

     int value;
     QSqlQuery query1;
     query1.setForwardOnly(true);
     query1.exec("select * FROM table1");
     while (query1.next()) {
         value = query1.value(0).toInt();
         if (value == 1) {
             QSqlQuery query2;
             query2.exec("update table2 set col=2");  // WRONG: This will discard all results of
         }                                            // query1, and cause the loop to quit
     }

This problem will not occur if query1 and query2 use different database connections, or if we execute query2 after the while loop.

Note: Some methods of QSqlDatabase like tables(), primaryIndex() implicitly execute SQL queries, so these also cannot be used while navigating the results of forward-only query.

Note: QPSQL will print the following warning if it detects a loss of query results:

     QPSQLDriver::getResult: Query results lost - probably discarded on executing another SQL query.

#### Connection options

The Qt PostgreSQL plugin honors all connection options specified in the connect() PostgreSQL documentation.

#### How to Build the QPSQL Plugin on Unix and macOS

You need the PostgreSQL client library and headers installed.

To make `qt-cmake` find the PostgreSQL header files and shared libraries, build the plugin the following way (assuming that the PostgreSQL client is installed in `/usr/local/pgsql`):

     mkdir build-psql-driver
     cd build-psql-driver

     qt-cmake -G Ninja <qt_source_directory>/qtbase/src/plugins/sqldrivers-DCMAKE_INSTALL_PREFIX=<qt_installation_path>/<platform> -DCMAKE_INCLUDE_PATH="/usr/local/pgsql/include" -DCMAKE_LIBRARY_PATH="/usr/local/pgsql/lib"
     cmake --build .
     cmake --install .

#### How to Build the QPSQL Plugin on Windows

Install the appropriate PostgreSQL developer libraries for your compiler. Assuming that PostgreSQL was installed in `C:\pgsql`, build the plugin as follows:

     mkdir build-sqldrivers
     cd build-sqldrivers
     qt-cmake -G Ninja <qt_installation_path>\Src\qtbase\src\plugins\sqldrivers -DCMAKE_INSTALL_PREFIX=<qt_installation_path>\<platform> -DCMAKE_INCLUDE_PATH="C:\pgsql\include" -DCMAKE_LIBRARY_PATH="C:\pgsql\lib"
     cmake --build .
     cmake --install .

Users of MinGW may wish to consult the following online document: PostgreSQL MinGW/Native Windows.

When you distribute your application, remember to include libpq.dll in your installation package. It must be placed in the same folder as the application executable.

### QDB2 for IBM DB2 (Version 7.1 and above)

The Qt DB2 plugin makes it possible to access IBM DB2 databases. It has been tested with IBM DB2 v7.1 and 7.2. You must install the IBM DB2 development client library, which contains the header and library files necessary for compiling the QDB2 plugin.

The QDB2 driver supports prepared queries, reading/writing of Unicode strings and reading/writing of BLOBs.

We suggest using a forward-only query when calling stored procedures in DB2 (see QSqlQuery::setForwardOnly()).

#### Connection options

The Qt IBM DB2 plugin honors the following connection options:

Attribute | Possible value
--- | ---
SQL_ATTR_ACCESS_MODE | SQL_MODE_READ_ONLY: open the database in read-only mode SQL_MODE_READ_WRITE: open the database in read-write mode (default)
SQL_ATTR_LOGIN_TIMEOUT | Number of seconds to wait for the database connection during login (max: 32767, a value of 0 will wait forever)

#### How to Build the QDB2 Plugin on Unix and macOS

     mkdir build-sqldrivers
     cd build-sqldrivers
     qt-cmake -G Ninja <qt_source_directory>/qtbase/src/plugins/sqldrivers -DCMAKE_INSTALL_PREFIX=<qt_installation_path>/<platform> -DDB2_INCLUDE_DIR="/usr/local/db2/include" -DDB2_LIBRARY="/usr/local/db2/lib/libdb2.<so|dylib>"
     cmake --build .
     cmake --install .

#### How to Build the QDB2 Plugin on Windows

The DB2 header and include files should already be installed in the right directories. You just have to build the plugin as follows:

     mkdir build-sqldrivers
     cd build-sqldrivers
     qt-cmake -G Ninja <qt_installation_path>\Src\qtbase\src\plugins\sqldrivers -DCMAKE_INSTALL_PREFIX=<qt_installation_path>\<platform> -DDB2_INCLUDE_DIR="C:\db2\include" -DDB2_LIBRARY="C:\db2\lib\db2.lib"
     cmake --build .
     cmake --install .

### QSQLITE for SQLite (Version 3 and above)

The Qt SQLite plugin makes it possible to access SQLite databases. SQLite is an in-process database, which means that it is not necessary to have a database server. SQLite operates on a single file, which must be set as the database name when opening a connection. If the file does not exist, SQLite will try to create it. SQLite also supports in-memory and temporary databases. Simply pass respectively ":memory:" or an empty string as the database name.

SQLite has some restrictions regarding multiple users and multiple transactions. If you try to read/write on a resource from different transactions, your application might freeze until one transaction commits or rolls back. The Qt SQLite driver will retry to write to a locked resource until it runs into a timeout (see `QSQLITE_BUSY_TIMEOUT` at QSqlDatabase::setConnectOptions()).

In SQLite any column, with the exception of an INTEGER PRIMARY KEY column, may be used to store any type of value. For instance, a column declared as INTEGER may contain an integer value in one row and a text value in the next. This is due to SQLite associating the type of a value with the value itself rather than with the column it is stored in. A consequence of this is that the type returned by QSqlField::type() only indicates the field's recommended type. No assumption of the actual type should be made from this and the type of the individual values should be checked.

The driver is locked for updates while a select is executed. This may cause problems when using QSqlTableModel because Qt's item views fetch data as needed (with QSqlQuery::fetchMore() in the case of QSqlTableModel).

You can find information about SQLite on http://www.sqlite.org.

#### Connection options

The Qt SQLite plugin honors the following connection options:

Attribute | Possible value
--- | ---
QSQLITE_BUSY_TIMEOUT | Busy handler timeout in milliseconds (val <= 0: disabled), see SQLite documentation for more information
QSQLITE_USE_QT_VFS | If set, the database is opened using Qt's VFS which allows to open databases using QFile. This way it can open databases from any read-write locations (e.g.android shared storage) but also from read-only resources (e.g. qrc or android assets). Be aware that when opening databases from read-only resources make sure you add QSQLITE_OPEN_READONLY attribute as well. Otherwise it will fail to open it.
QSQLITE_OPEN_READONLY | If set, the database is open in read-only mode which will fail if no database exists. Otherwise the database will be opened in read-write mode and created if the database file does not yet exist (default)
QSQLITE_OPEN_URI | The given filename is interpreted as an uri, see SQLITE_OPEN_URI
QSQLITE_ENABLE_SHARED_CACHE | If set, the database is opened in shared cache mode, otherwise in private cache mode
QSQLITE_ENABLE_REGEXP | If set, the plugin defines a function 'regex' which can be used in queries, QRegularExpression is used for evaluation of the regex query
QSQLITE_NO_USE_EXTENDED_RESULT_CODES | Disables the usage of the extended result code feature in SQLite
QSQLITE_ENABLE_NON_ASCII_CASE_FOLDING | If set, the plugin replaces the functions 'lower' and 'upper' with QString functions for correct case folding of non-ascii characters
QSQLITE_OPEN_NOFOLLOW | If set, the database filename is not allowed to contain a symbolic link

#### How to Build the QSQLITE Plugin

SQLite version 3 is included as a third-party library within Qt. It can be built by passing the `-DFEATURE_system_sqlite=OFF` parameter to the `qt-cmake` command line.

If you do not want to use the SQLite library included with Qt, you can pass `-DFEATURE_system_sqlite=ON` to the `qt-cmake` command line to use the SQLite libraries of the operating system. This is recommended whenever possible, as it reduces the installation size and removes one component for which you need to track security advisories.

On Unix and macOS (replace `$SQLITE` with the directory where SQLite resides):

     mkdir build-sqldrivers
     cd build-sqldrivers
     qt-cmake -G Ninja <qt_source_directory>/qtbase/src/plugins/sqldrivers -DCMAKE_INSTALL_PREFIX=<qt_installation_path>/<platform> -DFEATURE_system_sqlite=ON -DCMAKE_INCLUDE_PATH="$SQLITE/include" -DCMAKE_LIBRARY_PATH="$SQLITE/lib"
     cmake --build .
     cmake --install .

On Windows (assuming that SQLite is installed in `C:\SQLITE`):

     mkdir build-sqldrivers
     cd build-sqldrivers
     qt-cmake -G Ninja <qt_installation_path>\Src\qtbase\src\plugins\sqldrivers -DCMAKE_INSTALL_PREFIX=<qt_installation_path>\<platform> -DFEATURE_system_sqlite=ON -DCMAKE_INCLUDE_PATH="C:\SQLITE\include" -DCMAKE_LIBRARY_PATH="C:\SQLITE\lib"
     cmake --build .
     cmake --install .

#### Enable REGEXP operator

SQLite comes with a REGEXP operation. However the needed implementation must be provided by the user. For convenience a default implementation can be enabled by setting the connect option `QSQLITE_ENABLE_REGEXP` before the database connection is opened. Then a SQL statement like "column REGEXP 'pattern'" basically expands to the Qt code

     column.contains(QRegularExpression("pattern"));

For better performance the regular expressions are cached internally. By default the cache size is 25, but it can be changed through the option's value. For example passing "`QSQLITE_ENABLE_REGEXP=10`" reduces the cache size to 10.

#### QSQLITE File Format Compatibility

SQLite minor releases sometimes break file format forward compatibility. For example, SQLite 3.3 can read database files created with SQLite 3.2, but databases created with SQLite 3.3 cannot be read by SQLite 3.2. Please refer to the SQLite documentation and change logs for information about file format compatibility between versions.

Qt minor releases usually follow the SQLite minor releases, while Qt patch releases follow SQLite patch releases. Patch releases are therefore both backward and forward compatible.

To force SQLite to use a specific file format, it is necessary to build and ship your own database plugin with your own SQLite library as illustrated above. Some versions of SQLite can be forced to write a specific file format by setting the `SQLITE_DEFAULT_FILE_FORMAT` define when building SQLite.

### QMIMER for Mimer SQL version 11 and higher

The Qt Mimer SQL plugin makes it possible to work with the Mimer SQL RDBMS. Mimer SQL provides small footprint, scalable and robust relational database solutions that conform to international ISO SQL standards. Mimer SQL is available on Windows, Linux, macOS, and OpenVMS as well as several embedded platforms like QNX, Android, and embedded Linux.

Mimer SQL fully support Unicode. To work with Unicode data the column types National Character (NCHAR), National Character Varying (NVARCHAR), or National Character Large Object (NCLOB) must be used. For more information about Mimer SQL and unicode, see https://developer.mimer.com/features/multilingual-support

#### QMIMER Stored Procedure Support

Mimer SQL have stored procedures according to the SQL standard (PSM) and the plugin fully support IN, OUT, INOUT parameters as well as resultset procedures.

Example stored procedure with INOUT and OUT parameters:

     create procedure inout_proc (INOUT param1 INT, OUT param2 INT)
     BEGIN
         set param1 = param1 * 2;
         set param2 = param1 * param1;
     END

Source code to access the INOUT and OUT values:

         QSqlDatabase db;
         QSqlQuery query;
         int i1 = 10, i2 = 0;
         query.prepare("call qtestproc(?, ?)");
         query.bindValue(0, i1, QSql::InOut);
         query.bindValue(1, i2, QSql::Out);
         query.exec();

#### How to Build the QMIMER Plugin on Unix and macOS

You need the Mimer SQL header files and shared libraries. Get them by installing any of the Mimer SQL variants found at https://developer.mimer.com.

     mkdir build-sqldrivers
     cd build-sqldrivers

     qt-cmake -G Ninja <qt_installation_path>\Src\qtbase\src\plugins\sqldrivers -DCMAKE_INSTALL_PREFIX=<qt_installation_path>\<platform> -DMimer_INCLUDE_DIR="/usr/include" -DMimer_LIBRARIES="/usr/lib/libmimer.so"
     cmake --build .
     cmake --install .

#### How to Build the QMIMER Plugin on Windows

You need the Mimer SQL header files and shared libraries. Get them by installing any of the Mimer SQL variants found at https://developer.mimer.com.

     mkdir build-sqldrivers
     cd build-sqldrivers

     qt-cmake -G Ninja <qt_installation_path>\Src\qtbase\src\plugins\sqldrivers -DCMAKE_INSTALL_PREFIX=<qt_installation_path>\<platform> -DMimer_INCLUDE_DIR="C:\Program Files\Mimer SQL Experience 11.0\dev\include" -DMimer_LIBRARIES="C:\Program Files\Mimer SQL Experience 11.0\dev\lib\amd64\mimapi64.lib|C:\Program Files\Mimer SQL Experience 11.0\dev\lib\x86\mimapi32.lib"
     cmake --build .
     cmake --install .

### QIBASE for Borland InterBase

The Qt InterBase plugin makes it possible to access the InterBase and Firebird databases. InterBase can either be used as a client/server or without a server in which case it operates on local files. The database file must exist before a connection can be established. Firebird must be used with a server configuration.

Note that InterBase requires you to specify the full path to the database file, no matter whether it is stored locally or on another server.

#### Connection options

The Qt Borland InterBase plugin honors the following connection options:

Attribute | Possible value
--- | ---
ISC_DPB_SQL_ROLE_NAME | Specifies the login role name

#### How to Build the QIBASE Plugin

     QSqlDatabase db;
     db.setHostName("MyServer");
     db.setDatabaseName("C:\\test.gdb");

You need the InterBase/Firebird development headers and libraries to build this plugin.

Due to license incompatibilities with the GPL, users of the Qt Open Source Edition are not allowed to link this plugin to the commercial editions of InterBase. Please use Firebird or the free edition of InterBase.

#### QIBASE Stored procedures

InterBase/Firebird return OUT values as result set, so when calling stored procedure, only IN values need to be bound via QSqlQuery::bindValue(). The RETURN/OUT values can be retrieved via QSqlQuery::value(). Example:

     QSqlQuery q;
     q.exec("execute procedure my_procedure");
     if (q.next())
         qDebug() << q.value(0); // outputs the first RETURN/OUT value

#### How to Build the QIBASE Plugin on Unix and macOS

The following assumes InterBase or Firebird is installed in `/opt/interbase`:

If you are using InterBase:

     mkdir build-sqldrivers
     cd build-sqldrivers
     qt-cmake -G Ninja <qt_source_directory>/qtbase/src/plugins/sqldrivers -DCMAKE_INSTALL_PREFIX=<qt_installation_path>/<platform> -DInterbase_INCLUDE_DIR="/opt/interbase/include" -DInterbase_LIBRARY="/opt/interbase/lib/libgds.<so|dylib>"
     cmake --build .
     cmake --install .

If you are using Firebird, the Firebird library has to be set explicitly:

     mkdir build-sqldrivers
     cd build-sqldrivers
     qt-cmake -G Ninja <qt_source_directory>/qtbase/src/plugins/sqldrivers -DCMAKE_INSTALL_PREFIX=<qt_installation_path>/<platform> -DInterbase_INCLUDE_DIR="/opt/interbase/include" -DInterbase_LIBRARY="/opt/interbase/lib/libfbclient.<so|dylib>"
     cmake --build .
     cmake --install .

#### How to Build the QIBASE Plugin on Windows

The following assumes InterBase or Firebird is installed in `C:\interbase`:

If you are using InterBase:

     mkdir build-sqldrivers
     cd build-sqldrivers
     qt-cmake -G Ninja <qt_installation_path>\Src\qtbase\src\plugins\sqldrivers -DCMAKE_INSTALL_PREFIX=<qt_installation_path>\<platform> -DInterbase_INCLUDE_DIR="C:\interbase\include" -DInterbase_LIBRARY="C:\interbase\gds.lib"
     cmake --build .
     cmake --install .

If you are using Firebird:

     mkdir build-sqldrivers
     cd build-sqldrivers
     qt-cmake -G Ninja <qt_installation_path>\Src\qtbase\src\plugins\sqldrivers -DCMAKE_INSTALL_PREFIX=<qt_installation_path>\<platform> -DInterbase_INCLUDE_DIR="C:\interbase\include" -DInterbase_LIBRARY="C:\interbase\lib\fbclient_ms.lib"
     cmake --build .
     cmake --install .

Note that `C:\interbase\bin` must be in the `PATH`.

## Troubleshooting

You should always use client libraries that have been compiled with the same compiler as you are using for your project. If you cannot get a source distribution to compile the client libraries yourself, you must make sure that the pre-compiled library is compatible with your compiler, otherwise you will get a lot of "undefined symbols" errors.

If the compilation of a plugin succeeds but it cannot be loaded afterwards, check out the following steps to find out the culprit:

1. Ensure that the plugin is in the correct directory. You can use QApplication::libraryPaths() to determine where Qt looks for plugins.
2. Ensure that the client libraries of the DBMS are available on the system. On Unix, run the command `ldd` and pass the name of the plugin as parameter, for example `ldd libqsqlmysql.so`. You will get a warning if any of the client libraries could not be found. On Windows, you can use Visual Studio's dependency walker or Dependencies GUI to find out the dependent libraries. With Qt Creator, you can update the `PATH` environment variable in the Run section of the Project panel to include the path to the folder containing the client libraries.
3. When using MSVC, also make sure the plugin is built with the correct build type. Due to different MSVC runtimes for debug and release, a Qt debug build can't load a Qt release plugin and vice versa.
4. Run the compiled Qt executable with the environment variable QT_DEBUG_PLUGINS set to get very verbose debug output when loading plugins.
5. To retrieve possible debug messages from the SQL subsystem, enable the output by setting the environment variable `QT_LOGGING_RULES` to `qt.sql.*.debug=true`. Don't forget to enable the console when working on windows. See Logging Rules for a more detailed explanation on how to set logging rules.

Make sure you have followed the guide to Deploying Plugins.

## How to Write Your Own Database Driver

QSqlDatabase is responsible for loading and managing database driver plugins. When a database is added (see QSqlDatabase::addDatabase()), the appropriate driver plugin is loaded (using QSqlDriverPlugin). QSqlDatabase relies on the driver plugin to provide interfaces for QSqlDriver and QSqlResult.

QSqlDriver is an abstract base class which defines the functionality of a SQL database driver. This includes functions such as QSqlDriver::open() and QSqlDriver::close(). QSqlDriver is responsible for connecting to a database, establish the proper environment, etc. In addition, QSqlDriver can create QSqlQuery objects appropriate for the particular database API. QSqlDatabase forwards many of its function calls directly to QSqlDriver which provides the concrete implementation.

QSqlResult is an abstract base class which defines the functionality of a SQL database query. This includes statements such as `SELECT`, `UPDATE`, and `ALTER` `TABLE`. QSqlResult contains functions such as QSqlResult::next() and QSqlResult::value(). QSqlResult is responsible for sending queries to the database, returning result data, etc. QSqlQuery forwards many of its function calls directly to QSqlResult which provides the concrete implementation.

QSqlDriver and QSqlResult are closely connected. When implementing a Qt SQL driver, both of these classes must to be subclassed and the abstract virtual methods in each class must be implemented.

To implement a Qt SQL driver as a plugin (so that it is recognized and loaded by the Qt library at runtime), the driver must use the Q_PLUGIN_METADATA() macro. Read How to Create Qt Plugins for more information on this. You can also check out how this is done in the SQL plugins that are provided with Qt in `QTDIR/qtbase/src/plugins/sqldrivers`.

The following code can be used as a skeleton for a SQL driver:

     class XyzResult : public QSqlResult
     {
     public:
         XyzResult(const QSqlDriver *driver)
             : QSqlResult(driver) {}
         ~XyzResult() {}

     protected:
         QVariant data(int /* index */) override { return QVariant(); }
         bool isNull(int /* index */) override { return false; }
         bool reset(const QString & /* query */) override { return false; }
         bool fetch(int /* index */) override { return false; }
         bool fetchFirst() override { return false; }
         bool fetchLast() override { return false; }
         int size() override { return 0; }
         int numRowsAffected() override { return 0; }
         QSqlRecord record() const override { return QSqlRecord(); }
     };

     class XyzDriver : public QSqlDriver
     {
     public:
         XyzDriver() {}
         ~XyzDriver() {}

         bool hasFeature(DriverFeature /* feature */) const override { return false; }
         bool open(const QString & /* db */, const QString & /* user */,
                   const QString & /* password */, const QString & /* host */,
                   int /* port */, const QString & /* options */) override
             { return false; }
         void close() override {}
         QSqlResult *createResult() const override { return new XyzResult(this); }
     };