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