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