You are on page 1of 2

Chapter 12 Database Access

by Michael Van Canneyt Database access Many applications (by no means only business applications) display and manipulate data coming from various sources. The data may come from a database server or from regular files, and is commonly stored on the local hard disk.

12.1 Architecture

The Lazarus IDE and LCL do not provide their own database engine, nor do they offer a complete database management system. All database systems have their peculiarities, and most come with their own specialised tools to create and maintain their databases. General-purpose tools cannot hope to match that specialised functionality. Therefore when you develop database applications in Lazarus, you will also need whatever native database tools are available (for instance mysqladmin when working with MySQL, or FlameRobin when working with Firebird). Instead of attempting to duplicate what the database engines and tools do best, Lazarus and its LCL offer two kinds of support for creating database applications, which focus on connecting to as many database systems as possible.

12.1.1 Database access

Access to different data sources in Lazarus is achieved through a unified database interface, provided in the form of components. Most standard controls available for writing a user interface have a corresponding data-aware version. The data-aware control knows how to get data from the data interface, and how to communicate any data changes back to the database. Thanks to this unified architecture, you don't have to change your user interface to develop it as a database application which can edit an underlying database. The database layer is not dependent on the visual controls, and therefore it can also be used in service or web applications. The database interface is written in pure Object Pascal and is therefore fully accessible and Lazarus contains ready-made components for access to many different database types and file formats, regardless of whether they are open source or not: CSV data (comma-separated values), DBF files, Firebird and Interbase databases, MySQL databases (version 4.0, 4.1 or 5), Oracle databases, PostGreSQL databases, embedded SQLite databases and any other database for which an ODBC driver is available. Additionally, Lazarus offers components to keep data in memory. The advantage of this Object Pascal approach is that data can be shown in the IDE, where it can be inspected at design time. You don't have to run your application just to view the data.

Lazarus - the complete guide

651

12.1.1 Database access

Alternatively, there are database access interfaces from third parties. For instance, the Advantage Database Server, the ZeosLib components for access to different databases, and several Firebird/Interbase components from UIB (whose main components are not based on the Lazarus TDataset class). FBLib also offers access to Firebird/Interbase databases. In theory, anyone can develop components for database access. Components designed for database connectivity in the LCL (or FCL) fit into one of two distinct categories: the data access layer (TDataset descendants) and the presentation layer (various visual components, such as TDBGrid). The two categories are connected through a TDatasource instance. This component connects the presentation components to the database access layer. The Component Palette's Data Access page contains a series of components for connecting to a database in order to send data to (or receive data from) that database. The visual components that are the source of the data are the subject of this chapter. To develop a GUI database application you will use both data-access and datapresentation components. This usually involves three (or four) steps: 1. Dropping a database connection component on a form. (You can skip this step for most flat-file databases). 2. Dropping one or more TDataset descendants on a form and connecting them to the database connection component created in step 1. 3. Dropping a TDatasource component on the form for each TDataset descendant created in step 2, and connecting it to the corresponding TDataset descendant. 4. Finally, to see the data, you place as many data-aware controls on the form as are needed. Each of them must be connected to a TDatasource instance created in step 3.
Local access (Free Pascal) Interface DLL Database layer dbf sdf memdata Interface to external C/S databases Dataset odbc32 fbclient mysqlclient

Dataset abstraction layer

TDBF

TSDF Dataset

TMem Dataset

SQLDB

TDataset

Datasource Event-driven communication layer GUI controls Application layer

TDatasource

Master/Detail connection

Dependency on other database tables

TDBGrid

TDB Navigator

TDBedit

TDBImage

TDBMemo

TDBTest

Figure 12.1: The database access layers in Lazarus 652

Lazarus - the complete guide