Home | All Classes | Main Classes | Annotated | Grouped Classes | Functions | ![]() |
The Qt SQL Module uses driver plugins in order to communicate with different database APIs. Since the Qt 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 writing your own drivers.
To build a driver plugin you need the client API that is shipped with every DBMS (Database Management System). 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:
The Qt configure script automatically detects the available client libraries on your machine. Run "configure -help" to see what drivers may be built.
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 "-IC:\mysql\include" for Windows).
Note that 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".
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 "COFF2OMF.EXE" to convert libraries that have been generated with Microsoft Visual C++.
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 http://www.mysql.com
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:
cd $QTDIR/plugins/src/sqldrivers/mysql qmake -o Makefile "INCLUDEPATH+=/usr/local/include" "LIBS+=-L/usr/local/lib -lmysqlclient" mysql.pro make
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):
cd %QTDIR%\plugins\src\sqldrivers\mysql qmake -o Makefile "INCLUDEPATH+=C:\\MYSQL\\INCLUDE" "LIBS+=C:\\MYSQL\\LIB\\OPT\\LIBMYSQL.LIB" mysql.pro nmake
If you are not using a Microsoft compiler, replace "nmake" with "make" in the statement above.
All files required to build driver should ship with the standard Oracle install. For Linux, it may be possible to copy headers from another platform's install.
Oracle library files required to build driver:
Oracle header files required to build driver:
Tell qmake where to find the Oracle header files and shared libraries (here it is assumed that Oracle is installed in /usr/local) and run make:
cd $QTDIR/plugins/src/sqldrivers/oci qmake -o Makefile "INCLUDEPATH+=/usr/local/include" "LIBS+=-L/usr/local/lib -lclntsh -lwtc8" oci.pro make
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):
cd %QTDIR%\plugins\src\sqldrivers\oci qmake -o Makefile "INCLUDEPATH+=C:\\oracle\\oci\\include" oci.pro nmake
If you are not using a Microsoft compiler, replace "nmake" with "make" in the statement above.
ODBC (Open Database Connectivity) 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 his datasources. 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 #define ODBC_CHECK_DRIVER line from the file qsql_odbc.cpp. Do this at your own risk!
It is recommended that you use unixODBC. You can find the newest version and ODBC drivers at 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:
cd $QTDIR/plugins/src/sqldrivers/odbc qmake "INCLUDEPATH+=/usr/local/unixODBC/include" "LIBS+=-L/usr/local/unixODBC/lib -lodbc" make
The ODBC header and include files should already be installed in the right directories. You just have to build the plugin as follows:
cd %QTDIR%\\plugins\\src\\sqldrivers\\odbc qmake -o Makefile odbc.pro nmake
If you are not using a Microsoft compiler, replace "nmake" with "make" in the statement above.
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 http://www.postgresql.org.
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:
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
Under Unix, two libraries are available which support the TDS protocol:
- FreeTDS, a free implementation of the TDS protocol ( http://www.freetds.org ). Note that FreeTDS is not yet stable, so some functionality may not work as expected.
- Sybase Open Client, available from http://www.sybase.com Note for Linux users: Get the Open Client RPM from 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:
cd $QTDIR/plugins/src/sqldrivers/tds qmake -o Makefile "INCLUDEPATH=$SYBASE/include" "LIBS=-L$SYBASE/lib -lsybdb" make
You can either use the DB-Library supplied by Microsoft or the Sybase Open Client ( http://www.sybase.com ). You have to include NTWDBLIB.LIB to build the plugin:
cd %QTDIR%\\plugins\\src\\sqldrivers\\tds qmake -o Makefile "LIBS+=NTWDBLIB.LIB" tds.pro nmake
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.
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 QLibrary).
QSqlDatabase relies on the driver plugin to provide 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 export a
QSqlDriverFactoryInterface, which is an interface that is capable of
producing QSqlDriver objects (see QSqlDriverFactory::create()). There
are several macros available to assist in creating such a plugin. See
the source from the examples provided with Qt in
QTDIR/plugins/src/sqldrivers and QTDIR/src/sql/drivers
for more information.
interfaces for QSqlDriver and
QSqlResult.
Copyright © 2001
Trolltech Trademarks