Difference between revisions of "SQLdb Tutorial4"

From Lazarus wiki
Jump to navigationJump to search
Line 36: Line 36:
  
 
Unit: DataModule1   
 
Unit: DataModule1   
 +
On this module I 'drop' a dbConnection and a Transaction.
  
On this module I 'drop' a dbConnection and a Transaction.
+
[[file:conn.jpg]]
  
 
DataModule1 is used as the connection for all Querys.
 
DataModule1 is used as the connection for all Querys.

Revision as of 13:58, 6 May 2013

Category:DataModules

INTRODUCTION

This tutorial is an attempt to demonstrate the use of Lazarus DataModules to isolate the Data Access components of a project from the program logic associated with the access. Such isolation makes program maintenance and debugging easier. The tutorial was developed using Windows 7 and SQLite3 as the database, with Lazarus 1.0.8 with FPC 2.6.2, however it should work with earlier versions. Similarly, other DBMS and OpSystems should require minimal change.

WHY USE DATAMODULES?

Simple - after following the Lazarus Tutorials: Tut1 Tut2 Tut3, Developing a 'real' application becomes harder. 'Form1' grows to an exponential size handling the different Events and Database Queries. By 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 Datamodule for each Table or View.

The sample shown here uses just 2 Tables with simple queries, but to allow it to be expanded to possibly include more options with each Table.


GETTING STARTED

In Lazarus IDE Create a new Application and then click File --> New --> Data Module

newDM.jpg

You will be presented with a window like 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 non-visual components.


EVERYONE HAS THEIR OWN WAY

Your use of DataModules will vary to suit your own needs. but as a generalisation, I have at least 2 DataModules:

Unit: DataModule1 On this module I 'drop' a dbConnection and a Transaction.

conn.jpg

DataModule1 is used as the connection for all Querys.

I then create a DataModulen for each Table or View.

To Be Continued