Databases

From Lazarus wiki
Revision as of 11:21, 16 January 2006 by Neli (talk | contribs) (database overview table)

Deutsch (de) English (en) español (es) français (fr) italiano (it) português (pt) русский (ru) 中文(中国大陆)‎ (zh_CN)

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)

Database Technologies Overview

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

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. Note 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 which has MySQL installed, it still won't even start on any other workstation without the appropriate 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 'initialized'. This initialization fails if the database-client isn't installed on the computer. 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 (or any other TDataset descendent, of course) on your form. This TDataset-(descendent) is to access the data in a table or query.

Step three: Drop a TDatasource component on your form. 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)