Example: TDbf (creating table and indexes, selecting of index): what changed
Revision 0 to 1. Removed lines have a minus sign; added lines have a plus sign.
Title: Example: TDbf (creating table and indexes, selecting of index)
| Old | New | Line | |
|---|---|---|---|
| 1 | + | This example was taken from the post at the forum: | |
| 2 | + | <http://forum.lazarus.freepascal.org/index.php/topic,31169.msg199356.html#msg199356> | |
| 3 | + | ||
| 4 | + | ||
| 5 | + | You can copy text to files: | |
| 6 | + | ||
| 7 | + | * Code: **mainaddindex.pas** | |
| 8 | + | * Form: **mainaddindex.lfm** | |
| 9 | + | ||
| 10 | + | and test it. | |
| 11 | + | ||
| 12 | + | ||
| 13 | + | ||
| 14 | + | ### Code | |
| 15 | + | ``` | |
| 16 | + | unit 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 | + | ``` | |
| 129 | + | ||
| 130 | + | ||
| 131 | + | ||
| 132 | + | ### Form | |
| 133 | + | ``` | |
| 134 | + | object 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 | |
| 208 | + | ``` | |
| 209 | + | ||
| 210 | + | ## See also | |
| 211 | + | ||
| 212 | + | * [[Lazarus Tdbf Tutorial]] | |
| 213 | + | * [[Guide for usage of Tdbf component/ru]] (rus) | |
| 214 | + | ||
| 215 | + | --- | |
| 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).* |
