Sign in Join
Wiki copytemporary

TSqlite3 Master Detail Example/fr: what changed

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

OldNewLine
11Exemple de relation Maître/Détail pour TSqlite3.
22
33## A propos de la démo
44
55Cette démo est un exemple fonctionnel de comment utiliser le composant DataSet TSqlite3 de [[SQLite/fr|SQLite]] dans une relation Maître/Détail.
66
77> **Note:** Ce composant ne fait pas partie des composants de base de données généraux de la SQLDB. SQLDB fournit TSQLite3Connection qui correspond à d'autres connecteurs de bases de données. Il est fortement recommandé de regarder dans TSQLite3Connection du fait de la facilité de changement vers une autre base de données ; utilisez seulement TSQLite3 si cela est réellement demandé.
88
99Voir [[MasterDetail/fr|MasterDetail]] pour des instructions sur comment implémenter des relations Maître/Détail en utilisant des composants SQLdb standards (p.ex. sqlite3).
1010
1111# Auteur
1212
1313David Stewart .. davesimplewear at yahoo dot com\
1414NdT : Merci à Dave Stewart pour cet article.
1515
1616# Composants utilisés
1717
1818- TSqlite3
1919- Standard Lazarus database components
2020
2121# License
2222
2323- Vous êtes libres de l'utiliser comme vous le voulez.
2424
2525# Téléchargement
2626
2727L'exemple TSQLite3 peut être téléchargé depuis [L'emplacement de téléchargement Lazarus-CCR de SF](http://sourceforge.net/projects/lazarus-ccr/files/Demos%20and%20examples/). Aussi depuis [Freeware de David](http://www.users.on.net/~dave.stewart/index.html).
2828
2929## Captures d'écran
3030
3131![](/wiki-images/b/b8/Screenshot-Master_Detail_Example_-_SQLLite3.png)
3232
3333![](/wiki-images/8/82/Screenshot-Master_Detail_Example_-_SQLLite3-1.png)
3434
3535![](/wiki-images/7/71/Screenshot-Master_Detail_Example_-_SQLLite3-2.png)
3636
3737![](/wiki-images/1/14/Screenshot-Select_Customer_Name.png)
3838
3939![](/wiki-images/2/2e/Screenshot-Select_Item_Number.png)
4040
4141Cette capture d'écran montre la configuration de la table pour le Mapître/Détail.
4242
4343![](/wiki-images/e/e6/Screenshot.png)
4444
4545## Code du programme d'exemple
4646
4747``` pascal
4848unit uMain;
4949
5050{$mode objfpc}{$H+}
5151
5252interface
5353
5454uses
5555 Classes, SysUtils, db, sqlite3ds, FileUtil, LResources, Forms, Controls,
5656 Graphics, Dialogs, ComCtrls, ExtCtrls, Menus, DbCtrls, StdCtrls, DBGrids;
5757
5858type
5959
6060 { TfMain }
6161
6262 TfMain = class(TForm)
6363 btnSelCust: TButton;
6464 btnAddSale: TButton;
6565 btnSaveEntry: TButton;
6666 btnDelEntry: TButton;
6767 DBNavigator3: TDBNavigator;
6868 dsCust: TDatasource;
6969 dsSales: TDatasource;
7070 dsStock: TDatasource;
7171 DBEdit1: TDBEdit;
7272 DBEdit10: TDBEdit;
7373 DBEdit12: TDBEdit;
7474 DBEdit13: TDBEdit;
7575 DBEdit14: TDBEdit;
7676 DBEdit15: TDBEdit;
7777 DBEdit3: TDBEdit;
7878 DBEdit4: TDBEdit;
7979 DBEdit5: TDBEdit;
8080 DBEdit6: TDBEdit;
8181 DBEdit7: TDBEdit;
8282 DBEdit8: TDBEdit;
8383 DBEdit9: TDBEdit;
8484 dgSales: TDBGrid;
8585 DBNavigator1: TDBNavigator;
8686 DBNavigator2: TDBNavigator;
8787 Label1: TLabel;
8888 Label10: TLabel;
8989 Label11: TLabel;
9090 Label2: TLabel;
9191 Label3: TLabel;
9292 Label4: TLabel;
9393 Label5: TLabel;
9494 Label7: TLabel;
9595 Label8: TLabel;
9696 Label9: TLabel;
9797 miClose: TMenuItem;
9898 miFile: TMenuItem;
9999 mmMain: TMainMenu;
100100 nbMain: TNotebook;
101101 Panel1: TPanel;
102102 pnlSales: TPanel;
103103 pnlStock: TPanel;
104104 pnlCustomer: TPanel;
105105 pnlSelectCust: TPanel;
106106 pStock: TPage;
107107 pCustomer: TPage;
108108 pSales: TPage;
109109 sbMain: TStatusBar;
110110 TCustAddr: TStringField;
111111 TCustCustName: TStringField;
112112 TCustcustState: TStringField;
113113 TCustID: TAutoIncField;
114114 TCustpostCode: TStringField;
115115 TCustSuburb: TStringField;
116116 TSalescustID: TLongintField;
117117 TSalesID: TAutoIncField;
118118 TSalesitem: TStringField;
119119 TSalesitemNum: TStringField;
120120 TSalesprice: TFloatField;
121121 TSalessaleDate: TDateField;
122122 TSalesshipDate: TDateField;
123123 TStock: TSqlite3Dataset;
124124 TSales: TSqlite3Dataset;
125125 TCust: TSqlite3Dataset;
126126 TStockID: TAutoIncField;
127127 TStockitem: TStringField;
128128 TStockitemNum: TStringField;
129129 TStockprice: TFloatField;
130130 procedure btnAddSaleClick(Sender: TObject);
131131 procedure btnDelEntryClick(Sender: TObject);
132132 procedure btnSaveEntryClick(Sender: TObject);
133133 procedure btnSelCustClick(Sender: TObject);
134134 procedure dgSalesEditButtonClick(Sender: TObject);
135135 procedure FormCloseQuery(Sender: TObject; var CanClose: boolean);
136136 procedure FormCreate(Sender: TObject);
137137 procedure FormDestroy(Sender: TObject);
138138 procedure FormShow(Sender: TObject);
139139 procedure miCloseClick(Sender: TObject);
140140 private
141141 { private declarations }
142142 public
143143 { public declarations }
144144 end;
145145
146146var
147147 fMain: TfMain;
148148
149149implementation
150150uses uCust, uSales;
151151{ TfMain }
152152
153153procedure TfMain.miCloseClick(Sender: TObject);
154154begin
155155 Close;
156156end;
157157
158158procedure TfMain.FormCloseQuery(Sender: TObject; var CanClose: boolean);
159159begin
160160 CanClose := MessageDlg('Are You Sure ?',mtConfirmation,[mbYes,mbNo],0)=mrYes;
161161end;
162162
163163procedure TfMain.FormCreate(Sender: TObject);
164164var
165165n:integer;
166166c:TComponent;
167167FName:string;
168168begin
169169 fName := ExtractFilePath(ParamStr(0)) +'data/md.db3';
170170for n := 0 to ComponentCount -1 do
171171 begin
172172 c := Components[n];
173173 if c is TSqlite3Dataset then
174174 TSqlite3Dataset(c).FileName:= fName;
175175 end;
176176 for n := 0 to ComponentCount -1 do
177177 begin
178178 c := Components[n];
179179 if c is TSqlite3Dataset then
180180 TSqlite3Dataset(c).Open;
181181 end;
182182end;
183183
184184procedure TfMain.FormDestroy(Sender: TObject);
185185var
186186n:integer;
187187c:TComponent;
188188begin
189189 for n := 0 to ComponentCount -1 do
190190 begin
191191 c := Components[n];
192192 if c is TSqlite3Dataset then
193193 TSqlite3Dataset(c).Close;
194194 end;
195195end;
196196
197197procedure TfMain.FormShow(Sender: TObject);
198198begin
199199 nbMain.PageIndex:=0;
200200end;
201201
202202procedure TfMain.dgSalesEditButtonClick(Sender: TObject);
203203begin
204204 if SearchDlg.ShowModalParts =mrOk then
205205 begin
206206 TSales.Edit;
207207 TSalesItemNum.Value := SearchDlg.PartNum;
208208 TSalesItem.Value := TStockitem.Value;
209209 TSalesPRICE.Value:= TStockPrice.Value;
210210 end;
211211end;
212212
213213procedure TfMain.btnSelCustClick(Sender: TObject);
214214begin
215215 custDlg.CustName := TCustCUSTNAME.Value ;
216216 if CustDlg.ShowModalCust =mrOk then
217217 begin
218218 TCust.Edit;
219219 TCustCUSTNAME.Value := custDlg.CustName;
220220 end;
221221end;
222222
223223procedure TfMain.btnAddSaleClick(Sender: TObject);
224224begin
225225 TSales.Append;
226226end;
227227
228228procedure TfMain.btnDelEntryClick(Sender: TObject);
229229begin
230230 TSales.Delete;
231231end;
232232
233233procedure TfMain.btnSaveEntryClick(Sender: TObject);
234234begin
235235 TSales.ApplyUpdates;
236236end;
237237
238238initialization
239239 {$I uMain.lrs}
240240
241241end.
242242```
243243
244244uMain.lfm (edité)
245245
246-``` pascal
246+``` lfm
247247object fMain: TfMain
248248 Left = 395
249249 Height = 332
250250 Top = 218
251251 Width = 534
252252 ActiveControl = nbMain
253253 BorderIcons = [biSystemMenu, biMinimize]
254254 BorderStyle = bsSingle
255255 Caption = 'Master Detail Example - SQLLite3'
256256 ClientHeight = 305
257257 ClientWidth = 534
258258 Font.Height = -13
259259 Font.Name = 'Sans'
260260 Menu = mmMain
261261 OnCloseQuery = FormCloseQuery
262262 OnCreate = FormCreate
263263 OnDestroy = FormDestroy
264264 OnShow = FormShow
265265 Position = poScreenCenter
266266 LCLVersion = '0.9.27'
267267 object sbMain: TStatusBar
268268 Left = 0
269269 Height = 19
270270 Top = 286
271271 Width = 534
272272 AutoHint = True
273273 Panels = <>
274274 end
275275 object nbMain: TNotebook
276276 Left = 0
277277 Height = 286
278278 Top = 0
279279 Width = 534
280280 Align = alClient
281281 PageIndex = 0
282282 TabOrder = 1
283283 object pSales: TPage
284284 Caption = 'Sales'
285285 ClientWidth = 532
286286 ClientHeight = 259
287287 object Label3: TLabel
288288 Left = 0
289289 Height = 18
290290 Top = 241
291291 Width = 532
292292 Align = alBottom
293293 Caption = 'Select Customer First, then click in item number field to select item, then save'
294294 ParentColor = False
295295 end
296296 object pnlSelectCust: TPanel
297297 Left = 15
298298 Height = 216
299299 Top = 14
300300 Width = 232
301301 BevelInner = bvLowered
302302 BevelWidth = 2
303303 ClientHeight = 216
304304 ClientWidth = 232
305305 TabOrder = 0
306306 object Label1: TLabel
307307 Left = 4
308308 Height = 18
309309 Top = 4
310310 Width = 224
311311 Align = alTop
312312 Alignment = taCenter
313313 Caption = 'Customer Detail'
314314 Font.Height = -13
315315 Font.Name = 'Sans'
316316 Font.Style = [fsBold, fsItalic]
317317 ParentColor = False
318318 ParentFont = False
319319 end
320320 object Label2: TLabel
321321 Left = 24
322322 Height = 18
323323 Top = 31
324324 Width = 73
325325 Caption = 'First Name'
326326 Font.Height = -13
327327 Font.Name = 'Sans'
328328 Font.Style = [fsItalic]
329329 ParentColor = False
330330 ParentFont = False
331331 end
332332 object DBEdit1: TDBEdit
333333 Left = 24
334334 Height = 23
335335 Hint = 'Cust name'
336336 Top = 48
337337 Width = 184
338338 DataField = 'CustName'
339339 DataSource = dsCust
340340 ReadOnly = True
341341 MaxLength = 8192
342342 ParentShowHint = False
343343 ShowHint = True
344344 TabOrder = 0
345345 end
346346 object DBEdit3: TDBEdit
347347 Left = 24
348348 Height = 23
349349 Hint = 'Address'
350350 Top = 72
351351 Width = 184
352352 DataField = 'Addr'
353353 DataSource = dsCust
354354 ReadOnly = True
355355 MaxLength = 8192
356356 ParentShowHint = False
357357 ShowHint = True
358358 TabOrder = 1
359359 end
360360 object DBEdit4: TDBEdit
361361 Left = 24
362362 Height = 23
363363 Hint = 'Suburb/Town'
364364 Top = 96
365365 Width = 184
366366 DataField = 'Suburb'
367367 DataSource = dsCust
368368 ReadOnly = True
369369 MaxLength = 8192
370370 ParentShowHint = False
371371 ShowHint = True
372372 TabOrder = 2
373373 end
374374 object DBEdit5: TDBEdit
375375 Left = 24
376376 Height = 23
377377 Hint = 'Postal Code'
378378 Top = 120
379379 Width = 80
380380 DataField = 'postCode'
381381 DataSource = dsCust
382382 ReadOnly = True
383383 MaxLength = 8192
384384 ParentShowHint = False
385385 ShowHint = True
386386 TabOrder = 3
387387 end
388388 object DBEdit6: TDBEdit
389389 Left = 128
390390 Height = 23
391391 Hint = 'State'
392392 Top = 119
393393 Width = 80
394394 DataField = 'custState'
395395 DataSource = dsCust
396396 ReadOnly = True
397397 CharCase = ecUppercase
398398 MaxLength = 8192
399399 ParentShowHint = False
400400 ShowHint = True
401401 TabOrder = 4
402402 end
403403 object btnSelCust: TButton
404404 Left = 24
405405 Height = 25
406406 Hint = 'Click to select a Customer'
407407 Top = 176
408408 Width = 184
409409 Caption = 'Select Customer'
410410 OnClick = btnSelCustClick
411411 ParentShowHint = False
412412 ShowHint = True
413413 TabOrder = 5
414414 end
415415 object DBNavigator3: TDBNavigator
416416 Left = 30
417417 Height = 22
418418 Top = 150
419419 Width = 170
420420 BevelOuter = bvNone
421421 ClientHeight = 22
422422 ClientWidth = 170
423423 DataSource = dsCust
424424 TabOrder = 6
425425 VisibleButtons = [nbFirst, nbPrior, nbNext, nbLast]
426426 end
427427 end
428428 object pnlSales: TPanel
429429 Left = 255
430430 Height = 216
431431 Top = 14
432432 Width = 260
433433 BevelInner = bvLowered
434434 BevelWidth = 2
435435 ClientHeight = 216
436436 ClientWidth = 260
437437 TabOrder = 1
438438 object Label4: TLabel
439439 Left = 4
440440 Height = 18
441441 Top = 4
442442 Width = 252
443443 Align = alTop
444444 Alignment = taCenter
445445 Caption = 'Sales Detail'
446446 Font.Height = -13
447447 Font.Name = 'Sans'
448448 Font.Style = [fsBold, fsItalic]
449449 ParentColor = False
450450 ParentFont = False
451451 end
452452 object btnAddSale: TButton
453453 Left = 16
454454 Height = 25
455455 Hint = 'Add Another Sale'
456456 Top = 176
457457 Width = 72
458458 Caption = 'Add Sale'
459459 OnClick = btnAddSaleClick
460460 ParentShowHint = False
461461 ShowHint = True
462462 TabOrder = 0
463463 end
464464 object Panel1: TPanel
465465 Left = 4
466466 Height = 13
467467 Top = 22
468468 Width = 252
469469 Align = alTop
470470 BevelOuter = bvNone
471471 TabOrder = 1
472472 end
473473 object dgSales: TDBGrid
474474 Left = 4
475475 Height = 125
476476 Hint = 'Click Item Number Button to Select Item'
477477 Top = 35
478478 Width = 252
479479 Align = alTop
480480 Columns = <
481481 item
482482 ButtonStyle = cbsEllipsis
483483 Title.Caption = 'item Number'
484484 Width = 100
485485 FieldName = 'itemNum'
486486 end
487487 item
488488 Width = 150
489489 FieldName = 'item'
490490 end
491491 item
492492 FieldName = 'price'
493493 end
494494 item
495495 Title.Caption = 'sale Date'
496496 FieldName = 'saleDate'
497497 end
498498 item
499499 Title.Caption = 'ship Date'
500500 FieldName = 'shipDate'
501501 end>
502502 DataSource = dsSales
503503 ShowHint = True
504504 TabOrder = 2
505505 TitleFont.Height = -13
506506 TitleFont.Name = 'Sans'
507507 OnEditButtonClick = dgSalesEditButtonClick
508508 end
509509 object btnSaveEntry: TButton
510510 Left = 95
511511 Height = 25
512512 Hint = 'Save Entry'
513513 Top = 176
514514 Width = 51
515515 Caption = 'Save'
516516 OnClick = btnSaveEntryClick
517517 ParentShowHint = False
518518 ShowHint = True
519519 TabOrder = 3
520520 end
521521 object btnDelEntry: TButton
522522 Left = 168
523523 Height = 25
524524 Hint = 'Delete Entry'
525525 Top = 176
526526 Width = 75
527527 Caption = 'Delete'
528528 OnClick = btnDelEntryClick
529529 ParentShowHint = False
530530 ShowHint = True
531531 TabOrder = 4
532532 end
533533 end
534534 end
535535 object pCustomer: TPage
536536 Caption = 'Customer'
537537 ClientWidth = 532
538538 ClientHeight = 259
539539 object pnlCustomer: TPanel
540540 Left = 95
541541 Height = 194
542542 Top = 30
543543 Width = 339
544544 BevelInner = bvLowered
545545 BevelWidth = 2
546546 ClientHeight = 194
547547 ClientWidth = 339
548548 TabOrder = 0
549549 object Label5: TLabel
550550 Left = 72
551551 Height = 18
552552 Top = 31
553553 Width = 73
554554 Caption = 'First Name'
555555 Font.Height = -13
556556 Font.Name = 'Sans'
557557 Font.Style = [fsItalic]
558558 ParentColor = False
559559 ParentFont = False
560560 end
561561 object Label7: TLabel
562562 Left = 4
563563 Height = 18
564564 Top = 4
565565 Width = 331
566566 Align = alTop
567567 Alignment = taCenter
568568 Caption = 'Customer Entry'
569569 Font.Height = -13
570570 Font.Name = 'Sans'
571571 Font.Style = [fsBold, fsItalic]
572572 ParentColor = False
573573 ParentFont = False
574574 end
575575 object DBEdit10: TDBEdit
576576 Left = 72
577577 Height = 23
578578 Hint = 'Cust name'
579579 Top = 48
580580 Width = 184
581581 DataField = 'CustName'
582582 DataSource = dsCust
583583 MaxLength = 8192
584584 ParentShowHint = False
585585 ShowHint = True
586586 TabOrder = 0
587587 end
588588 object DBEdit12: TDBEdit
589589 Left = 72
590590 Height = 23
591591 Hint = 'Address'
592592 Top = 72
593593 Width = 184
594594 DataField = 'Addr'
595595 DataSource = dsCust
596596 MaxLength = 8192
597597 ParentShowHint = False
598598 ShowHint = True
599599 TabOrder = 1
600600 end
601601 object DBEdit13: TDBEdit
602602 Left = 72
603603 Height = 23
604604 Hint = 'Suburb/Town'
605605 Top = 96
606606 Width = 184
607607 DataField = 'Suburb'
608608 DataSource = dsCust
609609 MaxLength = 8192
610610 ParentShowHint = False
611611 ShowHint = True
612612 TabOrder = 2
613613 end
614614 object DBEdit14: TDBEdit
615615 Left = 72
616616 Height = 23
617617 Hint = 'Postal Code'
618618 Top = 120
619619 Width = 80
620620 DataField = 'postCode'
621621 DataSource = dsCust
622622 MaxLength = 8192
623623 ParentShowHint = False
624624 ShowHint = True
625625 TabOrder = 3
626626 end
627627 object DBEdit15: TDBEdit
628628 Left = 176
629629 Height = 23
630630 Hint = 'State'
631631 Top = 119
632632 Width = 80
633633 DataField = 'custState'
634634 DataSource = dsCust
635635 CharCase = ecUppercase
636636 MaxLength = 8192
637637 ParentShowHint = False
638638 ShowHint = True
639639 TabOrder = 4
640640 end
641641 object DBNavigator2: TDBNavigator
642642 Left = 45
643643 Height = 25
644644 Top = 154
645645 Width = 241
646646 BevelOuter = bvNone
647647 ClientHeight = 25
648648 ClientWidth = 241
649649 DataSource = dsCust
650650 TabOrder = 5
651651 end
652652 end
653653 end
654654 object pStock: TPage
655655 Caption = 'Stock'
656656 ClientWidth = 532
657657 ClientHeight = 259
658658 object pnlStock: TPanel
659659 Left = 95
660660 Height = 218
661661 Top = 22
662662 Width = 339
663663 BevelInner = bvLowered
664664 BevelWidth = 2
665665 ClientHeight = 218
666666 ClientWidth = 339
667667 TabOrder = 0
668668 object Label8: TLabel
669669 Left = 4
670670 Height = 18
671671 Top = 4
672672 Width = 331
673673 Align = alTop
674674 Alignment = taCenter
675675 Caption = 'Stock Entry'
676676 Font.Height = -13
677677 Font.Name = 'Sans'
678678 Font.Style = [fsBold, fsItalic]
679679 ParentColor = False
680680 ParentFont = False
681681 end
682682 object Label9: TLabel
683683 Left = 75
684684 Height = 18
685685 Top = 24
686686 Width = 83
687687 Caption = 'Part Number'
688688 Font.Height = -13
689689 Font.Name = 'Sans'
690690 Font.Style = [fsItalic]
691691 ParentColor = False
692692 ParentFont = False
693693 end
694694 object Label10: TLabel
695695 Left = 75
696696 Height = 18
697697 Top = 72
698698 Width = 75
699699 Caption = 'Description'
700700 Font.Height = -13
701701 Font.Name = 'Sans'
702702 Font.Style = [fsItalic]
703703 ParentColor = False
704704 ParentFont = False
705705 end
706706 object Label11: TLabel
707707 Left = 75
708708 Height = 18
709709 Top = 128
710710 Width = 33
711711 Caption = 'Price'
712712 Font.Height = -13
713713 Font.Name = 'Sans'
714714 Font.Style = [fsItalic]
715715 ParentColor = False
716716 ParentFont = False
717717 end
718718 object DBEdit7: TDBEdit
719719 Left = 75
720720 Height = 23
721721 Top = 40
722722 Width = 181
723723 DataField = 'itemNum'
724724 DataSource = dsStock
725725 MaxLength = 8192
726726 ParentShowHint = False
727727 ShowHint = True
728728 TabOrder = 0
729729 end
730730 object DBEdit8: TDBEdit
731731 Left = 75
732732 Height = 23
733733 Top = 89
734734 Width = 181
735735 DataField = 'item'
736736 DataSource = dsStock
737737 MaxLength = 8192
738738 ParentShowHint = False
739739 ShowHint = True
740740 TabOrder = 1
741741 end
742742 object DBEdit9: TDBEdit
743743 Left = 75
744744 Height = 23
745745 Top = 145
746746 Width = 181
747747 DataField = 'price'
748748 DataSource = dsStock
749749 ParentShowHint = False
750750 ShowHint = True
751751 TabOrder = 2
752752 end
753753 object DBNavigator1: TDBNavigator
754754 Left = 48
755755 Height = 25
756756 Top = 183
757757 Width = 241
758758 BevelOuter = bvNone
759759 ClientHeight = 25
760760 ClientWidth = 241
761761 DataSource = dsStock
762762 ParentShowHint = False
763763 ShowHint = True
764764 TabOrder = 3
765765 end
766766 end
767767 end
768768 end
769769 object mmMain: TMainMenu
770770 left = 16
771771 top = 277
772772 object miFile: TMenuItem
773773 Caption = '&File'
774774 object miClose: TMenuItem
775775 Caption = '&Close'
776776 GlyphShowMode = gsmAlways
777777 Hint = 'Close Application'
778778 OnClick = miCloseClick
779779 end
780780 end
781781 end
782782 object dsCust: TDatasource
783783 DataSet = TCust
784784 left = 16
785785 top = 53
786786 end
787787 object dsSales: TDatasource
788788 DataSet = TSales
789789 left = 16
790790 top = 141
791791 end
792792 object dsStock: TDatasource
793793 DataSet = TStock
794794 left = 16
795795 top = 214
796796 end
797797 object TCust: TSqlite3Dataset
798798 AutoIncrementKey = True
799799 Options = []
800800 PrimaryKey = 'ID'
801801 SaveOnClose = True
802802 SaveOnRefetch = True
803803 SQL = 'Select * from cust;'
804804 TableName = 'cust'
805805 FieldDefs = <
806806 item
807807 Name = 'ID'
808808 DataType = ftAutoInc
809809 Precision = -1
810810 Size = 0
811811 end
812812 item
813813 Name = 'CustName'
814814 DataType = ftString
815815 Precision = -1
816816 Size = 8192
817817 end
818818 item
819819 Name = 'Addr'
820820 DataType = ftString
821821 Precision = -1
822822 Size = 8192
823823 end
824824 item
825825 Name = 'Suburb'
826826 DataType = ftString
827827 Precision = -1
828828 Size = 8192
829829 end
830830 item
831831 Name = 'postCode'
832832 DataType = ftString
833833 Precision = -1
834834 Size = 8192
835835 end
836836 item
837837 Name = 'custState'
838838 DataType = ftString
839839 Precision = -1
840840 Size = 8192
841841 end>
842842 left = 66
843843 top = 53
844844 object TCustID: TAutoIncField
845845 DisplayWidth = 10
846846 FieldKind = fkData
847847 FieldName = 'ID'
848848 Index = 0
849849 LookupCache = False
850850 ProviderFlags = [pfInUpdate, pfInWhere]
851851 ReadOnly = False
852852 Required = False
853853 end
854854 object TCustCustName: TStringField
855855 DisplayWidth = 8192
856856 FieldKind = fkData
857857 FieldName = 'CustName'
858858 Index = 1
859859 LookupCache = False
860860 ProviderFlags = [pfInUpdate, pfInWhere]
861861 ReadOnly = False
862862 Required = False
863863 Size = 8192
864864 end
865865 object TCustAddr: TStringField
866866 DisplayWidth = 8192
867867 FieldKind = fkData
868868 FieldName = 'Addr'
869869 Index = 2
870870 LookupCache = False
871871 ProviderFlags = [pfInUpdate, pfInWhere]
872872 ReadOnly = False
873873 Required = False
874874 Size = 8192
875875 end
876876 object TCustSuburb: TStringField
877877 DisplayWidth = 8192
878878 FieldKind = fkData
879879 FieldName = 'Suburb'
880880 Index = 3
881881 LookupCache = False
882882 ProviderFlags = [pfInUpdate, pfInWhere]
883883 ReadOnly = False
884884 Required = False
885885 Size = 8192
886886 end
887887 object TCustpostCode: TStringField
888888 DisplayWidth = 8192
889889 FieldKind = fkData
890890 FieldName = 'postCode'
891891 Index = 4
892892 LookupCache = False
893893 ProviderFlags = [pfInUpdate, pfInWhere]
894894 ReadOnly = False
895895 Required = False
896896 Size = 8192
897897 end
898898 object TCustcustState: TStringField
899899 DisplayWidth = 8192
900900 FieldKind = fkData
901901 FieldName = 'custState'
902902 Index = 5
903903 LookupCache = False
904904 ProviderFlags = [pfInUpdate, pfInWhere]
905905 ReadOnly = False
906906 Required = False
907907 Size = 8192
908908 end
909909 end
910910 object TSales: TSqlite3Dataset
911911 AutoIncrementKey = True
912912 IndexFieldNames = 'CustID'
913913 Options = []
914914 PrimaryKey = 'ID'
915915 SaveOnClose = True
916916 SaveOnRefetch = True
917917 SQL = 'Select * from sales;'
918918 TableName = 'sales'
919919 MasterSource = dsCust
920920 MasterFields = 'ID'
921921 FieldDefs = <
922922 item
923923 Name = 'itemNum'
924924 DataType = ftString
925925 Precision = -1
926926 Size = 8192
927927 end
928928 item
929929 Name = 'ID'
930930 DataType = ftAutoInc
931931 Precision = -1
932932 Size = 0
933933 end
934934 item
935935 Name = 'custID'
936936 DataType = ftInteger
937937 Precision = -1
938938 Size = 0
939939 end
940940 item
941941 Name = 'saleDate'
942942 DataType = ftDate
943943 Precision = -1
944944 Size = 0
945945 end
946946 item
947947 Name = 'shipDate'
948948 DataType = ftDate
949949 Precision = -1
950950 Size = 0
951951 end
952952 item
953953 Name = 'item'
954954 DataType = ftString
955955 Precision = -1
956956 Size = 8192
957957 end
958958 item
959959 Name = 'price'
960960 DataType = ftFloat
961961 Precision = -1
962962 Size = 0
963963 end>
964964 left = 66
965965 top = 141
966966 object TSalesitemNum: TStringField
967967 DisplayWidth = 8192
968968 FieldKind = fkData
969969 FieldName = 'itemNum'
970970 Index = 0
971971 LookupCache = False
972972 ProviderFlags = [pfInUpdate, pfInWhere]
973973 ReadOnly = False
974974 Required = False
975975 Size = 8192
976976 end
977977 object TSalesID: TAutoIncField
978978 DisplayWidth = 10
979979 FieldKind = fkData
980980 FieldName = 'ID'
981981 Index = 1
982982 LookupCache = False
983983 ProviderFlags = [pfInUpdate, pfInWhere]
984984 ReadOnly = False
985985 Required = False
986986 end
987987 object TSalescustID: TLongintField
988988 DisplayWidth = 10
989989 FieldKind = fkData
990990 FieldName = 'custID'
991991 Index = 2
992992 LookupCache = False
993993 ProviderFlags = [pfInUpdate, pfInWhere]
994994 ReadOnly = False
995995 Required = False
996996 end
997997 object TSalessaleDate: TDateField
998998 DisplayWidth = 10
999999 FieldKind = fkData
10001000 FieldName = 'saleDate'
10011001 Index = 3
10021002 LookupCache = False
10031003 ProviderFlags = [pfInUpdate, pfInWhere]
10041004 ReadOnly = False
10051005 Required = False
10061006 end
10071007 object TSalesshipDate: TDateField
10081008 DisplayWidth = 10
10091009 FieldKind = fkData
10101010 FieldName = 'shipDate'
10111011 Index = 4
10121012 LookupCache = False
10131013 ProviderFlags = [pfInUpdate, pfInWhere]
10141014 ReadOnly = False
10151015 Required = False
10161016 end
10171017 object TSalesitem: TStringField
10181018 DisplayWidth = 8192
10191019 FieldKind = fkData
10201020 FieldName = 'item'
10211021 Index = 5
10221022 LookupCache = False
10231023 ProviderFlags = [pfInUpdate, pfInWhere]
10241024 ReadOnly = False
10251025 Required = False
10261026 Size = 8192
10271027 end
10281028 object TSalesprice: TFloatField
10291029 DisplayWidth = 10
10301030 FieldKind = fkData
10311031 FieldName = 'price'
10321032 Index = 6
10331033 LookupCache = False
10341034 ProviderFlags = [pfInUpdate, pfInWhere]
10351035 ReadOnly = False
10361036 Required = False
10371037 Currency = True
10381038 MaxValue = 0
10391039 MinValue = 0
10401040 Precision = -1
10411041 end
10421042 end
10431043 object TStock: TSqlite3Dataset
10441044 AutoIncrementKey = True
10451045 Options = []
10461046 PrimaryKey = 'ID'
10471047 SaveOnClose = True
10481048 SaveOnRefetch = True
10491049 SQL = 'Select * from stock;'
10501050 TableName = 'stock'
10511051 FieldDefs = <
10521052 item
10531053 Name = 'ID'
10541054 DataType = ftAutoInc
10551055 Precision = -1
10561056 Size = 0
10571057 end
10581058 item
10591059 Name = 'item'
10601060 DataType = ftString
10611061 Precision = -1
10621062 Size = 8192
10631063 end
10641064 item
10651065 Name = 'price'
10661066 DataType = ftFloat
10671067 Precision = -1
10681068 Size = 0
10691069 end
10701070 item
10711071 Name = 'itemNum'
10721072 DataType = ftString
10731073 Precision = -1
10741074 Size = 8192
10751075 end>
10761076 left = 66
10771077 top = 214
10781078 object TStockID: TAutoIncField
10791079 DisplayWidth = 10
10801080 FieldKind = fkData
10811081 FieldName = 'ID'
10821082 Index = 0
10831083 LookupCache = False
10841084 ProviderFlags = [pfInUpdate, pfInWhere]
10851085 ReadOnly = False
10861086 Required = False
10871087 end
10881088 object TStockitem: TStringField
10891089 DisplayWidth = 8192
10901090 FieldKind = fkData
10911091 FieldName = 'item'
10921092 Index = 1
10931093 LookupCache = False
10941094 ProviderFlags = [pfInUpdate, pfInWhere]
10951095 ReadOnly = False
10961096 Required = False
10971097 Size = 8192
10981098 end
10991099 object TStockprice: TFloatField
11001100 DisplayWidth = 10
11011101 FieldKind = fkData
11021102 FieldName = 'price'
11031103 Index = 2
11041104 LookupCache = False
11051105 ProviderFlags = [pfInUpdate, pfInWhere]
11061106 ReadOnly = False
11071107 Required = False
11081108 Currency = True
11091109 MaxValue = 0
11101110 MinValue = 0
11111111 Precision = -1
11121112 end
11131113 object TStockitemNum: TStringField
11141114 DisplayWidth = 8192
11151115 FieldKind = fkData
11161116 FieldName = 'itemNum'
11171117 Index = 3
11181118 LookupCache = False
11191119 ProviderFlags = [pfInUpdate, pfInWhere]
11201120 ReadOnly = False
11211121 Required = False
11221122 Size = 8192
11231123 end
11241124 end
11251125end
11261126```
11271127
11281128``` pascal
11291129unit uCust;
11301130
11311131{$mode objfpc}{$H+}
11321132
11331133interface
11341134
11351135uses
11361136 Classes, SysUtils, FileUtil, LResources, Forms, Controls, Graphics, Dialogs,
11371137 StdCtrls, ExtCtrls, Buttons, DBGrids,DB;
11381138
11391139type
11401140
11411141 { TcustDlg }
11421142
11431143 TcustDlg = class(TForm)
11441144 cancelBtn: TButton;
11451145 dgCust: TDBGrid;
11461146 edSearch: TEdit;
11471147 Label1: TLabel;
11481148 okBtn: TButton;
11491149 pnlCust: TPanel;
11501150 sbSearch: TSpeedButton;
11511151 procedure dgCustDblClick(Sender: TObject);
11521152 procedure edSearchChange(Sender: TObject);
11531153 procedure sbSearchClick(Sender: TObject);
11541154 private
11551155 function GetCust: String;
11561156 procedure SetCust(const AValue: String);
11571157 { private declarations }
11581158 public
11591159 { public declarations }
11601160 property CustName: String Read GetCust Write SetCust;
11611161 function ShowModalCust:integer;
11621162 end;
11631163
11641164var
11651165 custDlg: TcustDlg;
11661166
11671167implementation
11681168uses uMain;
11691169{ TcustDlg }
11701170
11711171procedure TcustDlg.edSearchChange(Sender: TObject);
11721172begin
11731173 sbSearch.Enabled:=edSearch.Text<>'';
11741174end;
11751175
11761176procedure TcustDlg.dgCustDblClick(Sender: TObject);
11771177begin
11781178 ModalResult := mrOk;
11791179end;
11801180
11811181procedure TcustDlg.sbSearchClick(Sender: TObject);
11821182begin
11831183 if not fMain.TCust.Locate('CustName', edSearch.Text,[loCaseInsensitive, loPartialKey])
11841184 then
11851185 MessageDlg('No matching record found.', mtInformation, [mbOK], 0);
11861186 edSearch.Color:=clRed;
11871187end;
11881188
11891189function TcustDlg.GetCust: String;
11901190begin
11911191 Result := fMain.TCustCustName.Value;
11921192end;
11931193
11941194procedure TcustDlg.SetCust(const AValue: String);
11951195begin
11961196 fMain.TCust.Locate('CustName',AValue,[loPartialKey,loCaseInsensitive]);
11971197end;
11981198
11991199function TcustDlg.ShowModalCust: integer;
12001200begin
12011201 Caption:='Select Customer Name';
12021202 Result := ShowModal;
12031203end;
12041204
12051205initialization
12061206 {$I uCust.lrs}
12071207
12081208end.
12091209```
12101210
12111211``` pascal
12121212unit uSales;
12131213
12141214{$mode objfpc}{$H+}
12151215
12161216interface
12171217
12181218uses
12191219 Classes, SysUtils, FileUtil, LResources, Forms, Controls, Graphics, Dialogs,
12201220 StdCtrls, ExtCtrls, Buttons, DBGrids, DB;
12211221
12221222type
12231223
12241224 { TsearchDlg }
12251225
12261226 TsearchDlg = class(TForm)
12271227 cancelBtn: TButton;
12281228 dgParts: TDBGrid;
12291229 edSearch: TEdit;
12301230 Label1: TLabel;
12311231 okBtn: TButton;
12321232 pnlParts: TPanel;
12331233 sbSearch: TSpeedButton;
12341234 procedure dgPartsDblClick(Sender: TObject);
12351235 procedure edSearchChange(Sender: TObject);
12361236 procedure sbSearchClick(Sender: TObject);
12371237 private
12381238 function GetPartNum: String;
12391239 procedure SetPartNum(const AValue: String);
12401240 { private declarations }
12411241 public
12421242 { public declarations }
12431243 property PartNum:String Read GetPartNum Write SetPartNum;
12441244 function ShowModalParts: Integer;
12451245 end;
12461246
12471247var
12481248 searchDlg: TsearchDlg;
12491249
12501250implementation
12511251uses uMain;
12521252{ TsearchDlg }
12531253
12541254procedure TsearchDlg.edSearchChange(Sender: TObject);
12551255begin
12561256 sbSearch.Enabled:=edSearch.Text<>'';
12571257end;
12581258
12591259procedure TsearchDlg.dgPartsDblClick(Sender: TObject);
12601260begin
12611261 ModalResult := mrOk;
12621262end;
12631263
12641264procedure TsearchDlg.sbSearchClick(Sender: TObject);
12651265begin
12661266 if not fMain.TStock.Locate('itemNum', edSearch.Text,[loCaseInsensitive, loPartialKey])
12671267 then
12681268 MessageDlg('No matching record found.', mtInformation, [mbOK], 0);
12691269 edSearch.Color:=clRed;
12701270end;
12711271
12721272function TsearchDlg.GetPartNum: String;
12731273begin
12741274 Result := fMain.TStockitemNum.Value;
12751275end;
12761276
12771277procedure TsearchDlg.SetPartNum(const AValue: String);
12781278begin
12791279 fMain.TStock.Locate('itemNum',AValue,[loPartialKey,loCaseInsensitive]);
12801280end;
12811281
12821282function TsearchDlg.ShowModalParts: Integer;
12831283begin
12841284 Caption:='Select Item Number';
12851285 Result := ShowModal;
12861286end;
12871287
12881288initialization
12891289 {$I uSales.lrs}
12901290
12911291end.
12921292```
12931293
12941294---
12951295*From the Free Pascal wiki, page "TSqlite3 Master Detail Example/fr", as it stood on 2020-03-02 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).*

Back to history