Qt 6.x
The Qt SDK
Loading...
Searching...
No Matches
sql-driver.qdoc
Go to the documentation of this file.
1// Copyright (C) 2020 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GFDL-1.3-no-invariants-only
3
4/*!
5 \page sql-driver.html
6 \title SQL Database Drivers
7 \brief How to configure and install Qt SQL drivers for supported databases.
8
9 The Qt SQL module uses driver \l{How to Create Qt
10 Plugins}{plugins} to communicate with the different database
11 APIs. Since Qt's SQL Module API is database-independent, all
12 database-specific code is contained within these drivers. Several
13 drivers are supplied with Qt, and other drivers can be added. The
14 driver source code is supplied and can be used as a model for
15 \l{#development}{writing your own drivers}.
16
17 \tableofcontents
18
19 \section1 Supported Databases
20
21 The table below lists the drivers included with Qt:
22
23 \table
24 \header \li Driver name \li DBMS
25 \row \li \l{#QDB2}{QDB2} \li IBM DB2 (version 7.1 and above)
26 \row \li \l{#QIBASE} \li Borland InterBase / Firebird
27 \row \li \l{#QMYSQL}{QMYSQL / MARIADB} \li MySQL or MariaDB (version 5.6 and above)
28 \row \li \l{#QOCI}{QOCI} \li Oracle Call Interface Driver (version 12.1 and above)
29 \row \li \l{#QODBC}{QODBC}
30 \li Open Database Connectivity (ODBC) - Microsoft SQL Server and other
31 ODBC-compliant databases
32 \row \li \l{#QPSQL}{QPSQL} \li PostgreSQL (versions 7.3 and above)
33 \row \li \l{#QSQLITE}{QSQLITE} \li SQLite version 3
34 \row \li \l{#QMIMER}{QMIMER} \li Mimer SQL (version 11 and above)
35 \endtable
36
37 SQLite is the in-process database system with the best test coverage
38 and support on all platforms. Oracle via OCI, PostgreSQL, and MySQL
39 through either ODBC or a native driver are well-tested on Windows and
40 Linux. The completeness of the support for other systems depends on the
41 availability and quality of client libraries.
42
43 \note To build a driver plugin you need to have the appropriate
44 client library for your Database Management System (DBMS). This provides
45 access to the API exposed by the DBMS, and is typically shipped with it.
46 Most installation programs also allow you to install "development
47 libraries", and these are what you need. These libraries are responsible
48 for the low-level communication with the DBMS. Also make sure to install
49 the correct database libraries for your Qt architecture (32 or 64 bit).
50
51 \note When using Qt under Open Source terms but with a proprietary
52 database, verify the client library's license compatibility with
53 the LGPL.
54
55 \target building
56 \section1 Building the Drivers
57 \target DriverWithQt
58 \section2 Compile Qt with a specific driver
59
60 The Qt \c configure script tries to
61 automatically detect the available client libraries on your
62 machine. Run \c{configure -help} to see what drivers can be
63 built. You should get an output similar to this:
64
65 \snippet code/doc_src_sql-driver.qdoc 0
66
67 The \c configure script cannot detect the necessary libraries
68 and include files if they are not in the standard paths, so it
69 may be necessary to specify these paths using either
70 driver-specific include and library path variables or \c CMAKE_INCLUDE_PATH
71 and \c CMAKE_LIBRARY_PATH. For example, if your MySQL files are
72 installed in \c{C:\mysql-connector-c-6.1.11-winx64} on Windows, then pass
73 the following parameter to double-dash part of configure line:
74 \snippet code/doc_src_sql-driver.qdoc 42
75 When you configure drivers in the manner described above, CMake skips any
76 dependency checks and uses the provided paths as is. This is especially
77 useful if the package provides its own set of system libraries that
78 should not be recognized by the build routine.
79
80 In some cases it's more convenient to use \c CMAKE_INCLUDE_PATH
81 and \c CMAKE_LIBRARY_PATH variables to locate required libraries.
82 You should prefer this method if module needs to set properties
83 for the provided target libraries (e.g. this is required for PostgreSQL
84 and SQLite).
85 For example, you can do this as follows, to locate MySQL:
86 \snippet code/doc_src_sql-driver.qdoc 43
87
88 The particulars for each driver are explained below.
89
90 \note If something goes wrong and you want CMake to recheck your
91 available drivers, you might need to remove \e{CMakeCache.txt} from the build
92 directory.
93
94 \target DriverStandalone
95 \section2 Compile only a specific sql driver
96
97 A typical \c qt-cmake run (in this case to configure for MySQL) looks like this:
98
99 \snippet code/doc_src_sql-driver.qdoc 41
100 \note As mentioned in \l{#DriverWithQt}{Compile Qt with a specific driver},
101 if the driver could not be found or is not enabled, start over by removing
102 \e{CMakeCache.txt}.
103
104 Due to the practicalities of dealing with external dependencies,
105 only the SQLite plugin is shipped with binary builds of Qt.
106 Binary builds of Qt for Windows also include the ODBC plugin.
107 To be able to add additional drivers to the Qt installation
108 without re-building all of Qt, it is possible to configure
109 and build the \c qtbase/src/plugins/sqldrivers directory outside
110 of a full Qt build directory. Note that it is not possible to
111 \e configure each driver separately, only all of them at once.
112 Drivers can be \e built separately, though.
113
114 \note You need to specify \c{CMAKE_INSTALL_PREFIX}, if you want to install
115 plugins after the build is finished.
116
117 \section1 Driver Specifics
118
119 \target QMYSQL
120 \section2 QMYSQL for MySQL or MariaDB 5.6 and higher
121
122 MariaDB is a fork of MySQL intended to remain free and open-source software
123 under the GNU General Public License. MariaDB intended to maintain high
124 compatibility with MySQL, ensuring a drop-in replacement capability with
125 library binary parity and exact matching with MySQL APIs and commands.
126 Therefore the plugin for MySQL and MariaDB are combined into one Qt plugin.
127
128 \section3 QMYSQL Stored Procedure Support
129
130 MySQL has stored procedure support at the SQL level, but no
131 API to control IN, OUT, and INOUT parameters. Therefore, parameters
132 have to be set and read using SQL commands instead of QSqlQuery::bindValue().
133
134 Example stored procedure:
135
136 \snippet code/doc_src_sql-driver.qdoc 1
137
138 Source code to access the OUT values:
139
140 \snippet code/doc_src_sql-driver.cpp 2
141
142 \note \c{@outval1} and \c{@outval2} are variables local to the current
143 connection and will not be affected by queries sent from another host
144 or connection.
145
146 \section3 Embedded MySQL Server
147
148 The MySQL embedded server is a drop-in replacement for the normal
149 client library. With the embedded MySQL server, a MySQL server is
150 not required to use MySQL functionality.
151
152 To use the embedded MySQL server, simply link the Qt plugin to \c
153 libmysqld instead of \c libmysqlclient. This can be done by adding
154 \c{-DMySQL_LIBRARY=<path/to/mysqld/>libmysqld.<so|lib|dylib>} to the configure command line.
155
156 Please refer to the MySQL documentation, chapter "libmysqld, the Embedded
157 MySQL Server Library" for more information about the MySQL embedded server.
158
159 \section3 Connection options
160 The Qt MySQL/MariaDB plugin honors the following connection options:
161 \table
162 \header \li Attribute \li Possible value
163 \row
164 \li CLIENT_COMPRESS
165 \li If set, switches to compressed protocol after successful authentication
166 \row
167 \li CLIENT_FOUND_ROWS
168 \li If set, send found rows instead of affected rows
169 \row
170 \li CLIENT_IGNORE_SPACE
171 \li If set, ignore spaces before '('
172 \row
173 \li CLIENT_NO_SCHEMA
174 \li If set, don't allow database.table.column
175 \row
176 \li CLIENT_INTERACTIVE
177 \li If set, client is treated as interactive
178 \row
179 \li MYSQL_OPT_PROTOCOL
180 \li explicitly specify the protocol to use:\br
181 MYSQL_PROTOCOL_TCP: use tcp connection (ip/hostname specified through setHostname())
182 MYSQL_PROTOCOL_SOCKET: connect through a socket specified in UNIX_SOCKET
183 MYSQL_PROTOCOL_PIPE: connect through a named pipe specified in UNIX_SOCKET
184 MYSQL_PROTOCOL_MEMORY: connect through shared memory specified in MYSQL_SHARED_MEMORY_BASE_NAME
185 \row
186 \li UNIX_SOCKET
187 \li Specifies the socket or named pipe to use, even it's called UNIX_SOCKET it
188 can also be used on windows
189 \row
190 \li MYSQL_SHARED_MEMORY_BASE_NAME
191 \li Specified the shared memory segment name to use
192 \row
193 \li MYSQL_OPT_RECONNECT
194 \li TRUE or 1: Automatically reconnect after connection loss\br
195 FALSE or 0: No automatic reconnect after connection loss (default)\br
196 See \l {https://dev.mysql.com/doc/c-api/8.0/en/c-api-auto-reconnect.html}
197 {Automatic Reconnection Control}
198 \row
199 \li MYSQL_OPT_CONNECT_TIMEOUT
200 \li The connect timeout in seconds
201 \row
202 \li MYSQL_OPT_READ_TIMEOUT
203 \li The timeout in seconds for each attempt to read from the server
204 \row
205 \li MYSQL_OPT_WRITE_TIMEOUT
206 \li The timeout in seconds for each attempt to write to the server
207 \row
208 \li MYSQL_OPT_LOCAL_INFILE
209 \li Set to 1 to enable the support for local
210 \l {https://dev.mysql.com/doc/refman/8.0/en/load-data.html} {LOAD_DATA},
211 disabled if not set or 0
212 \row
213 \li MYSQL_OPT_SSL_MODE
214 \li The security state to use for the connection to the server: SSL_MODE_DISABLED,
215 SSL_MODE_PREFERRED, SSL_MODE_REQUIRED, SSL_MODE_VERIFY_CA, SSL_MODE_VERIFY_IDENTITY.
216 \row
217 \li MYSQL_OPT_TLS_VERSION
218 \li A list of protocols the client permits for encrypted connections. The value can be
219 a combination of 'TLSv1' ,' TLSv1.1', 'TLSv1.2' or 'TLSv1.3' depending on the used \l
220 {https://dev.mysql.com/doc/refman/8.0/en/encrypted-connection-protocols-ciphers.html#encrypted-connection-protocol-configuration}
221 {MySQL server} version.
222 \row
223 \li MYSQL_OPT_SSL_KEY / SSL_KEY (deprecated)
224 \li The path name of the client private key file
225 \row
226 \li MYSQL_OPT_SSL_CERT / SSL_CERT (deprecated)
227 \li The path name of the client public key certificate file
228 \row
229 \li MYSQL_OPT_SSL_CA / SSL_CA (deprecated)
230 \li The path name of the Certificate Authority (CA) certificate file
231 \row
232 \li MYSQL_OPT_SSL_CAPATH / SSL_CAPATH (deprecated)
233 \li The path name of the directory that contains trusted SSL CA certificate files
234 \row
235 \li MYSQL_OPT_SSL_CIPHER / SSL_CIPHER (deprecated)
236 \li The list of permissible ciphers for SSL encryption
237 \row
238 \li MYSQL_OPT_SSL_CRL
239 \li The path name of the file containing certificate revocation lists
240 \row
241 \li MYSQL_OPT_SSL_CRLPATH
242 \li The path name of the directory that contains files containing certificate revocation lists
243 \endtable
244 For more detailed information about the connect options please refer
245 to the \l {https://dev.mysql.com/doc/c-api/8.0/en/mysql-options.html}
246 {mysql_options()} MySQL documentation.
247
248
249 \section3 How to Build the QMYSQL Plugin on Unix and \macos
250
251 You need the MySQL / MariaDB header files, as well as the shared library
252 \c{libmysqlclient.<so|dylib>} / \c{libmariadb.<so|dylib>}. Depending on your Linux distribution,
253 you may need to install a package which is usually called "mysql-devel"
254 or "mariadb-devel".
255
256 Tell \c qt-cmake where to find the MySQL / MariaDB header files and shared
257 libraries (here it is assumed that MySQL / MariaDB is installed in
258 \c{/usr/local}) and build:
259
260 \snippet code/doc_src_sql-driver.qdoc 3
261
262 \section3 How to Build the QMYSQL Plugin on Windows
263
264 You need to get the MySQL installation files (e.g.
265 \l {https://dev.mysql.com/downloads/installer/}{MySQL web installer} or
266 \l {https://downloads.mariadb.com/Connectors/c/}{MariaDB C Connector}).
267 Run the installer,
268 select custom installation and install the MySQL C Connector
269 which matches your Qt installation (x86 or x64).
270 After installation check that the needed files are there:
271 \list
272 \li \c {<MySQL dir>/lib/libmysql.lib}
273 \li \c {<MySQL dir>/lib/libmysql.dll}
274 \li \c {<MySQL dir>/include/mysql.h}
275 \endlist
276 and for MariaDB
277 \list
278 \li \c {<MariaDB dir>/lib/libmariadb.lib}
279 \li \c {<MariaDB dir>/lib/libmariadb.dll}
280 \li \c {<MariaDB dir>/include/mysql.h}
281 \endlist
282
283 \note As of MySQL 8.0.19, the C Connector is no longer offered as a standalone
284 installable component. Instead, you can get \c{mysql.h} and \c{libmysql.*} by
285 installing the full MySQL Server (x64 only) or the
286 \l{https://downloads.mariadb.org/connector-c/}{MariaDB C Connector}.
287
288 Build the plugin as follows (here it is assumed that \c{<MySQL dir>} is
289 \c{C:\mysql-8.0.22-winx64}):
290
291 \snippet code/doc_src_sql-driver.qdoc 5
292
293 When you distribute your application, remember to include \e libmysql.dll / \e libmariadb.dll
294 in your installation package. It must be placed in the same folder
295 as the application executable. \e libmysql.dll additionally needs the
296 MSVC runtime libraries which can be installed with
297 \l {https://support.microsoft.com/en-us/help/2977003/the-latest-supported-visual-c-downloads}{vcredist.exe}
298
299 \target QOCI
300 \section2 QOCI for the Oracle Call Interface (OCI)
301
302 The Qt OCI plugin supports connecting to Oracle database as determined by
303 the version of the instant client used. This is dependent on what Oracle
304 indicates it supports. The plugin will auto-detect the database version
305 and enable features accordingly.
306
307 It's possible to connect to a Oracle database without a tnsnames.ora file.
308 This requires that the database SID is passed to the driver as the database
309 name, and that a hostname is given.
310
311 \section3 OCI User Authentication
312
313 The Qt OCI plugin supports authentication using
314 external credentials (OCI_CRED_EXT). Usually, this means that the database
315 server will use the user authentication provided by the operating system
316 instead of its own authentication mechanism.
317
318 Leave the username and password empty when opening a connection with
319 QSqlDatabase to use the external credentials authentication.
320
321 \section3 OCI BLOB/LOB Support
322
323 Binary Large Objects (BLOBs) can be read and written, but be aware
324 that this process may require a lot of memory. You should use a forward
325 only query to select LOB fields (see QSqlQuery::setForwardOnly()).
326
327 Inserting BLOBs should be done using either a prepared query where the
328 BLOBs are bound to placeholders or QSqlTableModel, which uses a prepared
329 query to do this internally.
330
331 \section3 Connection options
332 The Qt OCI plugin honors the following connection options:
333 \table
334 \header \li Attribute \li Possible value
335 \row
336 \li OCI_ATTR_PREFETCH_ROWS
337 \li Sets the OCI attribute
338 \l {https://docs.oracle.com/database/121/LNOCI/oci04sql.htm#LNOCI16355}
339 {OCI_ATTR_PREFETCH_ROWS} to the specified value
340 \row
341 \li OCI_ATTR_PREFETCH_MEMORY
342 \li Sets the OCI attribute
343 \l {https://docs.oracle.com/database/121/LNOCI/oci04sql.htm#LNOCI16355}
344 {OCI_ATTR_PREFETCH_MEMORY} to the specified value
345 \row
346 \li OCI_AUTH_MODE
347 \li OCI_SYSDBA: authenticate for SYSDBA access\br
348 OCI_SYSOPER: authenticate for SYSOPER access\br
349 OCI_DEFAULT: authenticate with normal access\br
350 see \l {https://docs.oracle.com/cd/B10500_01/appdev.920/a96584/oci15r13.htm}
351 {OCISessionBegin} for more information about the access modes
352 \endtable
353
354 \section3 How to Build the OCI Plugin on Unix and \macos
355
356 All you need is the " - Basic" and "Instant Client
357 Package - SDK".
358
359 Oracle library files required to build the driver:
360
361 \list
362 \li \c libclntsh.<so|dylib> (all versions)
363 \endlist
364
365 Tell \c qt-cmake where to find the Oracle header files and shared
366 libraries and build.
367
368 We assume that you installed the RPM packages of the Instant Client Package SDK
369 (you need to adjust the version number accordingly):
370 \snippet code/doc_src_sql-driver.qdoc 7
371
372 \note If you are using the Oracle Instant Client package,
373 you will need to set LD_LIBRARY_PATH when building the OCI SQL plugin,
374 and when running an application that uses the OCI SQL plugin.
375
376 \section3 How to Build the OCI Plugin on Windows
377
378 Choosing the option "Programmer" in the Oracle Client Installer from
379 the Oracle Client Installation CD is generally sufficient to build the
380 plugin. For some versions of Oracle Client, you may also need to select
381 the "Call Interface (OCI)" option if it is available.
382
383 Build the plugin as follows (here it is assumed that Oracle Client is
384 installed in \c{C:\oracle} and SDK is installed in \c{C:\oracle\sdk}):
385
386 \snippet code/doc_src_sql-driver.qdoc 8
387
388 When you run your application, you will also need to add the \c oci.dll
389 path to your \c PATH environment variable:
390
391 \snippet code/doc_src_sql-driver.qdoc 9
392
393 \target QODBC
394 \section2 QODBC for Open Database Connectivity (ODBC)
395
396 ODBC is a general interface that allows you to connect to multiple
397 DBMSs using a common interface. The QODBC driver allows you to connect
398 to an ODBC driver manager and access the available data sources. Note
399 that you also need to install and configure ODBC drivers for the ODBC
400 driver manager that is installed on your system. The QODBC plugin
401 then allows you to use these data sources in your Qt applications.
402
403 \note You should use the native driver, if it is available, instead
404 of the ODBC driver. ODBC support can be used as a fallback for compliant
405 databases if no native driver is available.
406
407 On Windows, an ODBC driver manager should be installed by default.
408 For Unix systems, there are some implementations which must be
409 installed first. Note that every end user of your application is
410 required to have an ODBC driver manager installed, otherwise the
411 QODBC plugin will not work.
412
413 When connecting to an ODBC datasource, you should pass the name
414 of the ODBC datasource to the QSqlDatabase::setDatabaseName()
415 function, rather than the actual database name.
416
417 The QODBC Plugin needs an ODBC compliant driver manager version 2.0 or
418 later. Some ODBC drivers claim to be version-2.0-compliant,
419 but do not offer all the necessary functionality. The QODBC plugin
420 therefore checks whether the data source can be used after a
421 connection has been established, and refuses to work if the check
422 fails. If you do not like this behavior, you can remove the \c{#define
423 ODBC_CHECK_DRIVER} line from the file \c{qsql_odbc.cpp}. Do this at
424 your own risk!
425
426 By default, Qt instructs the ODBC driver to behave as an ODBC 2.x
427 driver. However, for some \e{driver-manager/ODBC 3.x-driver}
428 combinations (e.g., \e{unixODBC/MaxDB ODBC}), telling the ODBC
429 driver to behave as a 2.x driver can cause the driver plugin to
430 have unexpected behavior. To avoid this problem, instruct the ODBC
431 driver to behave as a 3.x driver by
432 \l{QSqlDatabase::setConnectOptions()} {setting the connect option}
433 \c{"SQL_ATTR_ODBC_VERSION=SQL_OV_ODBC3"} before you
434 \l{QSqlDatabase::open()} {open your database connection}. Note
435 that this will affect multiple aspects of ODBC driver behavior,
436 e.g., the SQLSTATEs. Before setting this connect option, consult
437 your ODBC documentation about behavior differences you can expect.
438
439 If you experience very slow access of the ODBC datasource, make sure
440 that ODBC call tracing is turned off in the ODBC datasource manager.
441
442 Some drivers do not support scrollable cursors. In that case, only
443 queries in \l QSqlQuery::setForwardOnly() mode can be used successfully.
444
445 \section3 ODBC Stored Procedure Support
446
447 With Microsoft SQL Server the result set returned by a stored
448 procedure that uses the return statement, or returns multiple result
449 sets, will be accessible only if you set the query's forward only
450 mode to \e forward using \l QSqlQuery::setForwardOnly().
451
452 \snippet code/doc_src_sql-driver.cpp 10
453
454 \note The value returned by the stored procedure's return statement
455 is discarded.
456
457 \section3 ODBC Unicode Support
458
459 The QODBC Plugin will use the Unicode API if UNICODE is defined. On
460 Windows based systems, this is the default. Note that the ODBC
461 driver and the DBMS must also support Unicode.
462
463 For the Oracle 9 ODBC driver (Windows), it is necessary to check
464 "SQL_WCHAR support" in the ODBC driver manager otherwise Oracle
465 will convert all Unicode strings to local 8-bit representation.
466
467 \section3 Connection options
468 The Qt ODBC plugin honors the following connection options:
469 \table
470 \header \li Attribute \li Possible value
471 \row
472 \li SQL_ATTR_ACCESS_MODE
473 \li SQL_MODE_READ_ONLY: open the database in read-only mode\br
474 SQL_MODE_READ_WRITE: open the database in read-write mode (default)
475 \row
476 \li SQL_ATTR_LOGIN_TIMEOUT
477 \li Number of seconds to wait for the database connection
478 during login (a value of 0 will wait forever)
479 \row
480 \li SQL_ATTR_CONNECTION_TIMEOUT
481 \li Number of seconds to wait for any request to the database
482 (a value of 0 will wait forever)
483 \row
484 \li SQL_ATTR_CURRENT_CATALOG
485 \li The catalog (database) to use for this connection
486 \row
487 \li SQL_ATTR_METADATA_ID
488 \li SQL_TRUE: the string argument of catalog functions are treated
489 as identifiers\br
490 SQL_FALSE: the string arguments of catalog functions are not
491 treated as identifiers
492 \row
493 \li SQL_ATTR_PACKET_SIZE
494 \li Specifies the network packet size in bytes
495 \row
496 \li SQL_ATTR_TRACEFILE
497 \li A string containing the name of the trace file
498 \row
499 \li SQL_ATTR_TRACE
500 \li SQL_OPT_TRACE_ON: Enable database query tracing\br
501 SQL_OPT_TRACE_OFF: Disable database query tracing (default)
502 \row
503 \li SQL_ATTR_CONNECTION_POOLING
504 \li Enable or disable connection pooling at the environment level.\br
505 SQL_CP_DEFAULT, SQL_CP_OFF: Connection pooling is turned off (default)\br
506 SQL_CP_ONE_PER_DRIVER: A single connection pool is supported for each driver\br
507 SQL_CP_ONE_PER_HENV: A single connection pool is supported for each environment
508 \row
509 \li SQL_ATTR_ODBC_VERSION
510 \li SQL_OV_ODBC3: The driver should act as a ODBC 3.x driver\br
511 SQL_OV_ODBC2: The driver should act as a ODBC 2.x driver (default)
512 \endtable
513 For more detailed information about the connect options please refer
514 to the \l {https://learn.microsoft.com/en-us/sql/odbc/reference/syntax/sqlsetconnectattr-function}
515 {SQLSetConnectAttr()} ODBC documentation.
516
517 \section3 How to Build the ODBC Plugin on Unix and \macos
518
519 It is recommended that you use unixODBC. You can find the latest
520 version and ODBC drivers at \l http://www.unixodbc.org.
521 You need the unixODBC header files and shared libraries.
522
523 Tell \c qt-cmake where to find the unixODBC header files and shared
524 libraries (here it is assumed that unixODBC is installed in
525 \c{/usr/local/unixODBC}) and build:
526
527 \snippet code/doc_src_sql-driver.qdoc 11
528
529 \section3 How to Build the ODBC Plugin on Windows
530
531 The ODBC header and include files should already be installed in the
532 right directories. You just have to build the plugin as follows:
533
534 \snippet code/doc_src_sql-driver.qdoc 12
535
536 \target QPSQL
537 \section2 QPSQL for PostgreSQL (Version 7.3 and above)
538
539 The QPSQL driver supports version 7.3 and higher of the PostgreSQL server.
540
541 For more information about PostgreSQL visit \l http://www.postgresql.org.
542
543 \section3 QPSQL Unicode Support
544
545 The QPSQL driver automatically detects whether the PostgreSQL
546 database you are connecting to supports Unicode or not. Unicode is
547 automatically used if the server supports it. Note that the driver
548 only supports the UTF-8 encoding. If your database uses any other
549 encoding, the server must be compiled with Unicode conversion
550 support.
551
552 Unicode support was introduced in PostgreSQL version 7.1 and it will
553 only work if both the server and the client library have been compiled
554 with multibyte support. More information about how to set up a
555 multibyte enabled PostgreSQL server can be found in the PostgreSQL
556 Administrator Guide, Chapter 5.
557
558 \section3 QPSQL Case Sensitivity
559
560 PostgreSQL databases will only respect case sensitivity if the table or field
561 name is quoted when the table is created. So for example, a SQL query such
562 as:
563
564 \snippet code/doc_src_sql-driver.qdoc 39
565
566 will ensure that it can be accessed with the same case that was used. If the
567 table or field name is not quoted when created, the actual table name
568 or field name will be lower-case. When QSqlDatabase::record() or
569 QSqlDatabase::primaryIndex() access a table or field that was unquoted
570 when created, the name passed to the function must be lower-case to
571 ensure it is found. For example:
572
573 \snippet code/doc_src_sql-driver.qdoc 40
574
575 \section3 QPSQL Forward-only query support
576
577 To use forward-only queries, you must build the QPSQL plugin with
578 PostreSQL client library version 9.2 or later. If the plugin is
579 built with an older version, then forward-only mode will not be
580 available - calling QSqlQuery::setForwardOnly() with \c true will
581 have no effect.
582
583 \warning If you build the QPSQL plugin with PostgreSQL version 9.2 or later,
584 then you must distribute your application with libpq version 9.2 or later.
585 Otherwise, loading the QPSQL plugin will fail with the following message:
586
587 \snippet code/doc_src_sql-driver.qdoc 35
588
589 While navigating the results in forward-only mode, the handle of
590 QSqlResult may change. Applications that use the low-level handle of
591 SQL result must get a new handle after each call to any of QSqlResult
592 fetch functions. Example:
593
594 \snippet code/doc_src_sql-driver_snippet.cpp 36
595
596 While reading the results of a forward-only query with PostgreSQL,
597 the database connection cannot be used to execute other queries.
598 This is a limitation of libpq library. Example:
599
600 \snippet code/doc_src_sql-driver.cpp 37
601
602 This problem will not occur if query1 and query2 use different
603 database connections, or if we execute query2 after the while loop.
604
605 \note Some methods of QSqlDatabase like tables(), primaryIndex()
606 implicitly execute SQL queries, so these also cannot be used while
607 navigating the results of forward-only query.
608
609 \note QPSQL will print the following warning if it detects a loss of
610 query results:
611
612 \snippet code/doc_src_sql-driver.qdoc 38
613
614 \section3 Connection options
615 The Qt PostgreSQL plugin honors all connection options specified in the
616 \l {https://www.postgresql.org/docs/current/libpq-connect.html#LIBPQ-PARAMKEYWORDS}
617 {connect()} PostgreSQL documentation.
618
619 \section3 How to Build the QPSQL Plugin on Unix and \macos
620
621 You need the PostgreSQL client library and headers installed.
622
623 To make \c qt-cmake find the PostgreSQL header files and shared
624 libraries, build the plugin the following way (assuming that the
625 PostgreSQL client is installed in \c{/usr/local/pgsql}):
626
627 \snippet code/doc_src_sql-driver.qdoc 13
628
629 \section3 How to Build the QPSQL Plugin on Windows
630
631 Install the appropriate PostgreSQL developer libraries for your
632 compiler. Assuming that PostgreSQL was installed in \c{C:\pgsql},
633 build the plugin as follows:
634
635 \snippet code/doc_src_sql-driver.qdoc 15
636
637 Users of MinGW may wish to consult the following online document:
638 \l{http://www.postgresql.org/docs/current/static/installation-platform-notes.html#INSTALLATION-NOTES-MINGW}{PostgreSQL MinGW/Native Windows}.
639
640 When you distribute your application, remember to include libpq.dll
641 in your installation package. It must be placed in the same folder
642 as the application executable.
643
644 \target QDB2
645 \section2 QDB2 for IBM DB2 (Version 7.1 and above)
646
647 The Qt DB2 plugin makes it possible to access IBM DB2 databases. It
648 has been tested with IBM DB2 v7.1 and 7.2. You must install the IBM
649 DB2 development client library, which contains the header and library
650 files necessary for compiling the QDB2 plugin.
651
652 The QDB2 driver supports prepared queries, reading/writing of Unicode
653 strings and reading/writing of BLOBs.
654
655 We suggest using a forward-only query when calling stored procedures
656 in DB2 (see QSqlQuery::setForwardOnly()).
657
658 \section3 Connection options
659 The Qt IBM DB2 plugin honors the following connection options:
660 \table
661 \header \li Attribute \li Possible value
662 \row
663 \li SQL_ATTR_ACCESS_MODE
664 \li SQL_MODE_READ_ONLY: open the database in read-only mode\br
665 SQL_MODE_READ_WRITE: open the database in read-write mode (default)
666 \row
667 \li SQL_ATTR_LOGIN_TIMEOUT
668 \li Number of seconds to wait for the database connection
669 during login (max: 32767, a value of 0 will wait forever)
670 \endtable
671
672 \section3 How to Build the QDB2 Plugin on Unix and \macos
673
674 \snippet code/doc_src_sql-driver.qdoc 18
675
676 \section3 How to Build the QDB2 Plugin on Windows
677
678 The DB2 header and include files should already be installed in the
679 right directories. You just have to build the plugin as follows:
680
681 \snippet code/doc_src_sql-driver.qdoc 20
682
683 \target QSQLITE
684 \section2 QSQLITE for SQLite (Version 3 and above)
685
686 The Qt SQLite plugin makes it possible to access SQLite
687 databases. SQLite is an in-process database, which means that it
688 is not necessary to have a database server. SQLite operates on a
689 single file, which must be set as the database name when opening
690 a connection. If the file does not exist, SQLite will try to
691 create it. SQLite also supports in-memory and temporary databases. Simply
692 pass respectively ":memory:" or an empty string as the database name.
693
694 SQLite has some restrictions regarding multiple users and
695 multiple transactions. If you try to read/write on a resource from different
696 transactions, your application might freeze until one transaction commits
697 or rolls back. The Qt SQLite driver will retry to write to a locked resource
698 until it runs into a timeout (see \c{QSQLITE_BUSY_TIMEOUT}
699 at QSqlDatabase::setConnectOptions()).
700
701 In SQLite any column, with the exception of an INTEGER PRIMARY KEY column,
702 may be used to store any type of value. For instance, a column declared as
703 INTEGER may contain an integer value in one row and a text value in the
704 next. This is due to SQLite associating the type of a value with the value
705 itself rather than with the column it is stored in. A consequence of this
706 is that the type returned by QSqlField::type() only indicates the field's
707 recommended type. No assumption of the actual type should be made from
708 this and the type of the individual values should be checked.
709
710 The driver is locked for updates while a select is executed. This
711 may cause problems when using QSqlTableModel because Qt's item views
712 fetch data as needed (with QSqlQuery::fetchMore() in the case of
713 QSqlTableModel).
714
715 You can find information about SQLite on \l{http://www.sqlite.org}.
716
717 \section3 Connection options
718 The Qt SQLite plugin honors the following connection options:
719 \table
720 \header \li Attribute \li Possible value
721 \row
722 \li QSQLITE_BUSY_TIMEOUT
723 \li Busy handler timeout in milliseconds (val <= 0: disabled),
724 see \l {https://www.sqlite.org/c3ref/busy_timeout.html}
725 {SQLite documentation} for more information
726
727 \row
728 \li QSQLITE_USE_QT_VFS
729 \li If set, the database is opened using Qt's VFS which allows to
730 open databases using QFile. This way it can open databases from
731 any read-write locations (e.g.android shared storage) but also
732 from read-only resources (e.g. qrc or android assets). Be aware
733 that when opening databases from read-only resources make sure
734 you add QSQLITE_OPEN_READONLY attribute as well.
735 Otherwise it will fail to open it.
736
737 \row
738 \li QSQLITE_OPEN_READONLY
739 \li If set, the database is open in read-only mode which will fail
740 if no database exists. Otherwise the database will be opened in
741 read-write mode and created if the database file does not yet
742 exist (default)
743 \row
744 \li QSQLITE_OPEN_URI
745 \li The given filename is interpreted as an uri, see
746 \l {https://www.sqlite.org/c3ref/open.html} {SQLITE_OPEN_URI}
747 \row
748 \li QSQLITE_ENABLE_SHARED_CACHE
749 \li If set, the database is opened in
750 \l {https://www.sqlite.org/sharedcache.html} {shared cache mode},
751 otherwise in private cache mode
752 \row
753 \li QSQLITE_ENABLE_REGEXP
754 \li If set, the plugin defines a function 'regex' which can be used
755 in queries, QRegularExpression is used for evaluation of the regex query
756 \row
757 \li QSQLITE_NO_USE_EXTENDED_RESULT_CODES
758 \li Disables the usage of the \l {https://www.sqlite.org/c3ref/extended_result_codes.html}
759 {extended result code} feature in SQLite (for backwards compatibility)
760 \row
761 \li QSQLITE_ENABLE_NON_ASCII_CASE_FOLDING
762 \li If set, the plugin replaces the functions 'lower' and 'upper' with
763 QString functions for correct case folding of non-ascii characters
764 \endtable
765
766 \section3 How to Build the QSQLITE Plugin
767
768 SQLite version 3 is included as a third-party library within Qt.
769 It can be built by passing the \c{-DFEATURE_system_sqlite=OFF} parameter to the
770 \c qt-cmake command line.
771
772 If you do not want to use the SQLite library included with Qt, you
773 can pass \c{-DFEATURE_system_sqlite=ON} to the \c qt-cmake command
774 line to use the SQLite libraries of the operating system. This is
775 recommended whenever possible, as it reduces the installation size
776 and removes one component for which you need to track security
777 advisories.
778
779 On Unix and \macos (replace \c $SQLITE with the directory where
780 SQLite resides):
781
782 \snippet code/doc_src_sql-driver.qdoc 21
783
784 On Windows (assuming that SQLite is installed in \c{C:\SQLITE}):
785
786 \snippet code/doc_src_sql-driver.qdoc 23
787
788 \section3 Enable REGEXP operator
789
790 SQLite comes with a REGEXP operation. However the needed implementation must
791 be provided by the user. For convenience a default implementation can be
792 enabled by \l{QSqlDatabase::setConnectOptions()} {setting the connect
793 option} \c{QSQLITE_ENABLE_REGEXP} before \l{QSqlDatabase::open()} {the
794 database connection is opened}. Then a SQL statement like "column REGEXP
795 'pattern'" basically expands to the Qt code
796
797 \snippet code/doc_src_sql-driver.cpp 34
798
799 For better performance the regular expressions are cached internally. By
800 default the cache size is 25, but it can be changed through the option's
801 value. For example passing "\c{QSQLITE_ENABLE_REGEXP=10}" reduces the
802 cache size to 10.
803
804 \section3 QSQLITE File Format Compatibility
805
806 SQLite minor releases sometimes break file format forward compatibility.
807 For example, SQLite 3.3 can read database files created with SQLite 3.2,
808 but databases created with SQLite 3.3 cannot be read by SQLite 3.2.
809 Please refer to the SQLite documentation and change logs for information about
810 file format compatibility between versions.
811
812 Qt minor releases usually follow the SQLite minor releases, while Qt patch releases
813 follow SQLite patch releases. Patch releases are therefore both backward and forward
814 compatible.
815
816 To force SQLite to use a specific file format, it is necessary to build and
817 ship your own database plugin with your own SQLite library as illustrated above.
818 Some versions of SQLite can be forced to write a specific file format by setting
819 the \c{SQLITE_DEFAULT_FILE_FORMAT} define when building SQLite.
820
821 \target QMIMER
822 \section2 QMIMER for Mimer SQL version 11 and higher
823
824 The Qt Mimer SQL plugin makes it possible to work with the Mimer SQL RDBMS.
825 Mimer SQL provides small footprint, scalable and robust relational database
826 solutions that conform to international ISO SQL standards. Mimer SQL is available
827 on Windows, Linux, \macos, and OpenVMS as well as several embedded platforms like QNX, Android,
828 and embedded Linux.
829
830 Mimer SQL fully support Unicode. To work with Unicode data the column types National Character (NCHAR),
831 National Character Varying (NVARCHAR), or National Character Large Object (NCLOB) must be used.
832 For more information about Mimer SQL and unicode, see \l{https://developer.mimer.com/features/multilingual-support}
833
834 \section3 QMIMER Stored Procedure Support
835
836 Mimer SQL have stored procedures according to the SQL standard (PSM) and
837 the plugin fully support IN, OUT, INOUT parameters as well as resultset procedures.
838
839 Example stored procedure with INOUT and OUT parameters:
840
841 \snippet code/doc_src_sql-driver.qdoc 44
842
843 Source code to access the INOUT and OUT values:
844
845 \snippet code/doc_src_sql-driver.cpp 40
846
847 \section3 How to Build the QMIMER Plugin on Unix and \macos
848
849 You need the Mimer SQL header files and shared libraries. Get them by installing
850 any of the Mimer SQL variants found at \l{https://developer.mimer.com}.
851
852
853 \snippet code/doc_src_sql-driver.qdoc 31
854
855 \section3 How to Build the QMIMER Plugin on Windows
856
857 You need the Mimer SQL header files and shared libraries. Get them by installing
858 any of the Mimer SQL variants found at \l{https://developer.mimer.com}.
859
860 \snippet code/doc_src_sql-driver.qdoc 32
861
862 \target QIBASE
863 \section2 QIBASE for Borland InterBase
864
865 The Qt InterBase plugin makes it possible to access the InterBase and
866 Firebird databases. InterBase can either be used as a client/server or
867 without a server in which case it operates on local files. The
868 database file must exist before a connection can be established. Firebird
869 must be used with a server configuration.
870
871 Note that InterBase requires you to specify the full path to the
872 database file, no matter whether it is stored locally or on another
873 server.
874
875 \section3 Connection options
876 The Qt Borland InterBase plugin honors the following connection options:
877 \table
878 \header \li Attribute \li Possible value
879 \row
880 \li ISC_DPB_SQL_ROLE_NAME
881 \li Specifies the login role name
882 \endtable
883
884
885 \section3 How to Build the QIBASE Plugin
886 \snippet code/doc_src_sql-driver.cpp 24
887
888 You need the InterBase/Firebird development headers and libraries
889 to build this plugin.
890
891 Due to license incompatibilities with the GPL, users of the Qt Open Source
892 Edition are not allowed to link this plugin to the commercial editions of
893 InterBase. Please use Firebird or the free edition of InterBase.
894
895 \section3 QIBASE Stored procedures
896
897 InterBase/Firebird return OUT values as result set, so when calling stored
898 procedure, only IN values need to be bound via QSqlQuery::bindValue(). The
899 RETURN/OUT values can be retrieved via QSqlQuery::value(). Example:
900
901 \snippet code/doc_src_sql-driver.cpp 26
902
903 \section3 How to Build the QIBASE Plugin on Unix and \macos
904
905 The following assumes InterBase or Firebird is installed in
906 \c{/opt/interbase}:
907
908 If you are using InterBase:
909
910 \snippet code/doc_src_sql-driver.qdoc 27
911
912 If you are using Firebird, the Firebird library has to be set explicitly:
913
914 \snippet code/doc_src_sql-driver.qdoc 28
915
916 \section3 How to Build the QIBASE Plugin on Windows
917
918 The following assumes InterBase or Firebird is installed in
919 \c{C:\interbase}:
920
921 If you are using InterBase:
922
923 \snippet code/doc_src_sql-driver.qdoc 29
924
925 If you are using Firebird:
926
927 \snippet code/doc_src_sql-driver.qdoc 30
928
929 Note that \c{C:\interbase\bin} must be in the \c PATH.
930
931 \target troubleshooting
932 \section1 Troubleshooting
933
934 You should always use client libraries that have been compiled with
935 the same compiler as you are using for your project. If you cannot get
936 a source distribution to compile the client libraries yourself, you
937 must make sure that the pre-compiled library is compatible with
938 your compiler, otherwise you will get a lot of "undefined symbols"
939 errors. Some compilers have tools to convert libraries, e.g. Borland
940 ships the tool \c{COFF2OMF.EXE} to convert libraries that have been
941 generated with Microsoft Visual C++.
942
943 If the compilation of a plugin succeeds but it cannot be loaded,
944 make sure that the following requirements are met:
945
946 \list
947 \li Ensure that the plugin is in the correct directory. You can use
948 QApplication::libraryPaths() to determine where Qt looks for plugins.
949 \li Ensure that the client libraries of the DBMS are available on the
950 system. On Unix, run the command \c{ldd} and pass the name of the
951 plugin as parameter, for example \c{ldd libqsqlmysql.so}. You will
952 get a warning if any of the client libraries could not be found.
953 On Windows, you can use Visual Studio's dependency walker. With
954 Qt Creator, you can update the \c PATH environment variable in the
955 \gui Run section of the \gui Project panel to include the path to
956 the folder containing the client libraries.
957 \li Compile Qt with \c{QT_DEBUG_PLUGINS} defined to get very verbose
958 debug output when loading plugins.
959 \endlist
960
961 Make sure you have followed the guide to \l{Deploying Plugins}.
962
963 \target development
964 \section1 How to Write Your Own Database Driver
965
966 QSqlDatabase is responsible for loading and managing database driver
967 plugins. When a database is added (see QSqlDatabase::addDatabase()),
968 the appropriate driver plugin is loaded (using QSqlDriverPlugin).
969 QSqlDatabase relies on the driver plugin to provide interfaces for
970 QSqlDriver and QSqlResult.
971
972 QSqlDriver is an abstract base class which defines the functionality
973 of a SQL database driver. This includes functions such as
974 QSqlDriver::open() and QSqlDriver::close(). QSqlDriver is responsible
975 for connecting to a database, establish the proper environment, etc.
976 In addition, QSqlDriver can create QSqlQuery objects appropriate for
977 the particular database API. QSqlDatabase forwards many of its
978 function calls directly to QSqlDriver which provides the concrete
979 implementation.
980
981 QSqlResult is an abstract base class which defines the functionality
982 of a SQL database query. This includes statements such as \c{SELECT},
983 \c{UPDATE}, and \c{ALTER} \c{TABLE}. QSqlResult contains functions
984 such as QSqlResult::next() and QSqlResult::value(). QSqlResult is
985 responsible for sending queries to the database, returning result
986 data, etc. QSqlQuery forwards many of its function calls directly to
987 QSqlResult which provides the concrete implementation.
988
989 QSqlDriver and QSqlResult are closely connected. When implementing a
990 Qt SQL driver, both of these classes must to be subclassed and the
991 abstract virtual methods in each class must be implemented.
992
993 To implement a Qt SQL driver as a plugin (so that it is
994 recognized and loaded by the Qt library at runtime), the driver
995 must use the Q_PLUGIN_METADATA() macro. Read \l{How to Create Qt
996 Plugins} for more information on this. You can also check out how
997 this is done in the SQL plugins that are provided with Qt in
998 \c{QTDIR/qtbase/src/plugins/sqldrivers}.
999
1000 The following code can be used as a skeleton for a SQL driver:
1001
1002 \snippet sqldatabase/sqldatabase.cpp 47
1003 \codeline
1004 \snippet sqldatabase/sqldatabase.cpp 48
1005*/