Sign in Join
Wiki copytemporary

Example: TDbf (creating table and indexes, selecting of index): what changed

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

OldNewLine
1-This example was taken from the post at the forum:
1+This example was taken from the post at the forum:\
22<http://forum.lazarus.freepascal.org/index.php/topic,31169.msg199356.html#msg199356>
33
4-
5-You can copy text to files:
6-
7- * Code: **mainaddindex.pas**
8- * Form: **mainaddindex.lfm**
4+You can copy text to files:
95
10-and test it.
6+- Code: **mainaddindex.pas**
7+- Form: **mainaddindex.lfm**
118
12-
9+and test it.
1310
1411### Code
15-```
12+
13+``` pascal
1614unit MainAddIndex;
17-
18- {$mode objfpc}{$H+}
19-
20- interface
21-
22- uses
23- Classes, SysUtils, db, dbf, Forms, Controls, Graphics, Dialogs,
24- DBGrids, DbCtrls, StdCtrls, LazFileUtils;
25-
26- const
27- CountriesFilename = 'countries.dbf';
28-
29- type
30-
31- { TForm1 }
32-
33- TForm1 = class(TForm)
34- BSortByCountry: TButton;
35- BSortByCapital: TButton;
36- BSortByID: TButton;
37- dbfSource: TDataSource;
38- grid: TDBGrid;
39- Navigator: TDBNavigator;
40- procedure BSortByCapitalClick(Sender: TObject);
41- procedure BSortByCountryClick(Sender: TObject);
42- procedure BSortByIDClick(Sender: TObject);
43- procedure FormCreate(Sender: TObject);
44- procedure FormDestroy(Sender: TObject);
45- private
46- CountriesDbf: TDbf;
47- public
48- { public declarations }
49- end;
50-
51- var
52- Form1: TForm1;
53-
54- implementation
55-
56- {$R *.lfm}
57-
58- { TForm1 }
59-
60- procedure TForm1.FormCreate(Sender: TObject);
61- begin
62- CountriesDbf:=TDbf.Create(Self);
63- CountriesDbf.TableName:=CountriesFilename;
64- if not FileExistsUTF8(CountriesFilename) then with CountriesDbf do begin
65- TableLevel:=7;
66- Exclusive:=True;
67- FieldDefs.Add('ID', ftAutoInc, 0, True);
68- FieldDefs.Add('CountryName', ftString, 25, True);
69- FieldDefs.Add('Capital', ftString, 25, True);
70- CreateTable;
71- Open;
72- Append;
73- // Fields[0].AsInteger:=100; // Do not fill 'ID' because its type is ftAutoInc
74- fields[1].AsString:='France';
75- Fields[2].AsString:='Paris';
76- Post;
77- Append;
78- // Fields[0].AsInteger:=101;
79- fields[1].AsString:='Egypt';
80- Fields[2].AsString:='Cairo';
81- Post;
82- Append;
83- // Fields[0].AsInteger:=102;
84- fields[1].AsString:='Indonesia';
85- Fields[2].AsString:='Jakarta';
86- Post;
87- Append;
88- // Fields[0].AsInteger:=103;
89- fields[1].AsString:='Austria';
90- Fields[2].AsString:='Vienna';
91- Post;
92- AddIndex('idxByID', 'ID', [ixPrimary,ixUnique]);
93- AddIndex('idxByCountry', 'CountryName', [ixCaseInsensitive]);
94- AddIndex('idxByCapital', 'Capital', [ixCaseInsensitive]);
95- end;
96- CountriesDbf.Open;
97- dbfSource.DataSet:=CountriesDbf;
98- grid.DataSource:=dbfSource;
99- Navigator.DataSource:=dbfSource;
100- end;
101-
102- procedure TForm1.BSortByCapitalClick(Sender: TObject);
103- begin
104- CountriesDbf.IndexName:='idxByCapital';
105- end;
106-
107- procedure TForm1.BSortByCountryClick(Sender: TObject);
108- begin
109- CountriesDbf.IndexName:='idxByCountry';
110- end;
111-
112- procedure TForm1.BSortByIDClick(Sender: TObject);
113- begin
114- CountriesDbf.IndexName:='idxByID';
115- end;
116-
117- procedure TForm1.FormDestroy(Sender: TObject);
118- begin
119- with CountriesDbf do begin
120- CloseIndexFile('idxByCapital');
121- CloseIndexFile('idxByCountry');
122- CloseIndexFile('idxByID');
123- end;
124- CountriesDbf.Close;
125- end;
126-
127- end.
128-```
12915
130-
16+{$mode objfpc}{$H+}
13117
132-### Form
18+interface
19+
20+uses
21+ Classes, SysUtils, db, dbf, Forms, Controls, Graphics, Dialogs,
22+ DBGrids, DbCtrls, StdCtrls, LazFileUtils;
23+
24+const
25+ CountriesFilename = 'countries.dbf';
26+
27+type
28+
29+ { TForm1 }
30+
31+ TForm1 = class(TForm)
32+ BSortByCountry: TButton;
33+ BSortByCapital: TButton;
34+ BSortByID: TButton;
35+ dbfSource: TDataSource;
36+ grid: TDBGrid;
37+ Navigator: TDBNavigator;
38+ procedure BSortByCapitalClick(Sender: TObject);
39+ procedure BSortByCountryClick(Sender: TObject);
40+ procedure BSortByIDClick(Sender: TObject);
41+ procedure FormCreate(Sender: TObject);
42+ procedure FormDestroy(Sender: TObject);
43+ private
44+ CountriesDbf: TDbf;
45+ public
46+ { public declarations }
47+ end;
48+
49+var
50+ Form1: TForm1;
51+
52+implementation
53+
54+{$R *.lfm}
55+
56+{ TForm1 }
57+
58+procedure TForm1.FormCreate(Sender: TObject);
59+begin
60+ CountriesDbf:=TDbf.Create(Self);
61+ CountriesDbf.TableName:=CountriesFilename;
62+ if not FileExistsUTF8(CountriesFilename) then with CountriesDbf do begin
63+ TableLevel:=7;
64+ Exclusive:=True;
65+ FieldDefs.Add('ID', ftAutoInc, 0, True);
66+ FieldDefs.Add('CountryName', ftString, 25, True);
67+ FieldDefs.Add('Capital', ftString, 25, True);
68+ CreateTable;
69+ Open;
70+ Append;
71+ // Fields[0].AsInteger:=100; // Do not fill 'ID' because its type is ftAutoInc
72+ fields[1].AsString:='France';
73+ Fields[2].AsString:='Paris';
74+ Post;
75+ Append;
76+ // Fields[0].AsInteger:=101;
77+ fields[1].AsString:='Egypt';
78+ Fields[2].AsString:='Cairo';
79+ Post;
80+ Append;
81+ // Fields[0].AsInteger:=102;
82+ fields[1].AsString:='Indonesia';
83+ Fields[2].AsString:='Jakarta';
84+ Post;
85+ Append;
86+ // Fields[0].AsInteger:=103;
87+ fields[1].AsString:='Austria';
88+ Fields[2].AsString:='Vienna';
89+ Post;
90+ AddIndex('idxByID', 'ID', [ixPrimary,ixUnique]);
91+ AddIndex('idxByCountry', 'CountryName', [ixCaseInsensitive]);
92+ AddIndex('idxByCapital', 'Capital', [ixCaseInsensitive]);
93+ end;
94+ CountriesDbf.Open;
95+ dbfSource.DataSet:=CountriesDbf;
96+ grid.DataSource:=dbfSource;
97+ Navigator.DataSource:=dbfSource;
98+end;
99+
100+procedure TForm1.BSortByCapitalClick(Sender: TObject);
101+begin
102+ CountriesDbf.IndexName:='idxByCapital';
103+end;
104+
105+procedure TForm1.BSortByCountryClick(Sender: TObject);
106+begin
107+ CountriesDbf.IndexName:='idxByCountry';
108+end;
109+
110+procedure TForm1.BSortByIDClick(Sender: TObject);
111+begin
112+ CountriesDbf.IndexName:='idxByID';
113+end;
114+
115+procedure TForm1.FormDestroy(Sender: TObject);
116+begin
117+ with CountriesDbf do begin
118+ CloseIndexFile('idxByCapital');
119+ CloseIndexFile('idxByCountry');
120+ CloseIndexFile('idxByID');
121+ end;
122+ CountriesDbf.Close;
123+end;
124+
125+end.
133126```
127+
128+### Form
129+
130+``` pascal
134131object Form1: TForm1
135- Left = 229
136- Height = 270
137- Top = 94
138- Width = 420
139- Caption = 'Form1'
140- ClientHeight = 270
141- ClientWidth = 420
142- OnCreate = FormCreate
143- OnDestroy = FormDestroy
144- LCLVersion = '1.6.0.2'
145- object grid: TDBGrid
146- Left = 0
147- Height = 200
148- Top = 0
149- Width = 420
150- Align = alTop
151- Color = clWindow
152- Columns = <>
153- TabOrder = 0
154- end
155- object Navigator: TDBNavigator
156- Left = 0
157- Height = 25
158- Top = 200
159- Width = 420
160- Align = alTop
161- BevelOuter = bvNone
162- ChildSizing.EnlargeHorizontal = crsScaleChilds
163- ChildSizing.EnlargeVertical = crsScaleChilds
164- ChildSizing.ShrinkHorizontal = crsScaleChilds
165- ChildSizing.ShrinkVertical = crsScaleChilds
166- ChildSizing.Layout = cclLeftToRightThenTopToBottom
167- ChildSizing.ControlsPerLine = 100
168- ClientHeight = 25
169- ClientWidth = 420
170- Options = []
171- TabOrder = 1
172- end
173- object BSortByCountry: TButton
174- Left = 12
175- Height = 23
176- Top = 234
177- Width = 101
178- AutoSize = True
179- Caption = 'Sort by country'
180- OnClick = BSortByCountryClick
181- TabOrder = 2
182- end
183- object BSortByCapital: TButton
184- Left = 168
185- Height = 23
186- Top = 234
187- Width = 95
188- AutoSize = True
189- Caption = 'Sort by capital'
190- OnClick = BSortByCapitalClick
191- TabOrder = 3
192- end
193- object BSortByID: TButton
194- Left = 318
195- Height = 23
196- Top = 234
197- Width = 75
198- AutoSize = True
199- Caption = 'Sort by ID'
200- OnClick = BSortByIDClick
201- TabOrder = 4
202- end
203- object dbfSource: TDataSource
204- left = 53
205- top = 99
206- end
207- end
132+ Left = 229
133+ Height = 270
134+ Top = 94
135+ Width = 420
136+ Caption = 'Form1'
137+ ClientHeight = 270
138+ ClientWidth = 420
139+ OnCreate = FormCreate
140+ OnDestroy = FormDestroy
141+ LCLVersion = '1.6.0.2'
142+ object grid: TDBGrid
143+ Left = 0
144+ Height = 200
145+ Top = 0
146+ Width = 420
147+ Align = alTop
148+ Color = clWindow
149+ Columns = <>
150+ TabOrder = 0
151+ end
152+ object Navigator: TDBNavigator
153+ Left = 0
154+ Height = 25
155+ Top = 200
156+ Width = 420
157+ Align = alTop
158+ BevelOuter = bvNone
159+ ChildSizing.EnlargeHorizontal = crsScaleChilds
160+ ChildSizing.EnlargeVertical = crsScaleChilds
161+ ChildSizing.ShrinkHorizontal = crsScaleChilds
162+ ChildSizing.ShrinkVertical = crsScaleChilds
163+ ChildSizing.Layout = cclLeftToRightThenTopToBottom
164+ ChildSizing.ControlsPerLine = 100
165+ ClientHeight = 25
166+ ClientWidth = 420
167+ Options = []
168+ TabOrder = 1
169+ end
170+ object BSortByCountry: TButton
171+ Left = 12
172+ Height = 23
173+ Top = 234
174+ Width = 101
175+ AutoSize = True
176+ Caption = 'Sort by country'
177+ OnClick = BSortByCountryClick
178+ TabOrder = 2
179+ end
180+ object BSortByCapital: TButton
181+ Left = 168
182+ Height = 23
183+ Top = 234
184+ Width = 95
185+ AutoSize = True
186+ Caption = 'Sort by capital'
187+ OnClick = BSortByCapitalClick
188+ TabOrder = 3
189+ end
190+ object BSortByID: TButton
191+ Left = 318
192+ Height = 23
193+ Top = 234
194+ Width = 75
195+ AutoSize = True
196+ Caption = 'Sort by ID'
197+ OnClick = BSortByIDClick
198+ TabOrder = 4
199+ end
200+ object dbfSource: TDataSource
201+ left = 53
202+ top = 99
203+ end
204+end
208205```
209206
210207## See also
211208
212- * [[Lazarus Tdbf Tutorial]]
213- * [[Guide for usage of Tdbf component/ru]] (rus)
209+- [[Lazarus Tdbf Tutorial]]
210+- [[Guide for usage of Tdbf component/ru]] (rus)
214211
215212---
216-*From the Free Pascal wiki, page "Example: TDbf (creating table and indexes, selecting of index)", as the Internet Archive kept it on 2023-05-30. 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).*
213+*From the Free Pascal wiki, page "Example: TDbf (creating table and indexes, selecting of index)", as it stood on 2020-02-14 after a change by Trev, 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).*
214+Category: Databases
215+Category: Examples

Back to history