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

Last change on this file since 321 was 2, checked in by Dmitry A. Kuminov, 16 years ago

Initially imported qt-all-opensource-src-4.5.1 from Trolltech.

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