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