Sign in Join
Wiki copytemporary

SQLdb Tutorial4: what changed

Revision 1 to 2. Removed lines have a minus sign; added lines have a plus sign.

OldNewLine
11## Introduction
22
33This tutorial is an attempt to demonstrate the use of [[Data module|Lazarus Data Modules]] 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.
44
55The 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 Operating Systems should require minimal change, if any.
66
77## Why use datamodules?
88
99Simple - after following the Lazarus Tutorials:
1010
1111- [[SQLdb Tutorial0]]
1212- [[SQLdb Tutorial1]]
1313- [[SQLdb Tutorial2]]
1414- [[SQLdb Tutorial3]]
1515
1616developing a 'real' application becomes harder. 'Form1' grows to an exponential size handling the different Events and Database Queries.
1717
1818Isolating 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.
1919
2020The sample shown here uses just 2 tables with simple queries, but it can be expanded to include more options with each table.
2121
2222## Getting started
2323
2424In the Lazarus IDE, create a new Application and then click File --\> New --\> Data Module
2525
26-<figure>
27-<img src="newDM.jpg" title="newDM.jpg" />
28-<figcaption>newDM.jpg</figcaption>
29-</figure>
26+![](/wiki-images/4/42/NewDM.jpg)
3027
3128You will be presented with a window as if you selected 'New Form':
3229
33-<figure>
34-<img src="dm1.jpg" title="dm1.jpg" />
35-<figcaption>dm1.jpg</figcaption>
36-</figure>
30+![](/wiki-images/9/90/Dm1.jpg)
3731
3832The 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.
3933
4034## Everyone has their own way
4135
4236Your use of data modules will vary to suit your own needs. but as an example, I have at least 2 data modules:
4337
4438Unit: DataModule1 On this module I 'drop' a *T*Connection* and a *TSQLTransaction*.
4539
46-<figure>
47-<img src="conn.jpg" title="conn.jpg" />
48-<figcaption>conn.jpg</figcaption>
49-</figure>
40+![](/wiki-images/9/90/Conn.jpg)
5041
5142DataModule1 is used as the connection for all queries.
5243
5344I then create a DataModuleN for each table or view.
5445
55-<figure>
56-<img src="dm2.jpg" title="dm2.jpg" />
57-<figcaption>dm2.jpg</figcaption>
58-</figure>
46+![](/wiki-images/2/23/Dm2.jpg)
5947
6048Each DataModuleN will need to have the DataModule1 unit (unit2 in this example) added to the USES clause to connect to the database.
6149
6250From here everything is the same as stated in [[SQLdb Tutorial1]]. The components are connected in the same way and access is identical.
6351
6452## More uses of data modules
6553
6654### Additional components
6755
6856In 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.
6957
7058For 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.
7159
7260### Debugging
7361
7462Debugging 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.
7563
7664The 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.
7765
7866---
7967*From the Free Pascal wiki, page "SQLdb Tutorial4", as it stood on 2017-02-24 after a change by FTurtle, out of the wiki's dump of 2024-05-26. Copyright stays with the author who wrote it, according to the old wiki's [[Licensing]] page. If you have concerns about this material, [message the admin](/wiki-contact).*

Back to history