Lazarus - Chapter 12

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 12.1.1 Database access 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. 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

Transcript of Lazarus - Chapter 12

Page 1: Lazarus - Chapter 12

Chapter 12

Database Access

by Michael Van Canneyt

Database accessMany applications (by no means only business applications) display andmanipulate data coming from various sources. The data may come from adatabase server or from regular files, and is commonly stored on the local harddisk.

12.1 Architecture

12.1.1 Database access

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.

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

Page 2: Lazarus - Chapter 12

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 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 ( descendants) and the presentation layer

(various visual components, such as ). The two categories are connected

through a instance. This component connects the presentation components

to the database access layer. The Component Palette's 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 data-

presentation 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 descendants on a form and connecting them to the

database connection component created in step 1.

3. Dropping a component on the form for each descendant

created in step 2, and connecting it to the corresponding 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 instance created

in step 3.

TDataset

TDatasetTDBGrid

TDatasourceData Access

TDataset

TDatasource TDatasetTDataset

TDatasource

Figure 12.1: The database access layers in Lazarus

Local access (Free Pascal) Interface to external C/S databases Dataset

Interface

DLL

Database layer

Dataset

abstraction

layer

Master/Detail

connection

TDatasetSQLDBTMem

DatasetTSDF

DatasetTDBF

dbf sdf memdata odbc32 fbclient mysqlclient

TDatasource

TDBGridTDB

NavigatorTDBedit TDBImage TDBMemo TDBTest

Datasource

Event-driven

communication

layer

Dependency

on other

database tables

GUI controls

Application

layer

12.1.1 Database access

Lazarus - the complete guide652