SQL Database Drivers▲
The Qt SQL module uses driver plugins to communicate with the different database APIs. Since Qt's SQL Module API is database-independent, all database-specific code is contained within these drivers. Several drivers are supplied with Qt, and other drivers can be added. The driver source code is supplied and can be used as a model for writing your own drivers.
Supported Databases▲
The table below lists the drivers included with Qt:
Driver name |
DBMS |
---|---|
IBM DB2 (version 7.1 and above) |
|
Borland InterBase |
|
MySQL |
|
Oracle Call Interface Driver |
|
Open Database Connectivity (ODBC) - Microsoft SQL Server and other ODBC-compliant databases |
|
PostgreSQL (versions 7.3 and above) |
|
SQLite version 2 |
|
SQLite version 3 |
|
Sybase Adaptive Server obsolete from Qt 4.7 |
SQLite is the in-process database system with the best test coverage and support on all platforms. Oracle via OCI, PostgreSQL, and MySQL through either ODBC or a native driver are well-tested on Windows and Linux. The completeness of the support for other systems depends on the availability and quality of client libraries.
Note: To build a driver plugin you need to have the appropriate client library for your Database Management System (DBMS). This provides access to the API exposed by the DBMS, and is typically shipped with it. Most installation programs also allow you to install "development libraries", and these are what you need. These libraries are responsible for the low-level communication with the DBMS.
When using Qt under Open Source terms but with a proprietary database, verify the client library's license compatibility with the LGPL.
Building the Drivers▲
The Qt configure script tries to automatically detect the available client libraries on your machine. Run configure -help to see what drivers can be built. You should get an output similar to this:
[...]
Database options:
-
sql-&
lt;driver&
gt; ........ Enable SQL &
lt;driver&
gt; plugin. Supported drivers:
db2 ibase mysql oci odbc psql sqlite2 sqlite tds
[all auto
]
-
sqlite .............. Select used sqlite3 [system/
qt]
[...]
The configure script cannot detect the necessary libraries and include files if they are not in the standard paths, so it may be necessary to specify these paths using the *_INCDIR=, *_LIBDIR=, or *_PREFIX= command-line options. For example, if your MySQL files are installed in /usr/local/mysql (or in C:\mysql on Windows), then pass the following parameter to configure: MYSQL_PREFIX=/usr/local/mysql (or MYSQL_PREFIX=C:\mysql for Windows). The particulars for each driver are explained below.
Due to the practicalities of dealing with external dependencies, only the SQLite3 plugin is shipped with binary builds of Qt. To be able to add additional drivers to the Qt installation without re-building all of Qt, it is possible to configure and build the qtbase/src/plugins/sqldrivers directory outside of a full Qt build directory. Note that it is not possible to configure each driver separately, only all of them at once. Drivers can be built separately, though. If the Qt build is configured with -prefix, it is necessary to install the plugins after building them, too. For example:
cd $QTDIR/
qtbase/
src/
plugins/
sqldrivers/
mysql
make install
Driver Specifics▲
QMYSQL for MySQL 4 and higher▲
QMYSQL Stored Procedure Support▲
MySQL 5 introduces 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"
);
q.next();
qDebug() &
lt;&
lt; q.value(0
) &
lt;&
lt; q.value(1
); // outputs "42" and "43"
Note: @outval1 and @outval2 are variables local to the current connection and will not be affected by queries sent from another host or connection.
Embedded MySQL Server▲
The MySQL embedded server is a drop-in replacement for the normal client library. With the embedded MySQL server, a MySQL server is not required to use MySQL functionality.
To use the embedded MySQL server, simply link the Qt plugin to libmysqld instead of libmysqlclient. This can be done by adding MYSQL_LIBS=-lmysqld to the configure command line.
Please refer to the MySQL documentation, chapter "libmysqld, the Embedded MySQL Server Library" for more information about the MySQL embedded server.
How to Build the QMYSQL Plugin on Unix and macOS▲
You need the MySQL header files, as well as the shared library libmysqlclient.so. Depending on your Linux distribution, you may need to install a package which is usually called "mysql-devel".
Tell qmake where to find the MySQL header files and shared libraries (here it is assumed that MySQL is installed in /usr/local) and run make:
cd $QTDIR/
qtbase/
src/
plugins/
sqldrivers
qmake --
MYSQL_PREFIX=/
usr/
local
make sub-
mysql
How to Build the QMYSQL Plugin on Windows▲
You need to get the MySQL installation files. Run SETUP.EXE and choose "Custom Install". Install the "Libs & Include Files" Module. Build the plugin as follows (here it is assumed that MySQL is installed in C:\MySQL):
cd %
QTDIR%
\qtbase\src\plugins\sqldrivers
qmake --
MYSQL_INCDIR=
C:/
MySQL/
include "MYSQL_LIBDIR=C:/MYSQL/MySQL Server <version>/lib/opt"
nmake sub-
mysql
If you are not using a Microsoft compiler, replace nmake with mingw32-make in the line above.
QOCI for the Oracle Call Interface (OCI)▲
The Qt OCI plugin supports Oracle 9i, 10g and higher. After connecting to the Oracle server, the plugin will auto-detect the database version and enable features accordingly.
It's possible to connect to a Oracle database without a tnsnames.ora file. This requires that the database SID is passed to the driver as the database name, and that a hostname is given.
OCI User Authentication▲
The Qt OCI plugin supports authentication using external credentials (OCI_CRED_EXT). Usually, this means that the database server will use the user authentication provided by the operating system instead of its own authentication mechanism.
Leave the username and password empty when opening a connection with QSqlDatabase to use the external credentials authentication.
OCI BLOB/LOB Support▲
Binary Large Objects (BLOBs) can be read and written, but be aware that this process may require a lot of memory. You should use a forward only query to select LOB fields (see QSqlQuery::setForwardOnly()).
Inserting BLOBs should be done using either a prepared query where the BLOBs are bound to placeholders or QSqlTableModel, which uses a prepared query to do this internally.
How to Build the OCI Plugin on Unix and macOS▲
For Oracle 10g, all you need is the "Instant Client Package - Basic" and "Instant Client Package - SDK". For Oracle prior to 10g, you require the standard Oracle client and the SDK packages.
Oracle library files required to build the driver:
-
libclntsh.so (all versions)
-
libwtc9.so (only Oracle 9)
Tell qmake where to find the Oracle header files and shared libraries and run make:
For Oracle version 9:
cd $QTDIR/
qtbase/
src/
plugins/
sqldrivers
qmake --
"OCI_INCDIR=$ORACLE_HOME/rdbms/public"
OCI_LIBDIR=
$ORACLE_HOME/
lib "OCI_LIBS=-lclntsh -lwtc9"
make sub-
oci
For Oracle version 10, we assume that you installed the RPM packages of the Instant Client Package SDK (you need to adjust the version number accordingly):
cd $QTDIR/
qtbase/
src/
plugins/
sqldrivers
qmake --
OCI_INCDIR=/
usr/
include/
oracle/
10.1.0.3
/
client OCI_LIBDIR=/
usr/
lib/
oracle/
10.1.0.3
/
client/
lib
make sub-
oci
Note: If you are using the Oracle Instant Client package, you will need to set LD_LIBRARY_PATH when building the OCI SQL plugin, and when running an application that uses the OCI SQL plugin. You can avoid this requirement by setting RPATH, and listing all of the libraries to link to. Here is an example:
configure OCI_INCDIR=/
usr/
include/
oracle/
10.1.0.3
/
client OCI_LIBDIR=/
usr/
lib/
oracle/
10.1.0.3
/
client