Sign in Join
Wiki copytemporary

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

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

OldNewLine
11This example was taken from the post at the forum:\
22<http://forum.lazarus.freepascal.org/index.php/topic,31169.msg199356.html#msg199356>
33
44You can copy text to files:
55
66- Code: **mainaddindex.pas**
77- Form: **mainaddindex.lfm**
88
99and test it.
1010
1111### Code
1212
1313``` pascal
1414unit MainAddIndex;
1515
1616{$mode objfpc}{$H+}
1717
1818interface
1919
2020uses
2121 Classes, SysUtils, db, dbf, Forms, Controls, Graphics, Dialogs,
2222 DBGrids, DbCtrls, StdCtrls, LazFileUtils;
2323
2424const
2525 CountriesFilename = 'countries.dbf';
2626
2727type
2828
2929 { TForm1 }
3030
3131 TForm1 = class(TForm)
3232 BSortByCountry: TButton;
3333 BSortByCapital: TButton;
3434 BSortByID: TButton;
3535 dbfSource: TDataSource;
3636 grid: TDBGrid;
3737 Navigator: TDBNavigator;
3838 procedure BSortByCapitalClick(Sender: TObject);
3939 procedure BSortByCountryClick(Sender: TObject);
4040 procedure BSortByIDClick(Sender: TObject);
4141 procedure FormCreate(Sender: TObject);
4242 procedure FormDestroy(Sender: TObject);
4343 private
4444 CountriesDbf: TDbf;
4545 public
4646 { public declarations }
4747 end;
4848
4949var
5050 Form1: TForm1;
5151
5252implementation
5353
5454{$R *.lfm}
5555
5656{ TForm1 }
5757
5858procedure TForm1.FormCreate(Sender: TObject);
5959begin
6060 CountriesDbf:=TDbf.Create(Self);
6161 CountriesDbf.TableName:=CountriesFilename;
6262 if not FileExistsUTF8(CountriesFilename) then with CountriesDbf do begin
6363 TableLevel:=7;
6464 Exclusive:=True;
6565 FieldDefs.Add('ID', ftAutoInc, 0, True);
6666 FieldDefs.Add('CountryName', ftString, 25, True);
6767 FieldDefs.Add('Capital', ftString, 25, True);
6868 CreateTable;
6969 Open;
7070 Append;
7171 // Fields[0].AsInteger:=100; // Do not fill 'ID' because its type is ftAutoInc
7272 fields[1].AsString:='France';
7373 Fields[2].AsString:='Paris';
7474 Post;
7575 Append;
7676 // Fields[0].AsInteger:=101;
7777 fields[1].AsString:='Egypt';
7878 Fields[2].AsString:='Cairo';
7979 Post;
8080 Append;
8181 // Fields[0].AsInteger:=102;
8282 fields[1].AsString:='Indonesia';
8383 Fields[2].AsString:='Jakarta';
8484 Post;
8585 Append;
8686 // Fields[0].AsInteger:=103;
8787 fields[1].AsString:='Austria';
8888 Fields[2].AsString:='Vienna';
8989 Post;
9090 AddIndex('idxByID', 'ID', [ixPrimary,ixUnique]);
9191 AddIndex('idxByCountry', 'CountryName', [ixCaseInsensitive]);
9292 AddIndex('idxByCapital', 'Capital', [ixCaseInsensitive]);
9393 end;
9494 CountriesDbf.Open;
9595 dbfSource.DataSet:=CountriesDbf;
9696 grid.DataSource:=dbfSource;
9797 Navigator.DataSource:=dbfSource;
9898end;
9999
100100procedure TForm1.BSortByCapitalClick(Sender: TObject);
101101begin
102102 CountriesDbf.IndexName:='idxByCapital';
103103end;
104104
105105procedure TForm1.BSortByCountryClick(Sender: TObject);
106106begin
107107 CountriesDbf.IndexName:='idxByCountry';
108108end;
109109
110110procedure TForm1.BSortByIDClick(Sender: TObject);
111111begin
112112 CountriesDbf.IndexName:='idxByID';
113113end;
114114
115115procedure TForm1.FormDestroy(Sender: TObject);
116116begin
117117 with CountriesDbf do begin
118118 CloseIndexFile('idxByCapital');
119119 CloseIndexFile('idxByCountry');
120120 CloseIndexFile('idxByID');
121121 end;
122122 CountriesDbf.Close;
123123end;
124124
125125end.
126126```
127127
128128### Form
129129
130-``` pascal
130+``` lfm
131131object Form1: TForm1
132132 Left = 229
133133 Height = 270
134134 Top = 94
135135 Width = 420
136136 Caption = 'Form1'
137137 ClientHeight = 270
138138 ClientWidth = 420
139139 OnCreate = FormCreate
140140 OnDestroy = FormDestroy
141141 LCLVersion = '1.6.0.2'
142142 object grid: TDBGrid
143143 Left = 0
144144 Height = 200
145145 Top = 0
146146 Width = 420
147147 Align = alTop
148148 Color = clWindow
149149 Columns = <>
150150 TabOrder = 0
151151 end
152152 object Navigator: TDBNavigator
153153 Left = 0
154154 Height = 25
155155 Top = 200
156156 Width = 420
157157 Align = alTop
158158 BevelOuter = bvNone
159159 ChildSizing.EnlargeHorizontal = crsScaleChilds
160160 ChildSizing.EnlargeVertical = crsScaleChilds
161161 ChildSizing.ShrinkHorizontal = crsScaleChilds
162162 ChildSizing.ShrinkVertical = crsScaleChilds
163163 ChildSizing.Layout = cclLeftToRightThenTopToBottom
164164 ChildSizing.ControlsPerLine = 100
165165 ClientHeight = 25
166166 ClientWidth = 420
167167 Options = []
168168 TabOrder = 1
169169 end
170170 object BSortByCountry: TButton
171171 Left = 12
172172 Height = 23
173173 Top = 234
174174 Width = 101
175175 AutoSize = True
176176 Caption = 'Sort by country'
177177 OnClick = BSortByCountryClick
178178 TabOrder = 2
179179 end
180180 object BSortByCapital: TButton
181181 Left = 168
182182 Height = 23
183183 Top = 234
184184 Width = 95
185185 AutoSize = True
186186 Caption = 'Sort by capital'
187187 OnClick = BSortByCapitalClick
188188 TabOrder = 3
189189 end
190190 object BSortByID: TButton
191191 Left = 318
192192 Height = 23
193193 Top = 234
194194 Width = 75
195195 AutoSize = True
196196 Caption = 'Sort by ID'
197197 OnClick = BSortByIDClick
198198 TabOrder = 4
199199 end
200200 object dbfSource: TDataSource
201201 left = 53
202202 top = 99
203203 end
204204end
205205```
206206
207207## See also
208208
209209- [[Lazarus Tdbf Tutorial]]
210210- [[Guide for usage of Tdbf component/ru]] (rus)
211211
212212---
213213*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).*
214214Category: Databases
215215Category: Examples

Back to history