Difference between revisions of "SQLdb Tutorial4"

From Lazarus wiki
Jump to navigationJump to search
Line 21: Line 21:
  
 
In Lazarus IDE Create a new Application and then click File --> New --> Data Module
 
In Lazarus IDE Create a new Application and then click File --> New --> Data Module
[[file:http://i1279.photobucket.com/albums/y534/Brosco65/newDM_zpsa65bdeed.jpg]]
+
[[file:newDM.jpg]]
You will be presented with a window like you selected 'New Form'. 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 addition of non-visual components.
+
You will be presented with a window like you selected 'New Form'.  
 +
[[file: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
 
EVERYONE HAS THEIR OWN WAY

Revision as of 13:41, 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 - dm1: TDataModule

On this module I 'drop' a dbConnection and a Transaction.

dm1 is used as the connection for all Querys.

I then create a dmXXXXXX for each Table or View.

To Be Continued