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

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

trunk: Merged in qt 4.6.2 sources.

  • Property svn:eol-style set to native
File size: 32.9 KB
Line 
1/****************************************************************************
2**
3** Copyright (C) 2010 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