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:
|
---|
423 |
|
---|
424 | \snippet doc/src/snippets/code/doc_src_sql-driver.qdoc 14
|
---|
425 |
|
---|
426 | \section3 How to Build the QPSQL Plugin on Windows
|
---|
427 |
|
---|
428 | Install the appropriate PostgreSQL developer libraries for your
|
---|
429 | compiler. Assuming that PostgreSQL was installed in \c{C:\psql},
|
---|
430 | build the plugin as follows:
|
---|
431 |
|
---|
432 | \snippet doc/src/snippets/code/doc_src_sql-driver.qdoc 15
|
---|
433 |
|
---|
434 | Users of MinGW may wish to consult the following online document:
|
---|
435 | \l{Compiling PostgreSQL On Native Win32 FAQ}.
|
---|
436 |
|
---|
437 | \bold{Note:} This database plugin is not supported for Windows CE.
|
---|
438 |
|
---|
439 | \target QTDS
|
---|
440 | \section2 QTDS for Sybase Adaptive Server
|
---|
441 | \section3 General Information about QTDS
|
---|
442 |
|
---|
443 | It is not possible to set the port with QSqlDatabase::setPort() due to limitations in the
|
---|
444 | Sybase client library. Refer to the Sybase documentation for information on how to set up
|
---|
445 | a Sybase client configuration file to enable connections to databases on non-default ports.
|
---|
446 |
|
---|
447 | \section3 How to Build the QDTS Plugin on Unix and Mac OS X
|
---|
448 |
|
---|
449 | Under Unix, two libraries are available which support the TDS protocol:
|
---|
450 |
|
---|
451 | \list
|
---|
452 | \i FreeTDS, a free implementation of the TDS protocol
|
---|
453 | (\l{http://www.freetds.org}). Note that FreeTDS is not yet stable,
|
---|
454 | so some functionality may not work as expected.
|
---|
455 |
|
---|
456 | \i Sybase Open Client, available from \l{http://www.sybase.com}.
|
---|
457 | Note for Linux users: Get the Open Client RPM from
|
---|
458 | \l{http://linux.sybase.com}.
|
---|
459 | \endlist
|
---|
460 |
|
---|
461 | Regardless of which library you use, the shared object file
|
---|
462 | \c{libsybdb.so} is needed. Set the \c SYBASE environment variable to
|
---|
463 | point to the directory where you installed the client library and
|
---|
464 | execute \c{qmake}:
|
---|
465 |
|
---|
466 | \snippet doc/src/snippets/code/doc_src_sql-driver.qdoc 16
|
---|
467 |
|
---|
468 | \section3 How to Build the QDTS Plugin on Windows
|
---|
469 |
|
---|
470 | You can either use the DB-Library supplied by Microsoft or the Sybase
|
---|
471 | Open Client (\l{http://www.sybase.com}). You must include \c
|
---|
472 | NTWDBLIB.LIB to build the plugin:
|
---|
473 |
|
---|
474 | \snippet doc/src/snippets/code/doc_src_sql-driver.qdoc 17
|
---|
475 |
|
---|
476 | By default the Microsoft library is used on Windows, if you want to
|
---|
477 | force the use of the Sybase Open Client, you must define \c
|
---|
478 | Q_USE_SYBASE in \c{%QTDIR%\src\sql\drivers\tds\qsql_tds.cpp}. If you
|
---|
479 | are not using a Microsoft compiler, replace \c nmake with \c make in
|
---|
480 | the line above.
|
---|
481 |
|
---|
482 | \bold{Note:} This database plugin is not supported for Windows CE.
|
---|
483 |
|
---|
484 | \target QDB2
|
---|
485 | \section2 QDB2 for IBM DB2 (Version 7.1 and Above)
|
---|
486 |
|
---|
487 | \section3 General Information about QDB2
|
---|
488 |
|
---|
489 | The Qt DB2 plugin makes it possible to access IBM DB2 databases. It
|
---|
490 | has been tested with IBM DB2 v7.1 and 7.2. You must install the IBM
|
---|
491 | DB2 development client library, which contains the header and library
|
---|
492 | files necessary for compiling the QDB2 plugin.
|
---|
493 |
|
---|
494 | The QDB2 driver supports prepared queries, reading/writing of Unicode
|
---|
495 | strings and reading/writing of BLOBs.
|
---|
496 |
|
---|
497 | We suggest using a forward-only query when calling stored procedures
|
---|
498 | in DB2 (see QSqlQuery::setForwardOnly()).
|
---|
499 |
|
---|
500 | \section3 How to Build the QDB2 Plugin on Unix and Mac OS X
|
---|
501 |
|
---|
502 | \snippet doc/src/snippets/code/doc_src_sql-driver.qdoc 18
|
---|
503 |
|
---|
504 | After installing Qt, as described in the \l{Installing Qt on X11 Platforms} document,
|
---|
505 | you also need to install the plugin in the standard location:
|
---|
506 |
|
---|
507 | \snippet doc/src/snippets/code/doc_src_sql-driver.qdoc 19
|
---|
508 |
|
---|
509 | \section3 How to Build the QDB2 Plugin on Windows
|
---|
510 |
|
---|
511 | The DB2 header and include files should already be installed in the
|
---|
512 | right directories. You just have to build the plugin as follows:
|
---|
513 |
|
---|
514 | \snippet doc/src/snippets/code/doc_src_sql-driver.qdoc 20
|
---|
515 |
|
---|
516 | If you are not using a Microsoft compiler, replace \c nmake
|
---|
517 | with \c make in the line above.
|
---|
518 |
|
---|
519 | \bold{Note:} This database plugin is not supported for Windows CE.
|
---|
520 |
|
---|
521 | \target QSQLITE2
|
---|
522 | \section2 QSQLITE2 for SQLite Version 2
|
---|
523 |
|
---|
524 | The Qt SQLite 2 plugin is offered for compatibility. Whenever
|
---|
525 | possible, use the \l{#QSQLITE}{version 3 plugin} instead. The
|
---|
526 | build instructions for version 3 apply to version 2 as well.
|
---|
527 |
|
---|
528 | \target QSQLITE
|
---|
529 | \section2 QSQLITE for SQLite (Version 3 and Above)
|
---|
530 |
|
---|
531 | \section3 General Information about QSQLITE
|
---|
532 |
|
---|
533 | The Qt SQLite plugin makes it possible to access SQLite
|
---|
534 | databases. SQLite is an in-process database, which means that it
|
---|
535 | is not necessary to have a database server. SQLite operates on a
|
---|
536 | single file, which must be set as the database name when opening
|
---|
537 | a connection. If the file does not exist, SQLite will try to
|
---|
538 | create it. SQLite also supports in-memory databases, simply pass
|
---|
539 | ":memory:" as the database name.
|
---|
540 |
|
---|
541 | SQLite has some restrictions regarding multiple users and
|
---|
542 | multiple transactions. If you try to read/write on a resource from different
|
---|
543 | transactions, your application might freeze until one transaction commits
|
---|
544 | or rolls back. The Qt SQLite driver will retry to write to a locked resource
|
---|
545 | until it runs into a timeout (see \c{QSQLITE_BUSY_TIMEOUT}
|
---|
546 | at QSqlDatabase::setConnectOptions()).
|
---|
547 |
|
---|
548 | In SQLite any column, with the exception of an INTEGER PRIMARY KEY column,
|
---|
549 | may be used to store any type of value. For instance, a column declared as
|
---|
550 | INTEGER may contain an integer value in one row and a text value in the
|
---|
551 | next. This is due to SQLite associating the type of a value with the value
|
---|
552 | itself rather than with the column it is stored in. A consequence of this
|
---|
553 | is that the type returned by QSqlField::type() only indicates the field's
|
---|
554 | recommended type. No assumption of the actual type should be made from
|
---|
555 | this and the type of the individual values should be checked.
|
---|
556 |
|
---|
557 | The driver is locked for updates while a select is executed. This
|
---|
558 | may cause problems when using QSqlTableModel because Qt's item views
|
---|
559 | fetch data as needed (with QSqlQuery::fetchMore() in the case of
|
---|
560 | QSqlTableModel).
|
---|
561 |
|
---|
562 | You can find information about SQLite on \l{http://www.sqlite.org}.
|
---|
563 |
|
---|
564 | \section3 How to Build the QSQLITE Plugin
|
---|
565 |
|
---|
566 | SQLite version 3 is included as a third-party library within Qt.
|
---|
567 | It can be built by passing the following parameters to the
|
---|
568 | configure script: \c{-plugin-sql-sqlite} (build as a plugin) or
|
---|
569 | \c{-qt-sql-sqlite} (linked directly into the Qt library).
|
---|
570 |
|
---|
571 | If you don't want to use the SQLite library included with Qt, you
|
---|
572 | can build it manually (replace \c $SQLITE by the directory where
|
---|
573 | SQLite resides):
|
---|
574 |
|
---|
575 | \snippet doc/src/snippets/code/doc_src_sql-driver.qdoc 21
|
---|
576 |
|
---|
577 | After installing Qt, as described in the \l{Installing Qt on X11 Platforms} document,
|
---|
578 | you also need to install the plugin in the standard location:
|
---|
579 |
|
---|
580 | \snippet doc/src/snippets/code/doc_src_sql-driver.qdoc 22
|
---|
581 |
|
---|
582 | On Windows:
|
---|
583 |
|
---|
584 | \snippet doc/src/snippets/code/doc_src_sql-driver.qdoc 23
|
---|
585 |
|
---|
586 | \section3 QSQLITE File Format Compatibility
|
---|
587 |
|
---|
588 | SQLite minor releases sometimes break file format forward compatibility.
|
---|
589 | For example, SQLite 3.3 can read database files created with SQLite 3.2,
|
---|
590 | but databases created with SQLite 3.3 cannot be read by SQLite 3.2.
|
---|
591 | Please refer to the SQLite documentation and change logs for information about
|
---|
592 | file format compatibility between versions.
|
---|
593 |
|
---|
594 | Qt minor releases usually follow the SQLite minor releases, while Qt patch releases
|
---|
595 | follow SQLite patch releases. Patch releases are therefore both backward and forward
|
---|
596 | compatible.
|
---|
597 |
|
---|
598 | To force SQLite to use a specific file format, it is neccessary to build and
|
---|
599 | ship your own database plugin with your own SQLite library as illustrated above.
|
---|
600 | Some versions of SQLite can be forced to write a specific file format by setting
|
---|
601 | the \c{SQLITE_DEFAULT_FILE_FORMAT} define when building SQLite.
|
---|
602 |
|
---|
603 | \target QIBASE
|
---|
604 | \section2 QIBASE for Borland InterBase
|
---|
605 |
|
---|
606 | \section3 General Information about QIBASE
|
---|
607 |
|
---|
608 | The Qt InterBase plugin makes it possible to access the InterBase and
|
---|
609 | Firebird databases. InterBase can either be used as a client/server or
|
---|
610 | without a server in which case it operates on local files. The
|
---|
611 | database file must exist before a connection can be established.
|
---|
612 |
|
---|
613 | Note that InterBase requires you to specify the full path to the
|
---|
614 | database file, no matter whether it is stored locally or on another
|
---|
615 | server.
|
---|
616 |
|
---|
617 | \snippet doc/src/snippets/code/doc_src_sql-driver.qdoc 24
|
---|
618 |
|
---|
619 | You need the InterBase/Firebird development headers and libraries
|
---|
620 | to build this plugin.
|
---|
621 |
|
---|
622 | Due to license incompatibilities with the GPL, users of the Qt Open Source
|
---|
623 | Edition are not allowed to link this plugin to the commercial editions of
|
---|
624 | InterBase. Please use Firebird or the free edition of InterBase.
|
---|
625 |
|
---|
626 | \section3 QIBASE Unicode Support and Text Encoding
|
---|
627 |
|
---|
628 | By default the driver connects to the database using UNICODE_FSS. This can
|
---|
629 | be overridden by setting the ISC_DPB_LC_CTYPE parameter with
|
---|
630 | QSqlDatabase::setConnectOptions() before opening the connection.
|
---|
631 |
|
---|
632 | \snippet doc/src/snippets/code/doc_src_sql-driver.qdoc 25
|
---|
633 |
|
---|
634 | If Qt doesn't support the given text encoding the driver will issue a
|
---|
635 | warning message and connect to the database using UNICODE_FSS.
|
---|
636 |
|
---|
637 | Note that if the text encoding set when connecting to the database is
|
---|
638 | not the same as in the database, problems with transliteration might arise.
|
---|
639 |
|
---|
640 | \section3 QIBASE Stored procedures
|
---|
641 |
|
---|
642 | InterBase/Firebird return OUT values as result set, so when calling stored
|
---|
643 | procedure, only IN values need to be bound via QSqlQuery::bindValue(). The
|
---|
644 | RETURN/OUT values can be retrieved via QSqlQuery::value(). Example:
|
---|
645 |
|
---|
646 | \snippet doc/src/snippets/code/doc_src_sql-driver.qdoc 26
|
---|
647 |
|
---|
648 | \section3 How to Build the QIBASE Plugin on Unix and Mac OS X
|
---|
649 |
|
---|
650 | The following assumes InterBase or Firebird is installed in
|
---|
651 | \c{/opt/interbase}:
|
---|
652 |
|
---|
653 | If you are using InterBase:
|
---|
654 |
|
---|
655 | \snippet doc/src/snippets/code/doc_src_sql-driver.qdoc 27
|
---|
656 |
|
---|
657 | If you are using Firebird, the Firebird library has to be set explicitly:
|
---|
658 |
|
---|
659 | \snippet doc/src/snippets/code/doc_src_sql-driver.qdoc 28
|
---|
660 |
|
---|
661 | \section3 How to Build the QIBASE Plugin on Windows
|
---|
662 |
|
---|
663 | The following assumes InterBase or Firebird is installed in
|
---|
664 | \c{C:\interbase}:
|
---|
665 |
|
---|
666 | If you are using InterBase:
|
---|
667 |
|
---|
668 | \snippet doc/src/snippets/code/doc_src_sql-driver.qdoc 29
|
---|
669 |
|
---|
670 | If you are using Firebird, the Firebird library has to be set explicitely:
|
---|
671 |
|
---|
672 | \snippet doc/src/snippets/code/doc_src_sql-driver.qdoc 30
|
---|
673 |
|
---|
674 | If you are not using a Microsoft compiler, replace \c nmake
|
---|
675 | with \c make in the line above.
|
---|
676 |
|
---|
677 | Note that \c{C:\interbase\bin} must be in the \c PATH.
|
---|
678 |
|
---|
679 | \bold{Note:} This database plugin is not supported for Windows CE.
|
---|
680 |
|
---|
681 | \target troubleshooting
|
---|
682 | \section1 Troubleshooting
|
---|
683 |
|
---|
684 | You should always use client libraries that have been compiled with
|
---|
685 | the same compiler as you are using for your project. If you cannot get
|
---|
686 | a source distibution to compile the client libraries yourself, you
|
---|
687 | must make sure that the pre-compiled library is compatible with
|
---|
688 | your compiler, otherwise you will get a lot of "undefined symbols"
|
---|
689 | errors. Some compilers have tools to convert libraries, e.g. Borland
|
---|
690 | ships the tool \c{COFF2OMF.EXE} to convert libraries that have been
|
---|
691 | generated with Microsoft Visual C++.
|
---|
692 |
|
---|
693 | If the compilation of a plugin succeeds but it cannot be loaded,
|
---|
694 | make sure that the following requirements are met:
|
---|
695 |
|
---|
696 | \list
|
---|
697 | \i Ensure that you are using a shared Qt library; you cannot use the
|
---|
698 | plugins with a static build.
|
---|
699 | \i Ensure that the plugin is in the correct directory. You can use
|
---|
700 | QApplication::libraryPaths() to determine where Qt looks for plugins.
|
---|
701 | \i Ensure that the client libraries of the DBMS are available on the
|
---|
702 | system. On Unix, run the command \c{ldd} and pass the name of the
|
---|
703 | plugin as parameter, for example \c{ldd libqsqlmysql.so}. You will
|
---|
704 | get a warning if any of the client libraries couldn't be found.
|
---|
705 | On Windows, you can use Visual Studio's dependency walker.
|
---|
706 | \i Compile Qt with \c{QT_DEBUG_COMPONENT} defined to get very verbose
|
---|
707 | debug output when loading plugins.
|
---|
708 | \endlist
|
---|
709 |
|
---|
710 | If you experience plugin load problems and see output like this:
|
---|
711 |
|
---|
712 | \snippet doc/src/snippets/code/doc_src_sql-driver.qdoc 31
|
---|
713 |
|
---|
714 | The problem is usually that the plugin had the wrong \l{How to
|
---|
715 | Create Qt Plugins#The Build Key}{build key}. This might require
|
---|
716 | removing an entry from the \l{How to Create Qt Plugins#The Plugin
|
---|
717 | Cache} {plugin cache}. Here is some more \l{How to Create Qt
|
---|
718 | Plugins#Debugging Plugins} {plugin debugging info}.
|
---|
719 |
|
---|
720 | \target development
|
---|
721 | \section1 How to Write Your Own Database Driver
|
---|
722 |
|
---|
723 | QSqlDatabase is responsible for loading and managing database driver
|
---|
724 | plugins. When a database is added (see QSqlDatabase::addDatabase()),
|
---|
725 | the appropriate driver plugin is loaded (using QSqlDriverPlugin).
|
---|
726 | QSqlDatabase relies on the driver plugin to provide interfaces for
|
---|
727 | QSqlDriver and QSqlResult.
|
---|
728 |
|
---|
729 | QSqlDriver is an abstract base class which defines the functionality
|
---|
730 | of a SQL database driver. This includes functions such as
|
---|
731 | QSqlDriver::open() and QSqlDriver::close(). QSqlDriver is responsible
|
---|
732 | for connecting to a database, establish the proper environment, etc.
|
---|
733 | In addition, QSqlDriver can create QSqlQuery objects appropriate for
|
---|
734 | the particular database API. QSqlDatabase forwards many of its
|
---|
735 | function calls directly to QSqlDriver which provides the concrete
|
---|
736 | implementation.
|
---|
737 |
|
---|
738 | QSqlResult is an abstract base class which defines the functionality
|
---|
739 | of a SQL database query. This includes statements such as \c{SELECT},
|
---|
740 | \c{UPDATE}, and \c{ALTER} \c{TABLE}. QSqlResult contains functions
|
---|
741 | such as QSqlResult::next() and QSqlResult::value(). QSqlResult is
|
---|
742 | responsible for sending queries to the database, returning result
|
---|
743 | data, etc. QSqlQuery forwards many of its function calls directly to
|
---|
744 | QSqlResult which provides the concrete implementation.
|
---|
745 |
|
---|
746 | QSqlDriver and QSqlResult are closely connected. When implementing a
|
---|
747 | Qt SQL driver, both of these classes must to be subclassed and the
|
---|
748 | abstract virtual methods in each class must be implemented.
|
---|
749 |
|
---|
750 | To implement a Qt SQL driver as a plugin (so that it is
|
---|
751 | recognized and loaded by the Qt library at runtime), the driver
|
---|
752 | must use the Q_EXPORT_PLUGIN2() macro. Read \l{How to Create Qt
|
---|
753 | Plugins} for more information on this. You can also check out how
|
---|
754 | this is done in the SQL plugins that are provided with Qt in
|
---|
755 | \c{QTDIR/src/plugins/sqldrivers} and \c{QTDIR/src/sql/drivers}.
|
---|
756 |
|
---|
757 | The following code can be used as a skeleton for a SQL driver:
|
---|
758 |
|
---|
759 | \snippet doc/src/snippets/sqldatabase/sqldatabase.cpp 47
|
---|
760 | \codeline
|
---|
761 | \snippet doc/src/snippets/sqldatabase/sqldatabase.cpp 48
|
---|
762 | */
|
---|