Databases

From Free Pascal wiki
Revision as of 04:58, 27 February 2005 by Tonymaro (talk | contribs)
Jump to navigationJump to search

Work in progress..

Introduction

This page is about creating databases applications with Lazarus using the DB-unit. In the DB unit all the basic functionality for working with databases is implemented, but to be usefull they need components which are dependent on the database you want to use.

The first part gives a short introduction on the several database-dependent components and how to install them, the second part is a short course in working with the DB-unit and the last part gives specific information about several supported databases.

First Part: General information about supported databases

Introduction

The databases you can use with Lazarus, (actually with FPC, but that's not so important for the rest of the story) can be divided into two categories: The databases which are completely 'internal' and are written in Freepascal, and the 'external' databases to which connections are made with client-libraries. To use these client-libraries the appropiate bindings must be available. Most client-libraries come only with bindings for C, but FPC provides several packages with the pascal-bindings for database-clients. The supported databases are listed in table 1. (fpc 1.9.7 and above)

Table 1

Database Package name Need Client Lib? Supported versions Supported platforms
DBase tDbf No III+, IV, VII All
FoxPro tDbf No - All
In memory memds No - All
Textfiles sdf No - All
Interbase IBase Yes 4 - 6 i386: Linux, Win32
Firebird IBase Yes 1 - 1.5 i386: Linux, Win32
SQLite SQLite Yes - -
PostgreSQL postgres Yes 6.6 - 8 i386: Linux, Win32
Oracle Oracle Yes - -
ODBC ODBC Yes - i386: Win32
MySQL MySQL Yes 3 - 4.0 i386: Linux, Win32

The bindings to the database-clients

If you want to use one of the databases-client libraries, those libraries has to be installed. Not only on the computer where you're programming on, but also on the computes where the application must run at. Not that some databases (in particular MySQL) only work if the bindings which are compiled in the application are from the same version as those of the installed libraries. How to install those libraries (.so files on *nix systems, and .dll's on windows) you can find on the website of the database-developers. The binding units can be found in the packages/base directory in the fpc-sources. They basically consist of the client-api calls like mysql_connect_database, which are completely different for each database. It is possible to write database applications using these units, but it is most often far more work and bug-sensitive then using the DB-unit Lazarus components.

Most of these bindings-packages are hard-linked to the client-libraries. This means that if the application is compiled with one of these units in it, the whole application can not be linked if the client libraries are not available on the workstation. This means that if you do not have installed - by example - MySQL on your computer, and you are using the mysql4.pp unit in your program. The program woud not link. If you succeed to compile the program on a computer with has MySQL installed, it still won't even start on any other workstation without the appropiate MySQL-libraries.

To avoid such problems some of the packages are also able to link dynamical to the libraries. Before any calls to those libraries can be made, the unit has to be 'initialised'. This initialisation failes if the database-client isn't installed on the coputer. If the program is ready using the client-library, the unit has to be 'released'.

The database-components in Lazarus

Second part: Using the components in the DB unit

This section gives a short tutorial which holds for all the databases based on the db unit. Before you can use the db-unit, a database-component has to be installed. As an example tDbf is used, but every TDataset descendent could be used.

Step one: Create a new Lazarus aplication

Step two: Drop a tDBF component on your form. (Or any other TDataset descendent, offcourse) This TDataset-(descendent) is to access the data in a table or query.

Step three: Drop a tDBF and a TDatasource component on your form. (Or any other TDataset descendent, offcourse) The TDataset is to access the data in a table or query, the TDatasource is the 'link' between the TDataset and the visible components on the form, like a TDBEdit. Link the dataset and the datasource to each other by setting the 'Dataset' property of the TDatasource to the TDataset. (In this case: Dbf1)



--Loesje 14:39, 10 Feb 2005 (PST)