📄 sql-driver.html
字号:
qmake -o Makefile "INCLUDEPATH+=$ORACLE_HOME/rdbms/public $ORACLE_HOME/rdbms/demo" "LIBS+=-L$ORACLE_HOME/lib -lclntsh -lwtc9" oci.promake</pre> <p> <h4> How to build the plugin on Windows</h4><a name="3-2-5"></a><p> Choosing the option "Programmer" in the Oracle Client Installer fromthe Oracle Client Installation CD is sufficient to build the plugin.<p> Build the plugin as follows (here it is assumed that Oracle Client isinstalled in C:\oracle):<p> <pre>set INCLUDE=%INCLUDE%;c:\oracle\oci\includeset LIB=%LIB%;c:\oracle\oci\lib\msvccd %QTDIR%\plugins\src\sqldrivers\ociqmake -o Makefile oci.pronmake</pre> <p> When you run your application you will also need to add the oci.dllpath to your PATH environment variable:<p> <pre>set PATH=%PATH%;c:\oracle\bin</pre> <p> If you are not using a Microsoft compiler, replace "nmake" with "make"in the statement above.<p> <a name="QODBC3"></a><h3> QODBC3 - Open Database Connectivity (ODBC)</h3><a name="3-3"></a><p> <!-- index QODBC3 --><a name="QODBC3"></a><p> <h4> General information</h4><a name="3-3-1"></a><p> ODBC is a general interface that allows you to connect to multipleDBMS using a common interface. The QODBC3 driver allows you to connectto an ODBC driver manager and access the available data sources. Notethat you also need to install and configure ODBC drivers for the ODBCdriver manager that is installed on your system. The QODBC3 pluginthen allows you to use these data sources in your Qt project.<p> On Windows systems after 95 an ODBC driver manager should be installedby default, for Unix systems there are some implementations which have tobe installed first. Note that every client that uses your applicationis required to have an ODBC driver manager installed, otherwise theQODBC3 plugin will not work.<p> Be aware that when connecting to an ODBC datasource you must pass inthe name of the ODBC datasource to the <a href="qsqldatabase.html#setDatabaseName">QSqlDatabase::setDatabaseName</a>()function - not the actual database name.<p> The QODBC3 Plugin needs an ODBC compliant driver manager version 2.0 orgreater to work. Some ODBC drivers claim to be version 2.0 compliant,but do not offer all needed functionality. The QODBC3 plugin thereforechecks whether the data source can be used after a connection has beenestablished and refuses to work if the check fails. If you don'tlike this behaviour, you can remove the <tt>#define ODBC_CHECK_DRIVER</tt>line from the file <tt>qsql_odbc.cpp</tt>. Do this at your own risk!<p> Note that some DBMSs, like Microsoft Access, does not return theprimary index for a table through ODBC. You can still use the QODBC3driver, but you will have to manage table indices yourself (through<a href="qsqlcursor.html#setPrimaryIndex">QSqlCursor::setPrimaryIndex</a>()), otherwise INSERT, UPDATE and DELETEoperations will fail in data-aware Qt widgets. You will have tocomment out the <tt>#define ODBC_CHECK_DRIVER</tt> line and recompile thedriver, otherwise you will not be able to connect to a MicrosoftAccess database.<p> <h4> How to build the plugin on Unix/Linux</h4><a name="3-3-2"></a><p> It is recommended that you use unixODBC. You can find the newestversion and ODBC drivers at <a href="http://www.unixodbc.org">http://www.unixodbc.org</a>.You need the unixODBC header files and shared libraries.<p> 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:<p> <pre>cd $QTDIR/plugins/src/sqldrivers/odbcqmake "INCLUDEPATH+=/usr/local/unixODBC/include" "LIBS+=-L/usr/local/unixODBC/lib -lodbc"make</pre> <p> <h4> How to build the plugin on Windows</h4><a name="3-3-3"></a><p> The ODBC header and include files should already be installed in theright directories. You just have to build the plugin as follows:<p> <pre>cd %QTDIR%\plugins\src\sqldrivers\odbcqmake -o Makefile odbc.pronmake</pre> <p> If you are not using a Microsoft compiler, replace "nmake" with "make" in thestatement above.<p> <a name="QPSQL7"></a><h3> QPSQL7 - PostgreSQL version 6 and 7</h3><a name="3-4"></a><p> <!-- index QPSQL7 --><a name="QPSQL7"></a><p> <h4> General information</h4><a name="3-4-1"></a><p> The QPSQL7 driver supports both version 6 and 7 of PostgreSQL. Werecommend compiling the plugin with a recent version of the PostgreSQLClient API (libpq) because it is more stable and still downwardcompatible.<p> If you want to link the plugin against the libpq shipped with version6 we recomment a recent version like PostgreSQL 6.5.3, otherwise aconnection to a version 7 server may not work.<p> The driver auto-detects the server version of PostgreSQL after aconnection was successful. If the server is too old or the versioninformation cannot be determined a warning is issued.<p> For more information about PostgreSQL visit <a href="http://www.postgresql.org">http://www.postgresql.org</a>.<p> <h4> Unicode support</h4><a name="3-4-2"></a><p> The QPSQL7 driver automatically detects whether the PostgreSQLdatabase you are connecting to supports Unicode or not. Unicode isautomatically used if the server supports it. Note that the driveronly supports the UTF-8 encoding. If your database uses any otherencoding, the server has to be compiled with Unicode conversionsupport.<p> Unicode support was introduced in PostgreSQL version 7.1 and it willonly work if both the server and the client library have been compiledwith multibyte support. More information about how to set up amultibyte enabled PostgreSQL server can be found in the PostgreSQLAdministrator Guide, Chapter 5.<p> <h4> BLOB support</h4><a name="3-4-3"></a><p> Binary Large Objects can be fetched but not inserted. Please use thePostgreSQL command lo_import to insert binary data into the database.<p> <h4> How to build the plugin on Unix/Linux</h4><a name="3-4-4"></a><p> Just installing "libpq.so" and the corresponding header files isunfortunately not sufficient. You have to get the whole sourcedistribution and run the configure script once (there is no need tobuild it if you have already installed a binary distribution).<p> Tell qmake where to find the PostgreSQL header files and sharedlibraries (here it is assumed that you extracted the PostgreSQL sourcecode in /usr/src/psql and the shared library is installed in /usr/lib)and run make:<p> <pre>cd $QTDIR/plugins/src/sqldrivers/psqlqmake -o Makefile "INCLUDEPATH+=/usr/src/psql/src/include /usr/src/psql/src/interfaces/libpq" "LIBS+=-L/usr/lib -lpq" psql.promake</pre> <p> <a name="QTDS7"></a><h3> QTDS7 - Sybase Adaptive Server and Microsoft SQL Server</h3><a name="3-5"></a><p> <!-- index QTDS7 --><a name="QTDS7"></a><p> <h4> How to build the plugin on Unix/Linux</h4><a name="3-5-1"></a><p> Under Unix, two libraries are available which support the TDS protocol:<p> - FreeTDS, a free implementation of the TDS protocol ( <a href="http://www.freetds.org">http://www.freetds.org</a> ).Note that FreeTDS is not yet stable, so some functionality may not workas expected.<p> - Sybase Open Client, available from <a href="http://www.sybase.com">http://www.sybase.com</a>Note for Linux users: Get the Open Client RPM from <a href="http://linux.sybase.com">http://linux.sybase.com</a><p> Regardless of which library you use, the shared object file"libsybdb.so" is needed. Set the SYBASE environment variable to pointto the directory where you installed the client library and executeqmake:<p> <pre>cd $QTDIR/plugins/src/sqldrivers/tdsqmake -o Makefile "INCLUDEPATH=$SYBASE/include" "LIBS=-L$SYBASE/lib -lsybdb"make</pre> <p> <h4> How to build the plugin on Windows</h4><a name="3-5-2"></a><p> You can either use the DB-Library supplied by Microsoft or the Sybase Open Client( <a href="http://www.sybase.com">http://www.sybase.com</a> ). You have to include NTWDBLIB.LIBto build the plugin:<p> <pre>cd %QTDIR%\plugins\src\sqldrivers\tdsqmake -o Makefile "LIBS+=NTWDBLIB.LIB" tds.pronmake</pre> <p> By default the Microsoft library is used on Windows, if you want to forcethe use of the Sybase Open Client, you have to defineQ_USE_SYBASE in %QTDIR%\src\sql\drivers\tds\qsql_tds.cpp.<p> <a name="troubleshooting"></a><h2> Troubleshooting</h2><a name="4"></a><p> You should always use client libraries that have been compiled withthe same compiler as you are using for your project. If you cannot geta source distibution to compile the client libraries yourself, youhave to make sure that the pre-compiled library is compatible withyour compiler, otherwise you will get a lot of "undefined symbols"errors. Some compilers have tools to convert libraries, e.g. Borlandships the tool <tt>COFF2OMF.EXE</tt> to convert libraries that have beengenerated with Microsoft Visual C++.<p> If the compilation of a plugin succeeds but it cannot be loaded,make sure that the following requirements are met:<p> <ul><li> Make sure you are using a shared Qt library, you cannot use theplugins with a static build.<li> Make sure that the environment variable QTDIR points to the rightdirectory. Go to the $QTDIR/plugins/sqldrivers directory and make surethat the plugin exists in that directory.<li> Make sure that the client libraries of the DBMS are available on the system. On Unix, run the command <tt>ldd</tt> and pass the name of theplugin as parameter, for example <tt>ldd libqsqlmysql.so</tt>. You willget a warning if any of the client libraries couldn't be found. On Windows, you can use the dependency walker of Visual Studio.</ul><p> <a name="development"></a><h2> How to write your own database driver</h2><a name="5"></a><p> <a href="qsqldatabase.html">QSqlDatabase</a> is responsible for loading and managing database driverplugins. When a database is added (see <a href="qsqldatabase.html#addDatabase">QSqlDatabase::addDatabase</a>()),the appropriate driver plugin is loaded (using <a href="qsqldriverplugin.html">QSqlDriverPlugin</a>).QSqlDatabase relies on the driver plugin to provide interfaces for<a href="qsqldriver.html">QSqlDriver</a> and <a href="qsqlresult.html">QSqlResult</a>.<p> QSqlDriver is an abstract base class which defines the functionalityof a SQL database driver. This includes functions such as<a href="qsqldriver.html#open">QSqlDriver::open</a>() and <a href="qsqldriver.html#close">QSqlDriver::close</a>(). QSqlDriver is responsiblefor connecting to a database, establish the proper environment, etc.In addition, QSqlDriver can create <a href="qsqlquery.html">QSqlQuery</a> objects appropriate forthe particular database API. QSqlDatabase forwards many of itsfunction calls directly to QSqlDriver which provides the concreteimplementation.<p> QSqlResult is an abstract base class which defines the functionalityof a SQL database query. This includes statements such as SELECT,UPDATE, or ALTER TABLE. QSqlResult contains functions such asQSqlResult::next() and QSqlResult::value(). QSqlResult is responsiblefor sending queries to the database, returning result data, etc.QSqlQuery forwards many of its function calls directly to <a href="qsqlresult.html">QSqlResult</a>which provides the concrete implementation.<p> <a href="qsqldriver.html">QSqlDriver</a> and QSqlResult are closely connected. When implementing aQt SQL driver, both of these classes must to be subclassed and theabstract virtual methods in each class must be implemented. <p> To implement a Qt SQL driver as a plugin (so that it is recognized andloaded by the Qt library at runtime), the driver must use theQ_EXPORT_PLUGIN macro. Please read the <a href="plugins-howto.html">QtPlugin</a> documentation for more information on this. You canalso check out how this is done in the SQL plugins that is providedwith Qt in <tt>QTDIR/plugins/src/sqldrivers</tt> and <tt>QTDIR/src/sql/drivers</tt>.<p> <!-- eof --><p><address><hr><div align=center><table width=100% cellspacing=0 border=0><tr><td>Copyright © 2002 <a href="http://www.trolltech.com">Trolltech</a><td><a href="http://www.trolltech.com/trademarks.html">Trademarks</a><td align=right><div align=right>Qt version 3.0.5</div></table></div></address></body></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -