Revision control

Copy as Markdown

// Copyright (C) 2020 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GFDL-1.3-no-invariants-only
/*!
\page sql-driver.html
\title SQL Database Drivers
\brief How to configure and install Qt SQL drivers for supported databases.
The Qt SQL module uses driver \l{How to Create Qt
Plugins}{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
\l{#development}{writing your own drivers}.
\section1 Supported Databases
The table below lists the drivers included with Qt:
\table
\header \li Driver name \li DBMS
\row \li \l{#QDB2}{QDB2} \li IBM DB2 (version 7.1 and above)
\row \li \l{#QIBASE}{QIBASE} \li Borland InterBase / Firebird (legacy InterBase client API)
\row \li \l{#QFIREBIRD}{QFIREBIRD} \li Firebird (version 4.0 and above, modern client API)
\row \li \l{#QMYSQL}{QMYSQL / MARIADB} \li MySQL or MariaDB (version 5.6 and above)
\row \li \l{#QOCI}{QOCI} \li Oracle Call Interface Driver (version 12.1 and above)
\row \li \l{#QODBC}{QODBC}
\li Open Database Connectivity (ODBC) - Microsoft SQL Server and other
ODBC-compliant databases
\row \li \l{#QPSQL}{QPSQL} \li PostgreSQL (versions 7.3 and above)
\row \li \l{#QSQLITE}{QSQLITE} \li SQLite version 3
\row \li \l{#QMIMER}{QMIMER} \li Mimer SQL (version 11 and above)
\endtable
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.
\target building
\section1 Building the Drivers
\target DriverWithQt
\section2 Compile Qt with a specific driver
The Qt \c configure script tries to
automatically detect the available client libraries on your
machine. Run \c{configure -help} to see what drivers can be
built. You should get an output similar to this:
\snippet code/doc_src_sql-driver.qdoc 0
The \c 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 \c CMAKE_INCLUDE_PATH
and \c CMAKE_LIBRARY_PATH. For example, if your MySQL files are
installed in \c{C:\mysql-connector-c-6.1.11-winx64} on Windows, then pass
the following parameter to double-dash part of configure line:
\snippet code/doc_src_sql-driver.qdoc 42
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.
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 \e{CMakeCache.txt} from the build
directory.
\target DriverStandalone
\section2 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 \QMT) -
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 \c qt-cmake run (in this case to configure for MySQL) looks like this:
\snippet code/doc_src_sql-driver.qdoc 41
After configuring with \c qt-cmake, build the driver by running \c ninja.
\note As mentioned in \l{#DriverWithQt}{Compile Qt with a specific driver},
if the driver could not be found or is not enabled, start over by removing
\e{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 and PostgreSQL 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 \c qtbase/src/plugins/sqldrivers directory outside
of a full Qt build directory. Note that it is not possible to
\e configure each driver separately, only all of them at once.
Drivers can be \e built separately, though.
\note You need to specify \c{CMAKE_INSTALL_PREFIX}, if you want to install
plugins after the build is finished.
\section1 Driver Specifics
\target QMYSQL
\section2 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.
\section3 Timestamp Support
Since Qt 6.8, QDateTime values are converted to UTC before insertion and
back from UTC during retrieval. To make this work, the driver sets
the connection time zone to UTC during open() (SET time_zone = '+00:00').
Since MySQL does not store any timezone information, this information is
lost and all retrieved QDateTime values are UTC.
\section3 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:
\snippet code/doc_src_sql-driver.qdoc 1
Source code to access the OUT values:
\snippet code/doc_src_sql-driver.cpp 2
\note \c{@outval1} and \c{@outval2} are variables local to the current
connection and will not be affected by queries sent from another host
or connection.
\section3 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 \c
libmysqld instead of \c libmysqlclient. This can be done by adding
\c{-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.
\section3 Connection options
The Qt MySQL/MariaDB plugin honors the following connection options:
\table
\header \li Attribute \li Possible value
\row
\li CLIENT_COMPRESS
\li If set, switches to compressed protocol after successful authentication
\row
\li CLIENT_FOUND_ROWS
\li If set, send found rows instead of affected rows
\row
\li CLIENT_IGNORE_SPACE
\li If set, ignore spaces before '('
\row
\li CLIENT_NO_SCHEMA
\li If set, don't allow database.table.column
\row
\li CLIENT_INTERACTIVE
\li If set, client is treated as interactive
\row
\li MYSQL_OPT_PROTOCOL
\li explicitly specify the protocol to use:\br
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
\row
\li UNIX_SOCKET
\li Specifies the socket or named pipe to use, even it's called UNIX_SOCKET it
can also be used on windows
\row
\li MYSQL_SHARED_MEMORY_BASE_NAME
\li Specified the shared memory segment name to use
\row
\li MYSQL_OPT_RECONNECT
\li TRUE or 1: Automatically reconnect after connection loss\br
FALSE or 0: No automatic reconnect after connection loss (default)\br
{Automatic Reconnection Control}
\row
\li MYSQL_OPT_CONNECT_TIMEOUT
\li The connect timeout in seconds
\row
\li MYSQL_OPT_READ_TIMEOUT
\li The timeout in seconds for each attempt to read from the server
\row
\li MYSQL_OPT_WRITE_TIMEOUT
\li The timeout in seconds for each attempt to write to the server
\row
\li MYSQL_OPT_LOCAL_INFILE
\li Set to 1 to enable the support for local
disabled if not set or 0
\row
\li MYSQL_OPT_SSL_MODE
\li 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.
Only available when linked against MySQL 5.7.10 or higher.
\row
\li MYSQL_OPT_TLS_VERSION
\li 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 \l
{MySQL server} version.
Only available when linked against MySQL 5.7.11 or higher or MariaDB C Connector 3.1.10.
\row
\li MYSQL_OPT_SSL_KEY / SSL_KEY (deprecated)
\li The path name of the client private key file
\row
\li MYSQL_OPT_SSL_CERT / SSL_CERT (deprecated)
\li The path name of the client public key certificate file
\row
\li MYSQL_OPT_SSL_CA / SSL_CA (deprecated)
\li The path name of the Certificate Authority (CA) certificate file
\row
\li MYSQL_OPT_SSL_CAPATH / SSL_CAPATH (deprecated)
\li The path name of the directory that contains trusted SSL CA certificate files
\row
\li MYSQL_OPT_SSL_CIPHER / SSL_CIPHER (deprecated)
\li The list of permissible ciphers for SSL encryption
\row
\li MYSQL_OPT_SSL_CRL
\li The path name of the file containing certificate revocation lists
\row
\li MYSQL_OPT_SSL_CRLPATH
\li The path name of the directory that contains files containing certificate revocation lists
\row
\li MYSQL_OPT_SSL_VERIFY_SERVER_CERT
\li TRUE or 1: Enable verification of the server's Common Name identity (default)\br
FALSE or 0: Enable verification of the server's Common Name identity\br
Only available when linked against MySQL 5.7.11 or MariaDB, removed with MySQL 8.0.
\endtable
For more detailed information about the connect options please refer
{mysql_options()} MySQL documentation.
\section3 How to Build the QMYSQL Plugin on Unix and \macos
You need the MySQL / MariaDB header files, as well as the shared library
\c{libmysqlclient.<so|dylib>} / \c{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 \c qt-cmake where to find the MySQL / MariaDB header files and shared
libraries (here it is assumed that MySQL / MariaDB is installed in
\c{/usr/local}) and build:
\snippet code/doc_src_sql-driver.qdoc 3
\section3 How to Build the QMYSQL Plugin on Windows
You need to get the MySQL installation files (e.g.
\l {https://dev.mysql.com/downloads/installer/}{MySQL web installer} or
\l {https://downloads.mariadb.com/Connectors/c/}{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:
\list
\li \c {<MySQL dir>/lib/libmysql.lib}
\li \c {<MySQL dir>/lib/libmysql.dll}
\li \c {<MySQL dir>/include/mysql.h}
\endlist
and for MariaDB
\list
\li \c {<MariaDB dir>/lib/libmariadb.lib}
\li \c {<MariaDB dir>/lib/libmariadb.dll}
\li \c {<MariaDB dir>/include/mysql.h}
\endlist
\note As of MySQL 8.0.19, the C Connector is no longer offered as a standalone
installable component. Instead, you can get \c{mysql.h} and \c{libmysql.*} by
installing the full MySQL Server (x64 only) or the
\l{https://downloads.mariadb.org/connector-c/}{MariaDB C Connector}.
Build the plugin as follows (here it is assumed that \c{<MySQL dir>} is
\c{C:\mysql-8.0.22-winx64}):
\snippet code/doc_src_sql-driver.qdoc 5
When you distribute your application, remember to include \e libmysql.dll / \e libmariadb.dll
in your installation package. It must be placed in the same folder
as the application executable. \e libmysql.dll additionally needs the
MSVC runtime libraries which can be installed with
\target QOCI
\section2 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.
\section3 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.
\section3 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.
\section3 Connection options
The Qt OCI plugin honors the following connection options:
\table
\header \li Attribute \li Possible value
\row
\li OCI_ATTR_PREFETCH_ROWS
\li Sets the OCI attribute
{OCI_ATTR_PREFETCH_ROWS} to the specified value
\row
\li OCI_ATTR_PREFETCH_MEMORY
\li Sets the OCI attribute
{OCI_ATTR_PREFETCH_MEMORY} to the specified value
\row
\li OCI_AUTH_MODE
\li OCI_SYSDBA: authenticate for SYSDBA access\br
OCI_SYSOPER: authenticate for SYSOPER access\br
OCI_DEFAULT: authenticate with normal access\br
{OCISessionBegin} for more information about the access modes
\endtable
\section3 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:
\list
\li \c libclntsh.<so|dylib> (all versions)
\endlist
Tell \c 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):
\snippet code/doc_src_sql-driver.qdoc 7
\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.
\section3 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{C:\oracle} and SDK is installed in \c{C:\oracle\sdk}):
\snippet code/doc_src_sql-driver.qdoc 8
When you run your application, you will also need to add the \c oci.dll
path to your \c PATH environment variable:
\snippet code/doc_src_sql-driver.qdoc 9
\target QODBC
\section2 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 \c{#define
ODBC_CHECK_DRIVER} line from the file \c{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 \e{driver-manager/ODBC 3.x-driver}
combinations (e.g., \e{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
\l{QSqlDatabase::setConnectOptions()} {setting the connect option}
\c{"SQL_ATTR_ODBC_VERSION=SQL_OV_ODBC3"} before you
\l{QSqlDatabase::open()} {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 \l QSqlQuery::setForwardOnly() mode can be used successfully.
\section3 Timestamp Support
ODBC is using TIMESTAMP_STRUCT which has no information about any
timezone or similar. Due to this, the QDateTime is used without honoring
the timezone at all.
\note: This might change in the future.
\section3 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 \e forward using \l QSqlQuery::setForwardOnly().
\snippet code/doc_src_sql-driver.cpp 10
\note The value returned by the stored procedure's return statement
is discarded.
\section3 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.
\section3 Connection options
The Qt ODBC plugin honors the following connection options:
\table
\header \li Attribute \li Possible value
\row
\li SQL_ATTR_ACCESS_MODE
\li SQL_MODE_READ_ONLY: open the database in read-only mode\br
SQL_MODE_READ_WRITE: open the database in read-write mode (default)
\row
\li SQL_ATTR_LOGIN_TIMEOUT
\li Number of seconds to wait for the database connection
during login (a value of 0 will wait forever)
\row
\li SQL_ATTR_CONNECTION_TIMEOUT
\li Number of seconds to wait for any request to the database
(a value of 0 will wait forever)
\row
\li SQL_ATTR_CURRENT_CATALOG
\li The catalog (database) to use for this connection
\row
\li SQL_ATTR_METADATA_ID
\li SQL_TRUE: the string argument of catalog functions are treated
as identifiers\br
SQL_FALSE: the string arguments of catalog functions are not
treated as identifiers
\row
\li SQL_ATTR_PACKET_SIZE
\li Specifies the network packet size in bytes
\row
\li SQL_ATTR_TRACEFILE
\li A string containing the name of the trace file
\row
\li SQL_ATTR_TRACE
\li SQL_OPT_TRACE_ON: Enable database query tracing\br
SQL_OPT_TRACE_OFF: Disable database query tracing (default)
\row
\li SQL_ATTR_CONNECTION_POOLING
\li Enable or disable connection pooling at the environment level.\br
SQL_CP_DEFAULT, SQL_CP_OFF: Connection pooling is turned off (default)\br
SQL_CP_ONE_PER_DRIVER: A single connection pool is supported for each driver\br
SQL_CP_ONE_PER_HENV: A single connection pool is supported for each environment
\row
\li SQL_ATTR_ODBC_VERSION
\li SQL_OV_ODBC3: The driver should act as a ODBC 3.x driver\br
SQL_OV_ODBC2: The driver should act as a ODBC 2.x driver (default)
\row
\li SQL_ATTR_CP_MATCH
\li Can be either SQL_CP_STRICT_MATCH, SQL_CP_RELAXED_MATCH or
{SQLConnect()} ODBC documentation for more information
\row
\li SQL_PERCENT_ENCODE_PASSWORD
\li This is a custom Qt ODBC driver option to support drivers (Oracle, PostgreSQL) which need special
characters encoded with percent encoding instead in curly braces like ODBC standard defines.
\endtable
For more detailed information about the connect options please refer
{SQLSetConnectAttr()} ODBC documentation.
\section3 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 \l http://www.unixodbc.org.
You need the unixODBC header files and shared libraries.
Tell \c qt-cmake where to find the unixODBC header files and shared
libraries (here it is assumed that unixODBC is installed in
\c{/usr/local/unixODBC}) and build:
\snippet code/doc_src_sql-driver.qdoc 11
\section3 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:
\snippet code/doc_src_sql-driver.qdoc 12
\target QPSQL
\section2 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 \l http://www.postgresql.org.
\section3 Timestamp Support
Since Qt 6.8, QDateTime values are converted to UTC before insertion
and back from UTC during retrieval. To make this work, the driver sets
the connection time zone to UTC during open() (SET TIME ZONE 'UTC').
Although PostgreSQL has the `timestamptz` column type, the timezone
used during insertion is not preserved and therefore all retrieved
QDateTime values are UTC.
\section3 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.
\section3 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:
\snippet code/doc_src_sql-driver.qdoc 39
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:
\snippet code/doc_src_sql-driver.qdoc 40
\section3 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 \c 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:
\snippet code/doc_src_sql-driver.qdoc 35
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:
\snippet code/doc_src_sql-driver_snippet.cpp 36
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:
\snippet code/doc_src_sql-driver.cpp 37
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:
\snippet code/doc_src_sql-driver.qdoc 38
\section3 Connection options
The Qt PostgreSQL plugin honors all connection options specified in the
{connect()} PostgreSQL documentation.
\section3 How to Build the QPSQL Plugin on Unix and \macos
You need the PostgreSQL client library and headers installed.
To make \c qt-cmake find the PostgreSQL header files and shared
libraries, build the plugin the following way (assuming that the
PostgreSQL client is installed in \c{/usr/local/pgsql}):
\snippet code/doc_src_sql-driver.qdoc 13
\section3 How to Build the QPSQL Plugin on Windows
Install the appropriate PostgreSQL developer libraries for your
compiler. Assuming that PostgreSQL was installed in \c{C:\pgsql},
build the plugin as follows:
\snippet code/doc_src_sql-driver.qdoc 15
Users of MinGW may wish to consult the following online document:
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.
\target QDB2
\section2 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()).
\section3 Connection options
The Qt IBM DB2 plugin honors the following connection options:
\table
\header \li Attribute \li Possible value
\row
\li SQL_ATTR_ACCESS_MODE
\li SQL_MODE_READ_ONLY: open the database in read-only mode\br
SQL_MODE_READ_WRITE: open the database in read-write mode (default)
\row
\li SQL_ATTR_LOGIN_TIMEOUT
\li Number of seconds to wait for the database connection
during login (max: 32767, a value of 0 will wait forever)
\endtable
\section3 How to Build the QDB2 Plugin on Unix and \macos
\snippet code/doc_src_sql-driver.qdoc 18
\section3 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:
\snippet code/doc_src_sql-driver.qdoc 20
\target QSQLITE
\section2 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 \c{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::metaType() 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 \l{http://www.sqlite.org}.
\section3 Timestamp Support
SQLite does not have a special timestamp column type. A QDateTime is
stored as string, formatted in Qt::ISODateWithMs and therefore the
QDateTime timezone information is preserved during insertion and
select.
\section3 Connection options
The Qt SQLite plugin honors the following connection options:
\table
\header \li Attribute \li Possible value
\row
\li QSQLITE_BUSY_TIMEOUT
\li Busy handler timeout in milliseconds (val <= 0: disabled),
{SQLite documentation} for more information
\row
\li QSQLITE_USE_QT_VFS
\li 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.
\row
\li QSQLITE_OPEN_READONLY
\li 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)
\row
\li QSQLITE_OPEN_URI
\li The given filename is interpreted as an uri, see
\row
\li QSQLITE_ENABLE_SHARED_CACHE
\li If set, the database is opened in
\l {https://www.sqlite.org/sharedcache.html} {shared cache mode},
otherwise in private cache mode
\row
\li QSQLITE_ENABLE_REGEXP
\li If set, the plugin defines a function 'regex' which can be used
in queries, QRegularExpression is used for evaluation of the regex query
\row
\li QSQLITE_NO_USE_EXTENDED_RESULT_CODES
\li Disables the usage of the \l {https://www.sqlite.org/c3ref/extended_result_codes.html}
{extended result code} feature in SQLite
\row
\li QSQLITE_ENABLE_NON_ASCII_CASE_FOLDING
\li If set, the plugin replaces the functions 'lower' and 'upper' with
QString functions for correct case folding of non-ascii characters
\row
\li QSQLITE_OPEN_NOFOLLOW
\li If set, the database filename is not allowed to contain a symbolic link
\endtable
\section3 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 \c{-DFEATURE_system_sqlite=OFF} parameter to the
\c qt-cmake command line.
If you do not want to use the SQLite library included with Qt, you
can pass \c{-DFEATURE_system_sqlite=ON} to the \c 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 \c $SQLITE with the directory where
SQLite resides):
\snippet code/doc_src_sql-driver.qdoc 21
On Windows (assuming that SQLite is installed in \c{C:\SQLITE}):
\snippet code/doc_src_sql-driver.qdoc 23
\section3 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 \l{QSqlDatabase::setConnectOptions()} {setting the connect
option} \c{QSQLITE_ENABLE_REGEXP} before \l{QSqlDatabase::open()} {the
database connection is opened}. Then a SQL statement like "column REGEXP
'pattern'" basically expands to the Qt code
\snippet code/doc_src_sql-driver.cpp 34
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 "\c{QSQLITE_ENABLE_REGEXP=10}" reduces the
cache size to 10.
\section3 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 \c{SQLITE_DEFAULT_FILE_FORMAT} define when building SQLite.
\target QMIMER
\section2 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 \l{https://developer.mimer.com/features/multilingual-support}
\section3 Timestamp Support
MimerSQL does not know anything about timezones and QDateTime
is used without honoring the timezone at all.
\note: This might change in the future.
\section3 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:
\snippet code/doc_src_sql-driver.qdoc 44
Source code to access the INOUT and OUT values:
\snippet code/doc_src_sql-driver.cpp 40
\section3 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 \l{https://developer.mimer.com}.
\snippet code/doc_src_sql-driver.qdoc 31
\section3 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 \l{https://developer.mimer.com}.
\snippet code/doc_src_sql-driver.qdoc 32
\target QIBASE
\section2 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.
\section3 Timestamp Support
Interbase stores timestamps in UTC without any timezone information.
Due to this, the QDateTime is used without honoring the timezone at all.
Since Firebird 4.0, the database supports timestamps with timezones. The
timezone information is stored separately to the timestamp so it can
be properly retrieved later on. See the Firebird
{documentation} for more information about timestamp handling.
\section3 Connection options
The Qt Borland InterBase plugin honors the following connection options:
\table
\header \li Attribute \li Possible value
\row
\li ISC_DPB_SQL_ROLE_NAME
\li Specifies the login role name
\endtable
\section3 How to Build the QIBASE Plugin
\snippet code/doc_src_sql-driver.cpp 24
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.
\section3 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:
\snippet code/doc_src_sql-driver.cpp 26
\section3 How to Build the QIBASE Plugin on Unix and \macos
The following assumes InterBase or Firebird is installed in
\c{/opt/interbase}:
If you are using InterBase:
\snippet code/doc_src_sql-driver.qdoc 27
Optionally, use the CMake variables \c{Interbase_INCLUDE_DIR} and
\c{Interbase_LIBRARY} to specify the include path and library directly.
\section3 How to Build the QIBASE Plugin on Windows
The following assumes InterBase or Firebird is installed in
\c{C:\interbase}:
If you are using InterBase:
\snippet code/doc_src_sql-driver.qdoc 29
Optionally, use the CMake variables \c{Interbase_INCLUDE_DIR} and
\c{Interbase_LIBRARY} to specify the include path and library directly.
Note that \c{C:\interbase\bin} must be in the \c PATH.
\target QFIREBIRD
\section2 QFIREBIRD for Firebird
The Qt Firebird plugin (driver key \c QFIREBIRD) accesses the
\l{https://firebirdsql.org/}{Firebird} database through Firebird's modern
object-oriented client API and requires Firebird 4.0 or later. It is
separate from the \l{#QIBASE}{QIBASE} driver, which uses the legacy
InterBase C API, and the two can be used side by side; QFIREBIRD reports
QSqlDriver::FirebirdSQL as its QSqlDriver::dbmsType().
The client API reports errors as C++ exceptions, so the driver is only
available when Qt is configured with exception support (\c{-exceptions};
Qt itself is built without exceptions by default).
Firebird is a client/server database: the server opens the database file, so
specify the database as \c{host:/path/to/database.fdb}, or set the host name
separately from the database name.
\section3 Supported Data Types
In addition to the standard SQL types, the driver maps the Firebird 4 types:
\table
\header \li Firebird type \li Qt type \li Notes
\row \li \c INT128 \li QString
\li Surfaced as an exact decimal string (Qt has no 128-bit integer type).
\row \li \c{DECFLOAT(16)} / \c{DECFLOAT(34)} \li QString
\li IEEE 754 decimal floating point, surfaced as an exact decimal string.
\row \li \c NUMERIC / \c DECIMAL \li policy-dependent
\li Honors \l{QSqlQuery::setNumericalPrecisionPolicy()}; values wider
than 64 bits are INT128-backed and surfaced as strings.
\row \li \c{TIMESTAMP WITH TIME ZONE} \li QDateTime \li The time zone is preserved.
\row \li \c{TIME WITH TIME ZONE} \li QDateTime
\li Surfaced as a QDateTime carrying the stored time and zone.
\row \li \c ARRAY \li QVariantList \li Read and write of array columns.
\endtable
\section3 Timestamp Support
A plain \c TIMESTAMP column carries no time zone; QDateTime values are stored
and retrieved without time-zone conversion. Since Firebird 4.0 the
\c{TIMESTAMP WITH TIME ZONE} and \c{TIME WITH TIME ZONE} types preserve the
time zone across a round trip.
\section3 Connection Options
The plugin honors the following options via QSqlDatabase::setConnectOptions():
\table
\header \li Option \li Value
\row \li ISC_DPB_SESSION_TIME_ZONE \li Session time zone, for example \c{Asia/Tokyo}.
\row \li ISC_DPB_SQL_ROLE_NAME \li Login role name.
\row \li ISC_DPB_CONNECT_TIMEOUT \li Connection timeout in seconds.
\row \li FB_SCROLLABLE_CACHE
\li Set to \c 1 to enable a client-side row cache for non-forward-only
queries. This makes QSqlQuery::size() available and gives
QSqlQuery::last() and QSqlQuery::seek() a true absolute row index, at
the cost of buffering the result set.
\endtable
\section3 Other Features
QFIREBIRD supports prepared queries, BLOBs, transactions, batch operations
(QSqlQuery::execBatch()), asynchronous event notifications
(QSqlDriver::subscribeToNotification()), and cross-thread cancellation of a
running statement (QSqlDriver::cancelQuery()).
\section3 How to Build the QFIREBIRD Plugin on Unix and \macos
The following assumes Firebird is installed in \c{/opt/firebird}:
\snippet code/doc_src_sql-driver.qdoc 45
\section3 How to Build the QFIREBIRD Plugin on Windows
The following assumes Firebird is installed in
\c{C:\Program Files\Firebird\Firebird_5_0}:
\snippet code/doc_src_sql-driver.qdoc 46
Optionally, use the CMake variables \c{Firebird_INCLUDE_DIR} and
\c{Firebird_LIBRARY} to specify the include path and library directly. The
Firebird client library (\c fbclient) must be in the \c PATH at run time.
\target troubleshooting
\section1 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:
\list 1
\li Ensure that the plugin is in the correct directory. You can use
QApplication::libraryPaths() to determine where Qt looks for plugins.
\li Ensure that the client libraries of the DBMS are available on the
system. On Unix, run the command \c{ldd} and pass the name of the
plugin as parameter, for example \c{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
to find out the dependent libraries. With
Qt Creator, you can update the \c PATH environment variable in the
\gui Run section of the \gui Project panel to include the path to
the folder containing the client libraries.
\li 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.
\li Run the compiled Qt executable with the environment variable
\l{Environment Variables Recognized by Qt}{QT_DEBUG_PLUGINS} set
to get very verbose debug output when loading plugins.
\li To retrieve possible debug messages from the SQL subsystem, enable
the output by setting the environment variable \c{QT_LOGGING_RULES}
to \c{qt.sql.*.debug=true}. Don't forget to enable the
\l{Environment Variables Recognized by Qt}{console} when working on
windows.
See \l{Logging Rules} for a more detailed explanation on how to set
logging rules.
\endlist
Make sure you have followed the guide to \l{Deploying Plugins}.
\target development
\section1 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 \c{SELECT},
\c{UPDATE}, and \c{ALTER} \c{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 \l{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
\c{QTDIR/qtbase/src/plugins/sqldrivers}.
The following code can be used as a skeleton for a SQL driver:
\snippet sqldatabase/sqldatabase.cpp 47
\codeline
\snippet sqldatabase/sqldatabase.cpp 48
*/