CopperSpice Overview  1.5.2
QODBC

General Information about the ODBC plugin

ODBC is a general interface that allows you to connect to multiple DBMSs using a common interface. The QODBC 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 QODBC plugin then allows you to use these data sources in your applications.

You should use native drivers in preference to the ODBC driver where they are available. ODBC support can be used as a fallback for compliant databases if no native drivers are available.

On Windows an ODBC driver manager should be installed by default. For Unix systems there are some implementations which must be installed first. Note that every client that uses your application is required to have an ODBC driver manager installed, otherwise the QODBC plugin will not work.

Be aware that when connecting to an ODBC datasource you must pass in the name of the ODBC datasource to the QSqlDatabase::setDatabaseName() function rather than the actual database name.

The QODBC plugin needs an ODBC compliant driver manager version 2.0 or later to work. Some ODBC drivers claim to be version 2.0 compliant, but do not offer all the necessary functionality. The QODBC 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 do not like this behavior, you can remove the #define ODBC_CHECK_DRIVER line from the file qsql_odbc.cpp. Do this at your own risk.

By default, QODBC instructs the ODBC driver to behave as an ODBC 2.x driver. However, for some driver-manager/ODBC 3.x-driver combinations (e.g., unixODBC/MaxDB ODBC), telling the ODBC driver to behave as a 2.x driver can cause the driver plugin to have unexpected behavior.

To avoid this problem, instruct the ODBC driver to behave as a 3.x driver by setting the connect option: SQL_ATTR_ODBC_VERSION=SQL_OV_ODBC3 with QSqlDatabase::setConnectOptions(). This must be done before the database connection is opened.

Note: This will affect multiple aspects of ODBC driver behavior, including the SQLSTATEs. Before setting this connect option, consult your ODBC documentation about behavior differences you can expect.

If you experience very slow access of the ODBC datasource, make sure that ODBC call tracing is turned off in the ODBC datasource manager.

Some drivers do not support scrollable cursors. In that case case only queries in forwardOnly mode can be used successfully.

ODBC Stored Procedure Support

With Microsoft SQL Server the result set returned by a stored procedure that uses the return statement, or returns multiple result sets, will be accessible only if you set the query's forward only mode to forward using QSqlQuery::setForwardOnly().

// STORED_PROC uses the return statement or returns multiple result sets
QSqlQuery query;
query.setForwardOnly(true);
query.exec("{call STORED_PROC}");

The value returned by the stored procedure's return statement is discarded.

ODBC Unicode Support

The QODBC plugin will use the Unicode API if UNICODE is defined. On Windows NT this is the default. Note that the ODBC driver and the DBMS must also support Unicode.

Some driver managers and drivers do not support UNICODE. To use the QODBC plugin with such drivers it has to be compiled with the Q_ODBC_VERSION_2 defined.

For the Oracle 9 ODBC driver (Windows), it is necessary to check "SQL_WCHAR support" in the ODBC driver manager otherwise Oracle will convert all Unicode strings to local 8-bit.