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.
| 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 | - | ||
| 5 | - | ||
| 6 | - | ||
| 7 | - | ||
| 8 | - | ||
| 4 | + | You can copy text to files: | |
| 9 | 5 | ||
| 10 | - | ||
| 6 | + | - Code: **mainaddindex.pas** | |
| 7 | + | - Form: **mainaddindex.lfm** | |
| 11 | 8 | ||
| 12 | - | ||
| 9 | + | and test it. | |
| 13 | 10 | ||
| 14 | 11 | ### Code | |
| 15 | - | ||
| 12 | + | ||
| 13 | + | ``` pascal | |
| 16 | 14 | unit MainAddIndex; | |
| 17 | - | ||
| 18 | - | ||
| 19 | - | ||
| 20 | - | ||
| 21 | - | ||
| 22 | - | ||
| 23 | - | ||
| 24 | - | ||
| 25 | - | ||
| 26 | - | ||
| 27 | - | ||
| 28 | - | ||
| 29 | - | ||
| 30 | - | ||
| 31 | - | ||
| 32 | - | ||
| 33 | - | ||
| 34 | - | ||
| 35 | - | ||
| 36 | - | ||
| 37 | - | ||
| 38 | - | ||
| 39 | - | ||
| 40 | - | ||
| 41 | - | ||
| 42 | - | ||
| 43 | - | ||
| 44 | - | ||
| 45 | - | ||
| 46 | - | ||
| 47 | - | ||
| 48 | - | ||
| 49 | - | ||
| 50 | - | ||
| 51 | - | ||
| 52 | - | ||
| 53 | - | ||
| 54 | - | ||
| 55 | - | ||
| 56 | - | ||
| 57 | - | ||
| 58 | - | ||
| 59 | - | ||
| 60 | - | ||
| 61 | - | ||
| 62 | - | ||
| 63 | - | ||
| 64 | - | ||
| 65 | - | ||
| 66 | - | ||
| 67 | - | ||
| 68 | - | ||
| 69 | - | ||
| 70 | - | ||
| 71 | - | ||
| 72 | - | ||
| 73 | - | ||
| 74 | - | ||
| 75 | - | ||
| 76 | - | ||
| 77 | - | ||
| 78 | - | ||
| 79 | - | ||
| 80 | - | ||
| 81 | - | ||
| 82 | - | ||
| 83 | - | ||
| 84 | - | ||
| 85 | - | ||
| 86 | - | ||
| 87 | - | ||
| 88 | - | ||
| 89 | - | ||
| 90 | - | ||
| 91 | - | ||
| 92 | - | ||
| 93 | - | ||
| 94 | - | ||
| 95 | - | ||
| 96 | - | ||
| 97 | - | ||
| 98 | - | ||
| 99 | - | ||
| 100 | - | ||
| 101 | - | ||
| 102 | - | ||
| 103 | - | ||
| 104 | - | ||
| 105 | - | ||
| 106 | - | ||
| 107 | - | ||
| 108 | - | ||
| 109 | - | ||
| 110 | - | ||
| 111 | - | ||
| 112 | - | ||
| 113 | - | ||
| 114 | - | ||
| 115 | - | ||
| 116 | - | ||
| 117 | - | ||
| 118 | - | ||
| 119 | - | ||
| 120 | - | ||
| 121 | - | ||
| 122 | - | ||
| 123 | - | ||
| 124 | - | ||
| 125 | - | ||
| 126 | - | ||
| 127 | - | ||
| 128 | - | ||
| 129 | 15 | ||
| 130 | - | ||
| 16 | + | {$mode objfpc}{$H+} | |
| 131 | 17 | ||
| 132 | - | ||
| 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. | |
| 133 | 126 | ``` | |
| 127 | + | ||
| 128 | + | ### Form | |
| 129 | + | ||
| 130 | + | ``` pascal | |
| 134 | 131 | object Form1: TForm1 | |
| 135 | - | ||
| 136 | - | ||
| 137 | - | ||
| 138 | - | ||
| 139 | - | ||
| 140 | - | ||
| 141 | - | ||
| 142 | - | ||
| 143 | - | ||
| 144 | - | ||
| 145 | - | ||
| 146 | - | ||
| 147 | - | ||
| 148 | - | ||
| 149 | - | ||
| 150 | - | ||
| 151 | - | ||
| 152 | - | ||
| 153 | - | ||
| 154 | - | ||
| 155 | - | ||
| 156 | - | ||
| 157 | - | ||
| 158 | - | ||
| 159 | - | ||
| 160 | - | ||
| 161 | - | ||
| 162 | - | ||
| 163 | - | ||
| 164 | - | ||
| 165 | - | ||
| 166 | - | ||
| 167 | - | ||
| 168 | - | ||
| 169 | - | ||
| 170 | - | ||
| 171 | - | ||
| 172 | - | ||
| 173 | - | ||
| 174 | - | ||
| 175 | - | ||
| 176 | - | ||
| 177 | - | ||
| 178 | - | ||
| 179 | - | ||
| 180 | - | ||
| 181 | - | ||
| 182 | - | ||
| 183 | - | ||
| 184 | - | ||
| 185 | - | ||
| 186 | - | ||
| 187 | - | ||
| 188 | - | ||
| 189 | - | ||
| 190 | - | ||
| 191 | - | ||
| 192 | - | ||
| 193 | - | ||
| 194 | - | ||
| 195 | - | ||
| 196 | - | ||
| 197 | - | ||
| 198 | - | ||
| 199 | - | ||
| 200 | - | ||
| 201 | - | ||
| 202 | - | ||
| 203 | - | ||
| 204 | - | ||
| 205 | - | ||
| 206 | - | ||
| 207 | - | ||
| 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 | |
| 208 | 205 | ``` | |
| 209 | 206 | ||
| 210 | 207 | ## See also | |
| 211 | 208 | ||
| 212 | - | ||
| 213 | - | ||
| 209 | + | - [[Lazarus Tdbf Tutorial]] | |
| 210 | + | - [[Guide for usage of Tdbf component/ru]] (rus) | |
| 214 | 211 | ||
| 215 | 212 | --- | |
| 216 | - | ||
| 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 |
