The ODBCConn unit implements an SQLdb connection to ODBC data sources.
ODBC (Open Database Connectivity) is a technology that allows one to connect to a whole variety of databases using a single API, the ODBC API.
There are ODBC implementations for various platforms and there are drivers for most Database Management Systems. Official ODBC documentation can be found at the MSDN ODBC site.
FreePascal ships with ODBC headers; they are in the
TODBCConnection is an
TSQLConnection descendant providing a nice OOP wrapper for ODBC using the SQLdb framework.
In Lazarus, you can find the
TODBCConnection component on the SQLdb component tab.
You can also use the
TODBCConnection component in your code by adding
ODBCConn to your uses clause.
What has been implemented:
- executing queries and retrieving result sets
- most field types, including blobs
- query parameters (string and integer types)
- preparing queries
- UpdateIndexDefs (so you can use ApplyUpdates); patch submitted to fpc-devel, May 2, 2007
What is left to be implemented:
- proper transaction support; currently each connection corresponds to one transaction
- some field types
- SQL_TYPE_UTC* (these are mentioned in the ODBC docs, but seem not to be used in implementations)
- SQL_INTERVAL_* (what would be the corresponding TFieldType?)
- SQL_GUID (TGUIDField was not implemented, until recently)
Why use ODBC?
FreePascal ships with components for connecting to several databases, such as MySQL, PostGreSQL, Firebird, Oracle, etc. For those databases missing from the list, like MS Access, ODBC might be an outcome. Actually, the TODBCConnection component was developed originally to circumvent the strict MySQL license for applications that are not GPLed or do not obey MySQL AB's FLOSS exception.
Installing ODBC and ODBC Drivers
Before you can connect to your database using ODBC, you need to install
- an ODBC Driver Manager
- an ODBC driver specific to the DBMS you want to connect to
The ODBC Driver Manager
Windows has an ODBC Driver Manager built in. Its configuration is controlled by
- the Data Sources (ODBC) item under the Administrative Tools in the Control Panel (Win 2000, XP)
- the ODBC Data Sources (32-bit) item in the Control Panel (Win 9x, ME).
- the ODBC item in the Control Panel (Win NT)
For Debian, you can install the unixodbc package:
aptitude install unixodbc aptitude install unixodbc-bin # if you want some GUI tools
odbcsqldyn unit, and hence
odbcconn, will search for a file called
It will not accept a file named like
Debian's unixodbc package does not create a symlink with the name
/usr/lib/libodbc.so; you must either
- create the link yourself:
ln -s libodbc.so.1 /usr/lib/libodbc.so, or
- install the unixodbc-dev package, which does create the symlink.
If you installed the unixodbc-bin package, you can run the
ODBCConfig program to configure ODBC drivers and DSNs.
For Ubuntu, follow the instruction for Debian. Note: the unixodbc-bin package might not be available from the default package repository.
Connecting to an ODBC data source
The parameters for connecting to an ODBC data source are described in a connection string.
This is a string of the form
TODBCConnection provides a wrapper around this connection string.
Some of its properties are mapped to name-value pairs in the connection string, and custom parameters can be specified in the
Params property (which is a
The ODBC connection string
Before going to the details of this wrapper, you must first have a basic understanding of how an ODBC data source is identified. First of all, an ODBC driver manager provides two ways to make a shortcut for a set of parameters:
- DSN (DataSource Name): a system or user defined data source, identified by a (unique) name. DSNs can be configured using the ODBC Data Source Administrator or by manually editing the
odbc.inifile (or registry).
- File DSN: a file which contains connection parameters. An ODBC Data Source Administrator usually allows you to create File DSNs from the GUI.
The parameters in a DSN or File DSN can always be combined with additional parameters in the connection string, for example to specify a password.
The ODBC specification defines a few parameters that can be used in a connection string:
- Two special parameters,
FILEDSN, allow one to select a set of pre-defined parameters, as described above.
DRIVERspecifies which ODBC driver to use. Obviously, this is a very important parameter.
PWDparameters are used to supply a username and password.
All other parameters are driver dependent. Please refer to the documentation of the specific driver to learn more about available parameters and their names!
The following table describes the mapping to
TODBCConnection properties to ODBC connection string parameters:
|Property||Type||Connection string parameter|
||none; there is no corresponding parameter in the ODBC standard|
||Use this to specify custom parameters. Each item must be of the form |
TODBCConnection will take care of escaping parameter values in the connection string (when required).
LoginPrompt boolean property is not implemented yet.
It would require finding the correct window handle, so a driver can show a GUI dialog to specify parameters.
Note that this is not controlled by the connection string, but rather by the last parameter to the ODBC API function
In this section, examples are given of connecting to certain DBMSs using their specific ODBC drivers. The syntax used in the examples is as in *.lfm files. It should be straight-forward to interpret them.
Connecting to MySQL
For a reference of supported parameters by the MyODBC driver, see .
The driver name differs a bit depending on the platform and MyODBC version; examples are:
Driver = 'MySQL' (Unix) Driver = 'MySQL ODBC 3.51 Driver' (Windows) Driver = 'MySQL Connector/ODBC v5' (Windows)
UserName = 'myUsername' Password = 'myPassword' Params.Strings = ( 'SERVER=example.com', 'PORT=3306', 'DATABASE=myDatabase' )
Connecting to MS Access
On Windows, you can use
Driver = 'Microsoft Access Driver (*.mdb)' Params.Strings = ( 'DBQ=C:\path\to\my\database.mdb' )
Note: Le pilote MS Access ODBC driver ne semble pas fonctionner correctement avec les Clés Primaires. Si vous avez une exception en raport, vous pouvez faire
SQLQuery.UsePrimaryKeyAsKey:=false; pour l'éviter, en attendant que le problème soit résolu.
Les noms de fichiers avec caractères accentués ne semblent pas acceptés.
There are several facilities in ODBC that are helpful when debugging your ODBC application.
Each ODBC API call also returns a succes code; if there is an error, more information can be retrieved using calls to
SQLGetDiagRec. (For the sake of completeness: diagnostic records are also available when an API call returns
TODBCConnection checks the return code of each ODBC call and contructs an
message of this exception consists of:
- a message identifying what the
TODBCConnectionwas doing when the error occurred
- the return code of the ODBC API call (e.g.
- a number of diagnostic records (obtained using
SQLGetDiagRec); each record consists of three fields:
- a 5-character error code identifying the error
- a 'native error code'
- a message describing the error
Note: if you get a 'Function sequence error' in the finalization section of the
ODBCConn unit, then you probably did not properly clean up all you queries and connections.
Most ODBC managers have a tracing option in which all ODBC API calls are logged to a trace log. This can be very useful for debugging an ODBC application. The ODBC Data Source Administration GUI of both Windows and unixODBC have a tab where you can configure the tracing option.
Of course the trace log is mainly useful for developers that are familiar with the ODBC API, but it can help to identify the problem. Also, you can attach a trace log if you report a problem to the bug tracker.