1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460
|
/****************************************************************************
**
** Documentation for sql driver programming
**
** Copyright (C) 1992-2001 Trolltech AS. All rights reserved.
**
** This file is part of the Qt GUI Toolkit.
**
** This file may be distributed under the terms of the Q Public License
** as defined by Trolltech AS of Norway and appearing in the file
** LICENSE.QPL included in the packaging of this file.
**
** This file may be distributed and/or modified under the terms of the
** GNU General Public License version 2 as published by the Free Software
** Foundation and appearing in the file LICENSE.GPL included in the
** packaging of this file.
**
** Licensees holding valid Qt Enterprise Edition or Qt Professional Edition
** licenses may use this file in accordance with the Qt Commercial License
** Agreement provided with the Software.
**
** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
**
** See http://www.trolltech.com/pricing.html or email sales@trolltech.com for
** information about Qt Commercial License Agreements.
** See http://www.trolltech.com/qpl/ for QPL licensing information.
** See http://www.trolltech.com/gpl/ for GPL licensing information.
**
** Contact info@trolltech.com if any conditions of this licensing are
** not clear to you.
**
**********************************************************************/
/*! \page sql-driver.html
\title SQL Module - Drivers
\tableofcontents
\target Introduction
\section1 Introduction
The \link sql.html SQL Module\endlink uses driver \link plugins-howto.html
plugins\endlink in order to communicate with different database APIs.
Since the SQL Module API is database-independent, all
database-specific code is contained within these drivers. Several
drivers are supplied with Qt and other drivers can be added. The
driver source code is supplied and can be used as a model for \link
#development writing your own drivers\endlink.
To build a driver plugin you need the client API that is shipped with
every Database Management System (DBMS). Most installation programs
also allow you to install "development libraries", and these are what
you need. These libraries are responsible for the low-level
communication with the DBMS.
The currently available drivers shipped with Qt are:
\list
\i \link #QMYSQL3 QMYSQL3\endlink - MySQL Driver
\i \link #QOCI8 QOCI8\endlink - Oracle Call Interface Driver, version 8 and 9
\i \link #QODBC3 QODBC3\endlink - Open Database Connectivity Driver
\i \link #QPSQL7 QPSQL7\endlink - PostgreSQL v6.x and v7.x Driver
\i \link #QTDS7 QTDS7\endlink - Sybase Adaptive Server and Microsoft SQL Server Driver
\endlist
Note that not all of the plugins are shipped with the Qt Free Edition
due to licence incompatibilities with the GPL.
\section1 Building the drivers using configure
The Qt configure script automatically detects the available client
libraries on your machine. Run "configure -help" to see what drivers
may be built. You should get an output similar to this:
\code
Possible values for <driver>: [ mysql oci odbc psql tds ]
Auto-Detected on this system: [ mysql psql ]
\endcode
Note that on Windows, the configure script doesn't currently do any
auto-detection.
Note that configure cannot detect the neccessary libraries and include
files if they are not in the standard paths, so it may be necessary to
specify these paths using the "-I" and "-L" switches. If your MySQL
include files are installed in /usr/local/mysql (or in C:\\mysql\\include
on Windows), then pass the following parameter to configure:
"-I/usr/local/mysql" (or "-I C:\\mysql\\include" for Windows).
Note that on Windows the parameter -I doesn't allow spaces in filenames,
so use the 8.3 name instead, i.e. use "C:\\progra~1\\mysql" instead of
"C:\\program files\\mysql".
Use the \c{-qt-sql-<driver>} parameter to build the database driver statically
into your Qt library or \c{-plugin-sql-<driver>} to build the driver as a
plugin. Look at the chapters below for additional information about required
libraries.
\section1 Building the plugins manually
\target QMYSQL3
\section2 QMYSQL3 - MySQL 3.x
\keyword QMYSQL3
\section3 General information
MySQL 3.x doesn't support SQL transactions by default. There are some
backends which offer this functionality. Recent versions of the MySQL
client libraries (>3.23.34) allow you to use transactions on those
modified servers.
If you have a recent client library and connect to a transaction-enabled MySQL
server, a call to the QSqlDriver::hasFeature( QSqlDriver::Transactions )
function returns TRUE and SQL transactions can be used.
You can find information about MySQL on \l http://www.mysql.com
\section3 How to build the plugin on Unix/Linux
You need the MySQL header files and as well as the shared library
"libmysqlclient.so". Depending on your Linux distribution you need to
install a package which is usually called "mysql-devel".
Tell qmake where to find the MySQL header files and shared libraries
(here it is assumed that MySQL is installed in /usr/local) and run make:
\code
cd $QTDIR/plugins/src/sqldrivers/mysql
qmake -o Makefile "INCLUDEPATH+=/usr/local/include" "LIBS+=-L/usr/local/lib -lmysqlclient" mysql.pro
make
\endcode
\section3 How to build the plugin on Windows
You need to get the MySQL installation files. Run SETUP.EXE and
choose "Custom Install". Install the "Libs & Include Files" Module.
Build the plugin as follows (here it is assumed that MySQL is
installed in C:\\MYSQL):
\code
cd %QTDIR%\plugins\src\sqldrivers\mysql
qmake -o Makefile "INCLUDEPATH+=C:\MYSQL\INCLUDE" "LIBS+=C:\MYSQL\LIB\OPT\LIBMYSQL.LIB" mysql.pro
nmake
\endcode
If you are not using a Microsoft compiler, replace "nmake" with "make" in the
statement above.
\target QOCI8
\section2 QOCI8 - Oracle Call Interface (OCI)
\keyword QOCI8
\section3 General information
The Qt OCI plugin supports both Oracle 8 and Oracle 9. After
connecting to the Oracle server, the plugin will auto-detect the
database version and enable features accordingly.
\section3 Unicode support
If the Oracle server supports Unicode, the OCI plugin will use UTF-8
encoding to communicate with the server. When accessing an Oracle 8
database, only retrieval of Unicode strings are supported. For Oracle
9 both retrieval and insertion of Unicode strings are supported.
\section3 BLOB/LOB support
Binary Large Objects (BLOBs) can be retrieved, but be aware that this
process may require a lot of memory. We recommend using separate
queries and retrieving only one BLOB at a time.
Inserting BLOB fields is limited to Oracle's maximum SQL string size.
\section3 How to build the plugin on Unix/Linux
All files required to build driver should ship with the standard Oracle
Client install.
Oracle library files required to build driver:
\list
\i libclntsh.so
\i libclntsh.so.8.0 (Oracle 8) or libclntsh.so.9.0 (Oracle 9)
\i libwtc8.so or libwtc9.so
\endlist
Tell qmake where to find the Oracle header files and shared libraries
(it is assumed that the variable $ORACLE_HOME points to the directory
where Oracle is installed) and run make:
If you are using Oracle 8:
\code
cd $QTDIR/plugins/src/sqldrivers/oci
qmake -o Makefile "INCLUDEPATH+=$ORACLE_HOME/rdbms/public $ORACLE_HOME/rdbms/demo" "LIBS+=-L$ORACLE_HOME/lib -lclntsh -lwtc8" oci.pro
make
\endcode
For Oracle version 9:
\code
cd $QTDIR/plugins/src/sqldrivers/oci
qmake -o Makefile "INCLUDEPATH+=$ORACLE_HOME/rdbms/public $ORACLE_HOME/rdbms/demo" "LIBS+=-L$ORACLE_HOME/lib -lclntsh -lwtc9" oci.pro
make
\endcode
\section3 How to build the plugin on Windows
Choosing the option "Programmer" in the Oracle Client Installer from
the Oracle Client Installation CD is sufficient to build the plugin.
Build the plugin as follows (here it is assumed that Oracle Client is
installed in C:\oracle):
\code
cd %QTDIR%\plugins\src\sqldrivers\oci
qmake -o Makefile "INCLUDEPATH+=C:\oracle\oci\include" oci.pro
nmake
\endcode
If you are not using a Microsoft compiler, replace "nmake" with "make" in the
statement above.
\target QODBC3
\section2 QODBC3 - Open Database Connectivity (ODBC)
\keyword QODBC3
\section3 General information
ODBC is a general interface that allows you to connect to multiple
DBMS using a common interface. The QODBC3 driver allows you to connect
to an ODBC driver manager and access the available data sources. Note
that you also need to install and configure ODBC drivers for the ODBC
driver manager that is installed on your system. The QODBC3 plugin
then allows you to use these data sources in your Qt project.
On Windows systems after 95 an ODBC driver manager should be installed
by default, for Unix systems there are some implementations which have to
be installed first. Note that every client that uses your application
is required to have an ODBC driver manager installed, otherwise the
QODBC3 plugin will not work.
The QODBC3 Plugin needs an ODBC compilant driver manager version 2.0 or
greater to work. Some ODBC drivers claim to be version 2.0 compilant,
but do not offer all needed functionality. The QODBC3 plugin therefore
checks whether the data source can be used after a connection has been
established and refuses to work if the check fails. If you don't
like this behaviour, you can remove the \c{#define ODBC_CHECK_DRIVER}
line from the file \c{qsql_odbc.cpp}. Do this at your own risk!
Note that some DBMSs, like Microsoft Access, does not return the
primary index for a table through ODBC. You can still use the QODBC3
driver, but you will have to manage table indices yourself (through
QSqlCursor::setPrimaryIndex()), otherwise INSERT, UPDATE and DELETE
operations will fail in data-aware Qt widgets. You will have to
comment out the \c{#define ODBC_CHECK_DRIVER} line and recompile the
driver, otherwise you will not be able to connect to a Microsoft
Access database.
\section3 How to build the plugin on Unix/Linux
It is recommended that you use unixODBC. You can find the newest
version and ODBC drivers at \l http://www.unixodbc.org.
You need the unixODBC header files and shared libraries.
Tell qmake where to find the unixODBC header files and shared libraries
(here it is assumed that unixODBC is installed in /usr/local/unixODBC)
and run make:
\code
cd $QTDIR/plugins/src/sqldrivers/odbc
qmake "INCLUDEPATH+=/usr/local/unixODBC/include" "LIBS+=-L/usr/local/unixODBC/lib -lodbc"
make
\endcode
\section3 How to build the plugin on Windows
The ODBC header and include files should already be installed in the
right directories. You just have to build the plugin as follows:
\code
cd %QTDIR%\plugins\src\sqldrivers\odbc
qmake -o Makefile odbc.pro
nmake
\endcode
If you are not using a Microsoft compiler, replace "nmake" with "make" in the
statement above.
\target QPSQL7
\section2 QPSQL7 - PostgreSQL version 6 and 7
\keyword QPSQL7
\section3 General information
The QPSQL7 driver supports both version 6 and 7 of PostgreSQL. We
recommend compiling the plugin with a recent version of the PostgreSQL
Client API (libpq) because it is more stable and still downward
compatible.
If you want to link the plugin against the libpq shipped with version
6 we recomment a recent version like PostgreSQL 6.5.3, otherwise a
connection to a version 7 server may not work.
The driver auto-detects the server version of PostgreSQL after a
connection was successful. If the server is too old or the version
information cannot be determined a warning is issued.
For more information about PostgreSQL visit \l http://www.postgresql.org.
\section 3 Unicode support
The QPSQL7 driver automatically detects whether the PostgreSQL
database you are connecting to supports Unicode or not. Unicode is
automatically used if the server supports it. Note that the driver
only supports the UTF-8 encoding. If your database uses any other
encoding, the server has to be compiled with Unicode conversion
support.
Unicode support was introduced in PostgreSQL version 7.1 and it will
only work if both the server and the client library have been compiled
with multibyte support. More information about how to set up a
multibyte enabled PostgreSQL server can be found in the PostgreSQL
Administrator Guide, Chapter 5.
\section3 BLOB support
Binary Large Objects can be fetched but not inserted. Please use the
PostgreSQL command lo_import to insert binary data into the database.
\section3 How to build the plugin on Unix/Linux
Just installing "libpq.so" and the corresponding header files is
unfortunately not sufficient. You have to get the whole source
distribution and run the configure script once (there is no need to
build it if you have already installed a binary distribution).
Tell qmake where to find the PostgreSQL header files and shared
libraries (here it is assumed that you extracted the PostgreSQL source
code in /usr/src/psql and the shared library is installed in /usr/lib)
and run make:
\code
cd $QTDIR/plugins/src/sqldrivers/psql
qmake -o Makefile "INCLUDEPATH+=/usr/src/psql/src/include /usr/src/psql/src/interfaces/libpq" "LIBS+=-L/usr/lib -lpq" psql.pro
make
\endcode
\target QTDS7
\section2 QTDS7 - Sybase Adaptive Server and Microsoft SQL Server
\keyword QTDS7
\section3 How to build the plugin on Unix/Linux
Under Unix, two libraries are available which support the TDS protocol:
- FreeTDS, a free implementation of the TDS protocol ( \l http://www.freetds.org ).
Note that FreeTDS is not yet stable, so some functionality may not work
as expected.
- Sybase Open Client, available from \l http://www.sybase.com
Note for Linux users: Get the Open Client RPM from \l http://linux.sybase.com
Regardless of which library you use, the shared object file
"libsybdb.so" is needed. Set the SYBASE environment variable to point
to the directory where you installed the client library and execute
qmake:
\code
cd $QTDIR/plugins/src/sqldrivers/tds
qmake -o Makefile "INCLUDEPATH=$SYBASE/include" "LIBS=-L$SYBASE/lib -lsybdb"
make
\endcode
\section3 How to build the plugin on Windows
You can either use the DB-Library supplied by Microsoft or the Sybase Open Client
( \l http://www.sybase.com ). You have to include NTWDBLIB.LIB
to build the plugin:
\code
cd %QTDIR%\plugins\src\sqldrivers\tds
qmake -o Makefile "LIBS+=NTWDBLIB.LIB" tds.pro
nmake
\endcode
By default the Microsoft library is used on Windows, if you want to force
the use of the Sybase Open Client, you have to define
Q_USE_SYBASE in %QTDIR%\src\sql\drivers\tds\qsql_tds.cpp.
\target troubleshooting
\section1 Troubleshooting
You should always use client libraries that have been compiled with
the same compiler as you are using for your project. If you cannot get
a source distibution to compile the client libraries yourself, you
have to make sure that the pre-compiled library is compatible with
your compiler, otherwise you will get a lot of "undefined symbols"
errors. Some compilers have tools to convert libraries, e.g. Borland
ships the tool \c{COFF2OMF.EXE} to convert libraries that have been
generated with Microsoft Visual C++.
If the compilation of a plugin succeeds but it cannot be loaded,
make sure that the following requirements are met:
\list
\i Make sure you are using a shared Qt library, you cannot use the
plugins with a static build.
\i Make sure that the environment variable QTDIR points to the right
directory. Go to the $QTDIR/plugins/sqldrivers directory and make sure
that the plugin exists in that directory.
\i Make sure that the client libraries of the DBMS are available on the
system. On Unix, run the command \c{ldd} and pass the name of the
plugin as parameter, for example \c{ldd libqsqlmysql.so}. You will
get a warning if any of the client libraries couldn't be found.
On Windows, you can use the dependency walker of Visual Studio.
\endlist
\target development
\section1 How to write your own database driver
QSqlDatabase is responsible for loading and managing database driver
plugins. When a database is added (see QSqlDatabase::addDatabase()),
the appropriate driver plugin is loaded (using QSqlDriverPlugin).
QSqlDatabase relies on the driver plugin to provide interfaces for
QSqlDriver and QSqlResult.
QSqlDriver is an abstract base class which defines the functionality
of a SQL database driver. This includes functions such as
QSqlDriver::open() and QSqlDriver::close(). QSqlDriver is responsible
for connecting to a database, establish the proper environment, etc.
In addition, QSqlDriver can create QSqlQuery objects appropriate for
the particular database API. QSqlDatabase forwards many of its
function calls directly to QSqlDriver which provides the concrete
implementation.
QSqlResult is an abstract base class which defines the functionality
of a SQL database query. This includes statements such as SELECT,
UPDATE, or ALTER TABLE. QSqlResult contains functions such as
QSqlResult::next() and QSqlResult::value(). QSqlResult is responsible
for sending queries to the database, returning result data, etc.
QSqlQuery forwards many of its function calls directly to QSqlResult
which provides the concrete implementation.
QSqlDriver and QSqlResult are closely connected. When implementing a
Qt SQL driver, both of these classes must to be subclassed and the
abstract virtual methods in each class must be implemented.
To implement a Qt SQL driver as a plugin (so that it is recognized and
loaded by the Qt library at runtime), the driver must use the
Q_EXPORT_PLUGIN macro. Please read the \link plugins-howto.html Qt
Plugin\endlink documentation for more information on this. You can
also check out how this is done in the SQL plugins that is provided
with Qt in \c{QTDIR/plugins/src/sqldrivers} and \c{QTDIR/src/sql/drivers}.
*/
|