createusers.lpr 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. program createusers;
  2. {$mode objfpc}{$H+}
  3. uses
  4. {$IFDEF UNIX}{$IFDEF UseCThreads}
  5. cthreads,
  6. {$ENDIF}{$ENDIF}
  7. Classes, SysUtils, dbf, CustApp, db
  8. { you can add units after this };
  9. type
  10. { TMyApplication }
  11. TMyApplication = class(TCustomApplication)
  12. private
  13. procedure CreateUsers(DS: TDataset);
  14. protected
  15. procedure DoRun; override;
  16. public
  17. constructor Create(TheOwner: TComponent); override;
  18. destructor Destroy; override;
  19. procedure WriteHelp; virtual;
  20. end;
  21. { TMyApplication }
  22. Type
  23. TUserRecord = Record
  24. L,N,E : string;
  25. end;
  26. Const
  27. UserCount = 8;
  28. Users : Array[1..UserCount] of TUserRecord = (
  29. (L:'Daniel';N:'Daniel mantione'; E:'[email protected]'),
  30. (L:'Florian';N:'Florian Klaempfl'; E:'[email protected]'),
  31. (L:'Joost';N:'Joost van der Sluis'; E:'[email protected]'),
  32. (L:'Jonas';N:'Jonas Maebe'; E:'[email protected]'),
  33. (L:'Michael';N:'Michael van canneyt'; E:'[email protected]'),
  34. (L:'Marco';N:'Marco Van De Voort'; E:'[email protected]'),
  35. (L:'Pierre';N:'Pierre Muller'; E:'[email protected]'),
  36. (L:'Tomas';N:'Tomas Hajny'; E:'[email protected]')
  37. ) ;
  38. procedure TMyApplication.CreateUsers(DS : TDataset);
  39. Var
  40. I : integer;
  41. begin
  42. For I:=1 to UserCount do
  43. begin
  44. DS.Append;
  45. DS.FieldByName('Login').AsString:=Users[i].L;
  46. DS.FieldByName('Name').AsString:=Users[i].N;
  47. DS.FieldByName('Email').AsString:=Users[i].E;
  48. If Random(2)<1 then
  49. DS.FieldByname('LastLogin').AsDatetime:=Date-Random(10);
  50. DS.Post;
  51. end;
  52. end;
  53. procedure TMyApplication.DoRun;
  54. var
  55. ErrorMsg: String;
  56. DB : TDBF;
  57. begin
  58. // quick check parameters
  59. ErrorMsg:=CheckOptions('h','help');
  60. if ErrorMsg<>'' then begin
  61. ShowException(Exception.Create(ErrorMsg));
  62. Terminate;
  63. Exit;
  64. end;
  65. // parse parameters
  66. if HasOption('h','help') then begin
  67. WriteHelp;
  68. Terminate;
  69. Exit;
  70. end;
  71. { add your program here }
  72. DB:=TDBF.Create(Self);
  73. try
  74. With DB.FieldDefs do
  75. begin
  76. Add('ID',ftAutoInc,0,True);
  77. Add('Login',ftString,30,true);
  78. Add('Name',ftString,50,True);
  79. Add('Email',ftString,50,False);
  80. Add('LastLogin',ftDate,0,False);
  81. end;
  82. DB.TableName:='users.dbf';
  83. DB.TableLevel:=7;
  84. DB.CreateTable;
  85. DB.Open;
  86. CreateUsers(DB);
  87. finally
  88. DB.Free;
  89. end;
  90. // stop program loop
  91. Terminate;
  92. end;
  93. constructor TMyApplication.Create(TheOwner: TComponent);
  94. begin
  95. inherited Create(TheOwner);
  96. StopOnException:=True;
  97. end;
  98. destructor TMyApplication.Destroy;
  99. begin
  100. inherited Destroy;
  101. end;
  102. procedure TMyApplication.WriteHelp;
  103. begin
  104. { add your help code here }
  105. writeln('Usage: ',ExeName,' -h');
  106. end;
  107. var
  108. Application: TMyApplication;
  109. {$IFDEF WINDOWS}{$R createusers.rc}{$ENDIF}
  110. {$R *.res}
  111. begin
  112. Application:=TMyApplication.Create(nil);
  113. Application.Title:='My Application';
  114. Application.Run;
  115. Application.Free;
  116. end.