SQLdb Tutorial4/fr

From Free Pascal wiki
Revision as of 08:05, 3 April 2017 by E-ric (talk | contribs)
Jump to navigationJump to search

English (en) français (fr) 日本語 (ja)

Portail de la base de données

Références:

Tutoriels/articles pratiques :

Bases de données

Advantage - MySQL - MSSQL - Postgres - Interbase - Firebird - Oracle - ODBC - Paradox - SQLite - dBASE - MS Access - Zeos

Introduction

Ce tutoriel est une tentative de démonstration de l'emploi des Modules de données de Lazarus pour isoler les composants d'accès aux bases de données d'un projet de la logique associées aux accès. Une telle isolation rend la maintenance du programme et son débogage plus facile.

Le tutoriel a été développé en utilisant Windows 7 et SQLite3 comme base de données avec Lazarus 1.0.8 et FPC 2.6.2 ; toutefois, cela devrait fonctionner avec des versions antérieures. De la même façon, d'autres SGBD ou systèmes d'exploitation devraient nécessiter peu de modification, si jamais il y en a.

Pourquoi utiliser des modules de données ?

Simple - after following the Lazarus Tutorials:

developing a 'real' application becomes harder. 'Form1' grows to an exponential size handling the different Events and Database Queries.

Isolating each table access into a single datamodule makes debugging and maintenance so much easier. An application may have any number of datamodules - a small application with just one or 2 tables can probably suffice with just one Datamodule - a larger application could probably benefit from having a data module for each table or view.

The sample shown here uses just 2 tables with simple queries, but it can be expanded to include more options with each table.

Commençons

In the Lazarus IDE, create a new Application and then click File --> New --> Data Module

newDM.jpg

You will be presented with a window as if you selected 'New Form':

dm1.jpg

The difference is this window/form will only accept non-visual components. In fact, look at your Component Palette: it has been greatly reduced to only allow selection of ONLY non-visual components.

Chacun a sa propre façon de faire

Your use of data modules will vary to suit your own needs. but as an example, I have at least 2 data modules:

Unit: DataModule1 On this module I 'drop' a T*Connection and a TSQLTransaction.

conn.jpg

DataModule1 is used as the connection for all queries.

I then create a DataModuleN for each table or view.

dm2.jpg

Each DataModuleN will need to have the DataModule1 unit (unit2 in this example) added to the USES clause to connect to the database.

From here everything is the same as stated in SQLdb Tutorial1. The components are connected in the same way and access is identical.

Plus d'utilisations des modules de données

Additional components

In this example, DataModule1 had nothing more than a Connection and Transaction, but in a 'real' application, this container would typically also hold global non-visual components to be used by the application.

For example, Load and Save INI settings, TOpenDialog, TSaveDialog, etc. The concept here is to isolate data access from the business logic of an application. A change in data source for any application is never a minimal task, but having the datasources isolated will make the change much easier.

Débogage

Debugging a program is also a difficult task. By separating data access and business logic, the code to be viewed is halved. Data access and business logic can be tested separately to at least halve the problem.

The importance of the DataModule will become even more obvious when developing other applications using the same database and tables. The data module can of course be reused in the new application.

Voir aussi