source: trunk/doc/src/sql-programming/sql-driver.qdoc@ 561

Last change on this file since 561 was 561, checked in by Dmitry A. Kuminov, 15 years ago

trunk: Merged in qt 4.6.1 sources.

  • Property svn:eol-style set to native
File size: 32.9 KB
Line 
1/****************************************************************************
2**
3** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
4** All rights reserved.
5** Contact: Nokia Corporation ([email protected])
6**
7** This file is part of the documentation of the Qt Toolkit.
8**
9** $QT_BEGIN_LICENSE:LGPL$
10** Commercial Usage
11** Licensees holding valid Qt Commercial licenses may use this file in
12** accordance with the Qt Commercial License Agreement provided with the
13** Software or, alternatively, in accordance with the terms contained in
14** a written agreement between you and Nokia.
15**
16** GNU Lesser General Public License Usage
17** Alternatively, this file may be used under the terms of the GNU Lesser
18** General Public License version 2.1 as published by the Free Software
19** Foundation and appearing in the file LICENSE.LGPL included in the
20** packaging of this file. Please review the following information to
21** ensure the GNU Lesser General Public License version 2.1 requirements
22** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
23**
24** In addition, as a special exception, Nokia gives you certain additional
25** rights. These rights are described in the Nokia Qt LGPL Exception
26** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
27**
28** GNU General Public License Usage
29** Alternatively, this file may be used under the terms of the GNU
30** General Public License version 3.0 as published by the Free Software
31** Foundation and appearing in the file LICENSE.GPL included in the
32** packaging of this file. Please review the following information to
33** ensure the GNU General Public License version 3.0 requirements will be
34** met: http://www.gnu.org/copyleft/gpl.html.
35**
36** If you have questions regarding the use of this file, please contact
37** Nokia at [email protected].
38** $QT_END_LICENSE$
39**
40****************************************************************************/
41
42/*!
43 \page sql-driver.html
44 \title SQL Database Drivers
45 \brief How to configure and install QtSql drivers for supported databases.
46
47 \ingroup best-practices
48
49 The QtSql module uses driver \l{How to Create Qt
50 Plugins}{plugins} to communicate with the different database
51 APIs. Since Qt's SQL Module API is database-independent, all
52 database-specific code is contained within these drivers. Several
53 drivers are supplied with Qt and other drivers can be added. The
54 driver source code is supplied and can be used as a model for
55 \l{#development}{writing your own drivers}.
56
57 \tableofcontents
58
59 \section1 Supported Databases
60
61 The table below lists the drivers included with Qt. Due to
62 license incompatibilities with the GPL, not all of the plugins
63 are provided with Open Source Versions of Qt.
64
65 \table
66 \header \o Driver name \o DBMS
67 \row \o \link #QDB2 QDB2\endlink \o IBM DB2 (version 7.1 and above)
68 \row \o \link #QIBASE QIBASE\endlink \o Borland InterBase
69 \row \o \link #QMYSQL QMYSQL\endlink \o MySQL
70 \row \o \link #QOCI QOCI\endlink \o Oracle Call Interface Driver
71 \row \o \link #QODBC QODBC\endlink
72 \o Open Database Connectivity (ODBC) - Microsoft SQL Server and other
73 ODBC-compliant databases
74 \row \o \link #QPSQL QPSQL\endlink \o PostgreSQL (versions 7.3 and above)
75 \row \o \link #QSQLITE2 QSQLITE2\endlink \o SQLite version 2
76 \row \o \link #QSQLITE QSQLITE\endlink \o SQLite version 3
77 \row \o \link #QTDS QTDS\endlink \o Sybase Adaptive Server
78 \endtable
79
80 SQLite is the in-process database system with the best test coverage
81 and support on all platforms. Oracle via OCI, and PostreSQL and MySQL
82 through either ODBC or a native driver are well-tested on Windows and
83 Linux. The completeness of the support for other systems depends on the
84 availability and quality of client libraries.
85
86 \bold{Note:} To build a driver plugin you need to have the appropriate
87 client library for your Database Management System (DBMS). This provides
88 access to the API exposed by the DBMS, and is typically shipped with it.
89 Most installation programs also allow you to install "development
90 libraries", and these are what you need. These libraries are responsible
91 for the low-level communication with the DBMS.
92
93 \target building
94 \section1 Building the Drivers Using Configure
95
96 On Unix and Mac OS X, the Qt \c configure script tries to
97 automatically detect the available client libraries on your
98 machine. Run \c{configure -help} to see what drivers can be
99 built. You should get an output similar to this:
100
101 \snippet doc/src/snippets/code/doc_src_sql-driver.qdoc 0
102
103 The \c configure script cannot detect the neccessary libraries
104 and include files if they are not in the standard paths, so it
105 may be necessary to specify these paths using the \c -I and \c -L
106 command-line options. For example, if your MySQL include files
107 are installed in \c /usr/local/mysql (or in \c{C:\mysql\include}
108 on Windows), then pass the following parameter to configure: \c
109 -I/usr/local/mysql (or \c{-I C:\mysql\include} for Windows).
110
111 On Windows the \c -I parameter doesn't accept spaces in
112 filenames, so use the 8.3 name instead; for example, use
113 \c{C:\progra~1\mysql} instead of \c{C:\Program Files\mysql}.
114
115 Use the \c{-qt-sql-<driver>} parameter to build the database driver
116 statically into your Qt library or \c{-plugin-sql-<driver>} to build
117 the driver as a plugin. Look at the sections that follow for
118 additional information about required libraries.
119
120 \target buildingmanually
121 \section1 Building the Plugins Manually
122
123 \target QMYSQL
124 \section2 QMYSQL for MySQL 4 and higher
125
126 \section3 QMYSQL Stored Procedure Support
127
128 MySQL 5 introduces stored procedure support at the SQL level, but no
129 API to control IN, OUT and INOUT parameters. Therefore, parameters
130 have to be set and read using SQL commands instead of QSqlQuery::bindValue().
131
132 Example stored procedure:
133
134 \snippet doc/src/snippets/code/doc_src_sql-driver.qdoc 1
135
136 Source code to access the OUT values:
137
138 \snippet doc/src/snippets/code/doc_src_sql-driver.qdoc 2
139
140 \bold{Note:} \c{@outval1} and \c{@outval2} are variables local to the current
141 connection and will not be affected by queries sent from another host
142 or connection.
143
144 \section3 Embedded MySQL Server
145
146 The MySQL embedded server is a drop-in replacement for the normal
147 client library. With the embedded MySQL server, a MySQL server is
148 not required to use MySQL functionality.
149
150 To use the embedded MySQL server, simply link the Qt plugin to \c
151 libmysqld instead of libmysqlclient. This can be done by replacing
152 \c -lmysqlclient_r by \c -lmysqld in the \c qmake command in the
153 section below.
154
155 Please refer to the MySQL documentation, chapter "libmysqld, the Embedded
156 MySQL Server Library" for more information about the MySQL embedded server.
157
158 \section3 How to Build the QMYSQL Plugin on Unix and Mac OS X
159
160 You need the MySQL header files and as well as the shared library
161 \c{libmysqlclient.so}. Depending on your Linux distribution you may
162 need to install a package which is usually called "mysql-devel".
163
164 Tell \l qmake where to find the MySQL header files and shared
165 libraries (here it is assumed that MySQL is installed in
166 \c{/usr/local}) and run \c{make}:
167
168 \snippet doc/src/snippets/code/doc_src_sql-driver.qdoc 3
169
170 After installing Qt, as described in the \l{Installing Qt on X11
171 Platforms} document, you also need to install the plugin in the
172 standard location:
173
174 \snippet doc/src/snippets/code/doc_src_sql-driver.qdoc 4
175
176 \section3 How to Build the QMYSQL Plugin on Windows
177
178 You need to get the MySQL installation files. Run \c SETUP.EXE and
179 choose "Custom Install". Install the "Libs & Include Files" Module.
180 Build the plugin as follows (here it is assumed that MySQL is
181 installed in \c{C:\MySQL}):
182
183 \snippet doc/src/snippets/code/doc_src_sql-driver.qdoc 5
184
185 If you are not using a Microsoft compiler, replace \c nmake with \c
186 make in the line above.
187
188 \note This database plugin is not supported for Windows CE.
189
190 \note Including \c{"-o Makefile"} as an argument to \l qmake to
191 tell it where to build the makefile can cause the plugin to be
192 built in release mode only. If you are expecting a debug version
193 to be built as well, don't use the \c{"-o Makefile"} option.
194
195 \target QOCI
196 \section2 QOCI for the Oracle Call Interface (OCI)
197
198 \section3 General Information about the OCI plugin
199
200 The Qt OCI plugin supports Oracle 9i, 10g and higher. After
201 connecting to the Oracle server, the plugin will auto-detect the
202 database version and enable features accordingly.
203
204 It's possible to connect to a Oracle database without a tnsnames.ora file.
205 This requires that the database SID is passed to the driver as the database
206 name and that a hostname is given.
207
208 \section3 OCI User Authentication
209
210 The Qt OCI plugin supports authentication using
211 external credentials (OCI_CRED_EXT). Usually, this means that the database
212 server will use the user authentication provided by the operating system
213 instead of its own authentication mechanism.
214
215 Leave the username and password empty when opening a connection with
216 QSqlDatabase to use the external credentials authentication.
217
218 \section3 OCI BLOB/LOB Support
219
220 Binary Large Objects (BLOBs) can be read and written, but be aware
221 that this process may require a lot of memory. You should use a forward
222 only query to select LOB fields (see QSqlQuery::setForwardOnly()).
223
224 Inserting BLOBs should be done using either a prepared query where the
225 BLOBs are bound to placeholders or QSqlTableModel, which uses a prepared
226 query to do this internally.
227
228 \section3 How to Build the OCI Plugin on Unix and Mac OS X
229
230 For Oracle 10g, all you need is the "Instant Client Package - Basic" and
231 "Instant Client Package - SDK". For Oracle prior to 10g, you require
232 the standard Oracle client and the SDK packages.
233
234 Oracle library files required to build the driver:
235
236 \list
237 \i \c libclntsh.so (all versions)
238 \i \c libwtc9.so (only Oracle 9)
239 \endlist
240
241 Tell \c qmake where to find the Oracle header files and shared
242 libraries and run make:
243
244 For Oracle version 9:
245 \snippet doc/src/snippets/code/doc_src_sql-driver.qdoc 6
246
247 For Oracle version 10, we assume that you installed the RPM packages of the
248 Instant Client Package SDK (you need to adjust the version number accordingly):
249 \snippet doc/src/snippets/code/doc_src_sql-driver.qdoc 7
250
251 \bold{Note:} If you are using the Oracle Instant Client package,
252 you will need to set LD_LIBRARY_PATH when building the OCI SQL plugin
253 and when running an applicaiton that uses the OCI SQL plugin. You can
254 avoid this requirement by setting and RPATH and listing all of the
255 libraries to link to. Here is an example:
256 \snippet doc/src/snippets/code/doc_src_sql-driver.qdoc 32
257
258 If you wish to build the OCI plugin manually with this method the procedure looks like this:
259 \snippet doc/src/snippets/code/doc_src_sql-driver.qdoc 33
260
261 \section3 How to Build the OCI Plugin on Windows
262
263 Choosing the option "Programmer" in the Oracle Client Installer from
264 the Oracle Client Installation CD is sufficient to build the plugin.
265
266 Build the plugin as follows (here it is assumed that Oracle Client is
267 installed in \c{C:\oracle}):
268
269 \snippet doc/src/snippets/code/doc_src_sql-driver.qdoc 8
270
271 If you are not using a Microsoft compiler, replace \c nmake with \c
272 make in the line above.
273
274 When you run your application you will also need to add the \c oci.dll
275 path to your \c PATH environment variable:
276
277 \snippet doc/src/snippets/code/doc_src_sql-driver.qdoc 9
278
279 \bold{Note:} This database plugin is not supported for Windows CE.
280
281 \target QODBC
282 \section2 QODBC for Open Database Connectivity (ODBC)
283
284 \section3 General Information about the ODBC plugin
285
286 ODBC is a general interface that allows you to connect to multiple
287 DBMSs using a common interface. The QODBC driver allows you to connect
288 to an ODBC driver manager and access the available data sources. Note
289 that you also need to install and configure ODBC drivers for the ODBC
290 driver manager that is installed on your system. The QODBC plugin
291 then allows you to use these data sources in your Qt applications.
292
293 \bold{Note:} You should use native drivers in preference to the ODBC
294 driver where they are available. ODBC support can be used as a fallback
295 for compliant databases if no native drivers are available.
296
297 On Windows an ODBC driver manager should be installed by default.
298 For Unix systems there are some implementations which must be
299 installed first. Note that every client that uses your application is
300 required to have an ODBC driver manager installed, otherwise the
301 QODBC plugin will not work.
302
303 Be aware that when connecting to an ODBC datasource you must pass in
304 the name of the ODBC datasource to the QSqlDatabase::setDatabaseName()
305 function rather than the actual database name.
306
307 The QODBC Plugin needs an ODBC compliant driver manager version 2.0 or
308 later to work. Some ODBC drivers claim to be version 2.0 compliant,
309 but do not offer all the necessary functionality. The QODBC plugin
310 therefore checks whether the data source can be used after a
311 connection has been established and refuses to work if the check
312 fails. If you don't like this behavior, you can remove the \c{#define
313 ODBC_CHECK_DRIVER} line from the file \c{qsql_odbc.cpp}. Do this at
314 your own risk!
315
316 By default, Qt instructs the ODBC driver to behave as an ODBC 2.x
317 driver. However, for some \e{driver-manager/ODBC 3.x-driver}
318 combinations (e.g., \e{unixODBC/MaxDB ODBC}), telling the ODBC
319 driver to behave as a 2.x driver can cause the driver plugin to
320 have unexpected behavior. To avoid this problem, instruct the ODBC
321 driver to behave as a 3.x driver by
322 \l{QSqlDatabase::setConnectOptions()} {setting the connect option}
323 \c{"SQL_ATTR_ODBC_VERSION=SQL_OV_ODBC3"} before you
324 \l{QSqlDatabase::open()} {open your database connection}. Note
325 that this will affect multiple aspects of ODBC driver behavior,
326 e.g., the SQLSTATEs. Before setting this connect option, consult
327 your ODBC documentation about behavior differences you can expect.
328
329 If you experience very slow access of the ODBC datasource, make sure
330 that ODBC call tracing is turned off in the ODBC datasource manager.
331
332 Some drivers don't support scrollable cursors. In that case case only
333 queries in forwardOnly mode can be used successfully.
334
335 \section3 ODBC Stored Procedure Support
336
337 With Microsoft SQL Server the result set returned by a stored
338 procedure that uses the return statement, or returns multiple result
339 sets, will be accessible only if you set the query's forward only
340 mode to \e forward using \l QSqlQuery::setForwardOnly().
341
342 \snippet doc/src/snippets/code/doc_src_sql-driver.qdoc 10
343
344 \bold{Note:} The value returned by the stored procedure's return statement
345 is discarded.
346
347 \section3 ODBC Unicode Support
348
349 The QODBC Plugin will use the Unicode API if UNICODE is defined. On
350 Windows NT based systems, this is the default. Note that the ODBC
351 driver and the DBMS must also support Unicode.
352
353 Some driver managers and drivers don't support UNICODE. To use the
354 QODBC plugin with such drivers it has to be compiled with the
355 Q_ODBC_VERSION_2 defined.
356
357 For the Oracle 9 ODBC driver (Windows), it is neccessary to check
358 "SQL_WCHAR support" in the ODBC driver manager otherwise Oracle
359 will convert all Unicode strings to local 8-bit.
360
361 \section3 How to Build the ODBC Plugin on Unix and Mac OS X
362
363 It is recommended that you use unixODBC. You can find the latest
364 version and ODBC drivers at \l http://www.unixodbc.org.
365 You need the unixODBC header files and shared libraries.
366
367 Tell \c qmake where to find the unixODBC header files and shared
368 libraries (here it is assumed that unixODBC is installed in
369 \c{/usr/local/unixODBC}) and run \c{make}:
370
371 \snippet doc/src/snippets/code/doc_src_sql-driver.qdoc 11
372
373 \section3 How to Build the ODBC Plugin on Windows
374
375 The ODBC header and include files should already be installed in the
376 right directories. You just have to build the plugin as follows:
377
378 \snippet doc/src/snippets/code/doc_src_sql-driver.qdoc 12
379
380 If you are not using a Microsoft compiler, replace \c nmake with \c
381 make in the line above.
382
383 \bold{Note:} This database plugin is not officially supported for Windows CE.
384
385 \target QPSQL
386 \section2 QPSQL for PostgreSQL (Version 7.3 and Above)
387
388 \section3 General Information about the QPSQL driver
389
390 The QPSQL driver supports version 7.3 and higher of the PostgreSQL server.
391 We recommend that you use a client library from version 7.3.15, 7.4.13,
392 8.0.8, 8.1.4 or more recent as these versions contain security fixes, and
393 as the QPSQL driver might not build with older versions of the client
394 library depending on your platform.
395
396 For more information about PostgreSQL visit \l http://www.postgresql.org.
397
398 \section3 QPSQL Unicode Support
399
400 The QPSQL driver automatically detects whether the PostgreSQL
401 database you are connecting to supports Unicode or not. Unicode is
402 automatically used if the server supports it. Note that the driver
403 only supports the UTF-8 encoding. If your database uses any other
404 encoding, the server must be compiled with Unicode conversion
405 support.
406
407 Unicode support was introduced in PostgreSQL version 7.1 and it will
408 only work if both the server and the client library have been compiled
409 with multibyte support. More information about how to set up a
410 multibyte enabled PostgreSQL server can be found in the PostgreSQL
411 Administrator Guide, Chapter 5.
412
413 \section3 QPSQL BLOB Support
414
415 Binary Large Objects are supported through the \c BYTEA field type in
416 PostgreSQL server versions >= 7.1.
417
418 \section3 How to Build the QPSQL Plugin on Unix and Mac OS X
419
420 You need the PostgreSQL client library and headers installed.
421
422 To make \c qmake find the PostgreSQL header files and shared
423 libraries, run \c qmake the following way (assuming that the
424 PostgreSQL client is installed in \c{/usr}):
425
426 \snippet doc/src/snippets/code/doc_src_sql-driver.qdoc 13
427
428 After installing Qt, as described in the \l{Installing Qt on X11 Platforms} document,
429 you also need to install the plugin in the standard location:
430
431 \snippet doc/src/snippets/code/doc_src_sql-driver.qdoc 14
432
433 \section3 How to Build the QPSQL Plugin on Windows
434
435 Install the appropriate PostgreSQL developer libraries for your
436 compiler. Assuming that PostgreSQL was installed in \c{C:\psql},
437 build the plugin as follows:
438
439 \snippet doc/src/snippets/code/doc_src_sql-driver.qdoc 15
440
441 Users of MinGW may wish to consult the following online document:
442 \l{Compiling PostgreSQL On Native Win32 FAQ}.
443
444 \bold{Note:} This database plugin is not supported for Windows CE.
445
446 \target QTDS
447 \section2 QTDS for Sybase Adaptive Server
448 \section3 General Information about QTDS
449
450 It is not possible to set the port with QSqlDatabase::setPort() due to limitations in the
451 Sybase client library. Refer to the Sybase documentation for information on how to set up
452 a Sybase client configuration file to enable connections to databases on non-default ports.
453
454 \section3 How to Build the QTDS Plugin on Unix and Mac OS X
455
456 Under Unix, two libraries are available which support the TDS protocol:
457
458 \list
459 \i FreeTDS, a free implementation of the TDS protocol
460 (\l{http://www.freetds.org}). Note that FreeTDS is not yet stable,
461 so some functionality may not work as expected.
462
463 \i Sybase Open Client, available from \l{http://www.sybase.com}.
464 Note for Linux users: Get the Open Client RPM from
465 \l{http://linux.sybase.com}.
466 \endlist
467
468 Regardless of which library you use, the shared object file
469 \c{libsybdb.so} is needed. Set the \c SYBASE environment variable to
470 point to the directory where you installed the client library and
471 execute \c{qmake}:
472
473 \snippet doc/src/snippets/code/doc_src_sql-driver.qdoc 16
474
475 \section3 How to Build the QDTS Plugin on Windows
476
477 You can either use the DB-Library supplied by Microsoft or the Sybase
478 Open Client (\l{http://www.sybase.com}). You must include \c
479 NTWDBLIB.LIB to build the plugin:
480
481 \snippet doc/src/snippets/code/doc_src_sql-driver.qdoc 17
482
483 By default the Microsoft library is used on Windows, if you want to
484 force the use of the Sybase Open Client, you must define \c
485 Q_USE_SYBASE in \c{%QTDIR%\src\sql\drivers\tds\qsql_tds.cpp}. If you
486 are not using a Microsoft compiler, replace \c nmake with \c make in
487 the line above.
488
489 \bold{Note:} This database plugin is not supported for Windows CE.
490
491 \target QDB2
492 \section2 QDB2 for IBM DB2 (Version 7.1 and Above)
493
494 \section3 General Information about QDB2
495
496 The Qt DB2 plugin makes it possible to access IBM DB2 databases. It
497 has been tested with IBM DB2 v7.1 and 7.2. You must install the IBM
498 DB2 development client library, which contains the header and library
499 files necessary for compiling the QDB2 plugin.
500
501 The QDB2 driver supports prepared queries, reading/writing of Unicode
502 strings and reading/writing of BLOBs.