doc/src/sql-programming/sql-driver.qdoc
changeset 0 1918ee327afb
equal deleted inserted replaced
-1:000000000000 0:1918ee327afb
       
     1 /****************************************************************************
       
     2 **
       
     3 ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
       
     4 ** All rights reserved.
       
     5 ** Contact: Nokia Corporation (qt-info@nokia.com)
       
     6 **
       
     7 ** This file is part of the documentation of the Qt Toolkit.
       
     8 **
       
     9 ** $QT_BEGIN_LICENSE:LGPL$
       
    10 ** No Commercial Usage
       
    11 ** This file contains pre-release code and may not be distributed.
       
    12 ** You may use this file in accordance with the terms and conditions
       
    13 ** contained in the Technology Preview License Agreement accompanying
       
    14 ** this package.
       
    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 ** If you have questions regarding the use of this file, please contact
       
    29 ** Nokia at qt-info@nokia.com.
       
    30 **
       
    31 **
       
    32 **
       
    33 **
       
    34 **
       
    35 **
       
    36 **
       
    37 **
       
    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.
       
   503 
       
   504     We suggest using a forward-only query when calling stored procedures
       
   505     in DB2 (see QSqlQuery::setForwardOnly()).
       
   506 
       
   507     \section3 How to Build the QDB2 Plugin on Unix and Mac OS X
       
   508 
       
   509     \snippet doc/src/snippets/code/doc_src_sql-driver.qdoc 18
       
   510 
       
   511     After installing Qt, as described in the \l{Installing Qt on X11 Platforms} document,
       
   512     you also need to install the plugin in the standard location:
       
   513 
       
   514     \snippet doc/src/snippets/code/doc_src_sql-driver.qdoc 19
       
   515 
       
   516     \section3 How to Build the QDB2 Plugin on Windows
       
   517 
       
   518     The DB2 header and include files should already be installed in the
       
   519     right directories. You just have to build the plugin as follows:
       
   520 
       
   521     \snippet doc/src/snippets/code/doc_src_sql-driver.qdoc 20
       
   522 
       
   523     If you are not using a Microsoft compiler, replace \c nmake
       
   524     with \c make in the line above.
       
   525 
       
   526     \bold{Note:} This database plugin is not supported for Windows CE.
       
   527 
       
   528     \target QSQLITE2
       
   529     \section2 QSQLITE2 for SQLite Version 2
       
   530 
       
   531     The Qt SQLite 2 plugin is offered for compatibility. Whenever
       
   532     possible, use the \l{#QSQLITE}{version 3 plugin} instead. The
       
   533     build instructions for version 3 apply to version 2 as well.
       
   534 
       
   535     \target QSQLITE
       
   536     \section2 QSQLITE for SQLite (Version 3 and Above)
       
   537 
       
   538     \section3 General Information about QSQLITE
       
   539 
       
   540     The Qt SQLite plugin makes it possible to access SQLite
       
   541     databases. SQLite is an in-process database, which means that it
       
   542     is not necessary to have a database server. SQLite operates on a
       
   543     single file, which must be set as the database name when opening
       
   544     a connection. If the file does not exist, SQLite will try to
       
   545     create it. SQLite also supports in-memory databases, simply pass
       
   546     ":memory:" as the database name.
       
   547 
       
   548     SQLite has some restrictions regarding multiple users and
       
   549     multiple transactions. If you try to read/write on a resource from different
       
   550     transactions, your application might freeze until one transaction commits
       
   551     or rolls back. The Qt SQLite driver will retry to write to a locked resource
       
   552     until it runs into a timeout (see \c{QSQLITE_BUSY_TIMEOUT}
       
   553     at QSqlDatabase::setConnectOptions()).
       
   554 
       
   555     In SQLite any column, with the exception of an INTEGER PRIMARY KEY column,
       
   556     may be used to store any type of value. For instance, a column declared as
       
   557     INTEGER may contain an integer value in one row and a text value in the
       
   558     next. This is due to SQLite associating the type of a value with the value
       
   559     itself rather than with the column it is stored in. A consequence of this
       
   560     is that the type returned by QSqlField::type() only indicates the field's
       
   561     recommended type. No assumption of the actual type should be made from
       
   562     this and the type of the individual values should be checked.
       
   563 
       
   564     The driver is locked for updates while a select is executed. This
       
   565     may cause problems when using QSqlTableModel because Qt's item views
       
   566     fetch data as needed (with QSqlQuery::fetchMore() in the case of
       
   567     QSqlTableModel).
       
   568 
       
   569     You can find information about SQLite on \l{http://www.sqlite.org}.
       
   570 
       
   571     \section3 How to Build the QSQLITE Plugin
       
   572 
       
   573     SQLite version 3 is included as a third-party library within Qt.
       
   574     It can be built by passing the following parameters to the
       
   575     configure script: \c{-plugin-sql-sqlite} (build as a plugin) or
       
   576     \c{-qt-sql-sqlite} (linked directly into the Qt library).
       
   577 
       
   578     If you don't want to use the SQLite library included with Qt, you
       
   579     can build it manually (replace \c $SQLITE by the directory where
       
   580     SQLite resides):
       
   581 
       
   582     \snippet doc/src/snippets/code/doc_src_sql-driver.qdoc 21
       
   583 
       
   584     After installing Qt, as described in the \l{Installing Qt on X11 Platforms} document,
       
   585     you also need to install the plugin in the standard location:
       
   586 
       
   587     \snippet doc/src/snippets/code/doc_src_sql-driver.qdoc 22
       
   588 
       
   589     On Windows:
       
   590 
       
   591     \snippet doc/src/snippets/code/doc_src_sql-driver.qdoc 23
       
   592 
       
   593     \section3 QSQLITE File Format Compatibility
       
   594 
       
   595     SQLite minor releases sometimes break file format forward compatibility.
       
   596     For example, SQLite 3.3 can read database files created with SQLite 3.2,
       
   597     but databases created with SQLite 3.3 cannot be read by SQLite 3.2.
       
   598     Please refer to the SQLite documentation and change logs for information about
       
   599     file format compatibility between versions.
       
   600 
       
   601     Qt minor releases usually follow the SQLite minor releases, while Qt patch releases
       
   602     follow SQLite patch releases. Patch releases are therefore both backward and forward
       
   603     compatible.
       
   604 
       
   605     To force SQLite to use a specific file format, it is neccessary to build and
       
   606     ship your own database plugin with your own SQLite library as illustrated above.
       
   607     Some versions of SQLite can be forced to write a specific file format by setting
       
   608     the \c{SQLITE_DEFAULT_FILE_FORMAT} define when building SQLite.
       
   609 
       
   610     \target QIBASE
       
   611     \section2 QIBASE for Borland InterBase
       
   612 
       
   613     \section3 General Information about QIBASE
       
   614 
       
   615     The Qt InterBase plugin makes it possible to access the InterBase and
       
   616     Firebird databases. InterBase can either be used as a client/server or
       
   617     without a server in which case it operates on local files. The
       
   618     database file must exist before a connection can be established. Firebird
       
   619     must be used with a server configuration.
       
   620 
       
   621     Note that InterBase requires you to specify the full path to the
       
   622     database file, no matter whether it is stored locally or on another
       
   623     server.
       
   624 
       
   625     \snippet doc/src/snippets/code/doc_src_sql-driver.qdoc 24
       
   626 
       
   627     You need the InterBase/Firebird development headers and libraries
       
   628     to build this plugin.
       
   629 
       
   630     Due to license incompatibilities with the GPL, users of the Qt Open Source
       
   631     Edition are not allowed to link this plugin to the commercial editions of
       
   632     InterBase. Please use Firebird or the free edition of InterBase.
       
   633 
       
   634     \section3 QIBASE Unicode Support and Text Encoding
       
   635 
       
   636     By default the driver connects to the database using UNICODE_FSS. This can
       
   637     be overridden by setting the ISC_DPB_LC_CTYPE parameter with
       
   638     QSqlDatabase::setConnectOptions() before opening the connection.
       
   639 
       
   640     \snippet doc/src/snippets/code/doc_src_sql-driver.qdoc 25
       
   641 
       
   642     If Qt doesn't support the given text encoding the driver will issue a
       
   643     warning message and connect to the database using UNICODE_FSS.
       
   644 
       
   645     Note that if the text encoding set when connecting to the database is
       
   646     not the same as in the database, problems with transliteration might arise.
       
   647 
       
   648     \section3 QIBASE Stored procedures
       
   649 
       
   650     InterBase/Firebird return OUT values as result set, so when calling stored
       
   651     procedure, only IN values need to be bound via QSqlQuery::bindValue(). The
       
   652     RETURN/OUT values can be retrieved via QSqlQuery::value(). Example:
       
   653 
       
   654     \snippet doc/src/snippets/code/doc_src_sql-driver.qdoc 26
       
   655 
       
   656     \section3 How to Build the QIBASE Plugin on Unix and Mac OS X
       
   657 
       
   658     The following assumes InterBase or Firebird is installed in
       
   659     \c{/opt/interbase}:
       
   660 
       
   661     If you are using InterBase:
       
   662 
       
   663     \snippet doc/src/snippets/code/doc_src_sql-driver.qdoc 27
       
   664 
       
   665     If you are using Firebird, the Firebird library has to be set explicitly:
       
   666 
       
   667     \snippet doc/src/snippets/code/doc_src_sql-driver.qdoc 28
       
   668 
       
   669     \section3 How to Build the QIBASE Plugin on Windows
       
   670 
       
   671     The following assumes InterBase or Firebird is installed in
       
   672     \c{C:\interbase}:
       
   673 
       
   674     If you are using InterBase:
       
   675 
       
   676     \snippet doc/src/snippets/code/doc_src_sql-driver.qdoc 29
       
   677 
       
   678     If you are using Firebird, the Firebird library has to be set explicitely:
       
   679 
       
   680     \snippet doc/src/snippets/code/doc_src_sql-driver.qdoc 30
       
   681 
       
   682     If you are not using a Microsoft compiler, replace \c nmake
       
   683     with \c make in the line above.
       
   684 
       
   685     Note that \c{C:\interbase\bin} must be in the \c PATH.
       
   686 
       
   687     \bold{Note:} This database plugin is not supported for Windows CE.
       
   688 
       
   689     \target troubleshooting
       
   690     \section1 Troubleshooting
       
   691 
       
   692     You should always use client libraries that have been compiled with
       
   693     the same compiler as you are using for your project. If you cannot get
       
   694     a source distibution to compile the client libraries yourself, you
       
   695     must make sure that the pre-compiled library is compatible with
       
   696     your compiler, otherwise you will get a lot of "undefined symbols"
       
   697     errors. Some compilers have tools to convert libraries, e.g. Borland
       
   698     ships the tool \c{COFF2OMF.EXE} to convert libraries that have been
       
   699     generated with Microsoft Visual C++.
       
   700 
       
   701     If the compilation of a plugin succeeds but it cannot be loaded,
       
   702     make sure that the following requirements are met:
       
   703 
       
   704     \list
       
   705     \i Ensure that you are using a shared Qt library; you cannot use the
       
   706         plugins with a static build.
       
   707     \i Ensure that the plugin is in the correct directory. You can use
       
   708         QApplication::libraryPaths() to determine where Qt looks for plugins.
       
   709     \i Ensure that the client libraries of the DBMS are available on the
       
   710         system. On Unix, run the command \c{ldd} and pass the name of the
       
   711         plugin as parameter, for example \c{ldd libqsqlmysql.so}. You will
       
   712         get a warning if any of the client libraries couldn't be found.
       
   713         On Windows, you can use Visual Studio's dependency walker.
       
   714     \i Compile Qt with \c{QT_DEBUG_COMPONENT} defined to get very verbose
       
   715         debug output when loading plugins.
       
   716     \endlist
       
   717 
       
   718     Make sure you have followed the guide to \l{Deploying Plugins}.
       
   719     If you experience plugin load problems and see output like this:
       
   720 
       
   721     \snippet doc/src/snippets/code/doc_src_sql-driver.qdoc 31
       
   722 
       
   723     the problem is usually that the plugin had the wrong \l{Deploying
       
   724     Plugins#The Build Key}{build key}. This might require removing an
       
   725     entry from the \l{Deploying Plugins#The Plugin Cache} {plugin cache}.
       
   726 
       
   727     \target development
       
   728     \section1 How to Write Your Own Database Driver
       
   729 
       
   730     QSqlDatabase is responsible for loading and managing database driver
       
   731     plugins. When a database is added (see QSqlDatabase::addDatabase()),
       
   732     the appropriate driver plugin is loaded (using QSqlDriverPlugin).
       
   733     QSqlDatabase relies on the driver plugin to provide interfaces for
       
   734     QSqlDriver and QSqlResult.
       
   735 
       
   736     QSqlDriver is an abstract base class which defines the functionality
       
   737     of a SQL database driver. This includes functions such as
       
   738     QSqlDriver::open() and QSqlDriver::close(). QSqlDriver is responsible
       
   739     for connecting to a database, establish the proper environment, etc.
       
   740     In addition, QSqlDriver can create QSqlQuery objects appropriate for
       
   741     the particular database API. QSqlDatabase forwards many of its
       
   742     function calls directly to QSqlDriver which provides the concrete
       
   743     implementation.
       
   744 
       
   745     QSqlResult is an abstract base class which defines the functionality
       
   746     of a SQL database query. This includes statements such as \c{SELECT},
       
   747     \c{UPDATE}, and \c{ALTER} \c{TABLE}. QSqlResult contains functions
       
   748     such as QSqlResult::next() and QSqlResult::value(). QSqlResult is
       
   749     responsible for sending queries to the database, returning result
       
   750     data, etc. QSqlQuery forwards many of its function calls directly to
       
   751     QSqlResult which provides the concrete implementation.
       
   752 
       
   753     QSqlDriver and QSqlResult are closely connected. When implementing a
       
   754     Qt SQL driver, both of these classes must to be subclassed and the
       
   755     abstract virtual methods in each class must be implemented.
       
   756 
       
   757     To implement a Qt SQL driver as a plugin (so that it is
       
   758     recognized and loaded by the Qt library at runtime), the driver
       
   759     must use the Q_EXPORT_PLUGIN2() macro. Read \l{How to Create Qt
       
   760     Plugins} for more information on this. You can also check out how
       
   761     this is done in the SQL plugins that are provided with Qt in
       
   762     \c{QTDIR/src/plugins/sqldrivers} and \c{QTDIR/src/sql/drivers}.
       
   763 
       
   764     The following code can be used as a skeleton for a SQL driver:
       
   765 
       
   766     \snippet doc/src/snippets/sqldatabase/sqldatabase.cpp 47
       
   767     \codeline
       
   768     \snippet doc/src/snippets/sqldatabase/sqldatabase.cpp 48
       
   769 */