Bookstore

Recent

Author Topic: Error in working code I can not find.  (Read 1549 times)

xaver13

  • Full Member
  • ***
  • Posts: 106
Error in working code I can not find.
« on: March 13, 2017, 10:46:10 am »
Hello,
I am using INI file. When I debug the program
Code: Pascal  [Select]
  1. unit uStart;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, SysUtils, mysql56conn, sqldb, FileUtil, Forms, Controls, Graphics,
  9.   Dialogs, StdCtrls, LazLogger, IniFiles, LCLTranslator, uZadavani, uKnihovna;
  10. //LazLoggerDummy  //zaměnit pro zákaz logování
  11.  
  12. type
  13.  
  14.   { TFormPrihlaseni }
  15.  
  16.   TFormPrihlaseni = class(TForm)
  17.     btnKonec: TButton;
  18.     EditKod: TEdit;
  19.     Label1: TLabel;
  20.     MySQLConnection1: TMySQL56Connection;
  21.     Query: TSQLQuery;
  22.     SQLTransaction1: TSQLTransaction;
  23.     procedure btnKonecClick(Sender: TObject);
  24.     procedure EditKodEditingDone(Sender: TObject);
  25.     procedure FormActivate(Sender: TObject);
  26.     procedure FormDestroy(Sender: TObject);
  27.  
  28.   private
  29.     { private declarations }
  30.   public
  31.     { public declarations }
  32.   end;
  33.  
  34. const
  35.   C_DB_SECTION = 'DB-INFO';
  36.  
  37. var
  38.   FormPrihlaseni: TFormPrihlaseni;
  39.   jmeno, kod: string;
  40.   priorita: integer;
  41.   stationID: string;
  42.  
  43.   mySqlQuery:String;
  44.   INI: TINIFile;
  45.   DBpassword, DBlocation, DBname,DBuser: string;
  46.  
  47. implementation
  48.  
  49. {$R *.lfm}
  50.  
  51. { TFormPrihlaseni }
  52.  
  53. procedure TFormPrihlaseni.EditKodEditingDone(Sender: TObject);
  54. begin
  55.  
  56.   MySQLConnection1.Open;
  57.   if MySQLConnection1.Connected then
  58.   begin
  59.     Query.Database := MySQLConnection1;
  60.     SQLTransaction1.Database := MySQLConnection1;
  61.     Debugln('Connected to server: ' + MySQLConnection1.HostName);
  62.     Query.Close;
  63.     //Prepared statement:
  64.     Query.SQL.Text := 'Select * from Operator where kod = (:KOD)';
  65.     Query.Params.ParamByName('KOD').AsString := EditKod.Text;
  66.     Query.Open;
  67.     while not Query.EOF do
  68.     begin
  69.       (* DbgOut(Query.Fields[0].AsString);
  70.       DbgOut(Query.Fields[1].AsString);
  71.       DbgOut(Query.Fields[2].AsString);
  72.       DbgOut(Query.Fields[3].AsString);
  73.       DbgOut(Query.Fields[4].AsString);
  74.       Debugln(Query.Fields[5].AsString);
  75.       *)
  76.       Debugln('ID: ', Query.FieldByName('ID').AsString,
  77.         ' Operator: ' + Query.FieldByName('Operator').AsString,
  78.         ' Kod: ' + Query.FieldByName('Kod').AsString, ' Skupina: ' +
  79.         Query.FieldByName('Skupina').AsString);
  80.       kod := Query.FieldByName('Kod').AsString;
  81.       jmeno := Query.FieldByName('Operator').AsString;
  82.       priorita := Query.FieldByName('Priorita').AsInteger;
  83.       Query.Next;
  84.       Debugln('Konec dat uživatele');
  85.     end;
  86.     Query.Close;
  87.   end;
  88.   if kod = EditKod.Text then
  89.   begin
  90.     EditKod.Text :='';
  91.     EditKod.SetFocus;
  92.     //ShowMessage('Uživatel ověřen.');
  93.     FormZadavani := TFormZadavani.Create(nil);
  94.     FormZadavani.ShowModal;
  95.     FormZadavani.Free;
  96.   end
  97.   else
  98.   begin
  99.     //ShowMessage('Uživatel neověřen.');
  100.     Query.Close;
  101.     Close;
  102.   end;
  103.  
  104. end;
  105.  
  106. procedure TFormPrihlaseni.btnKonecClick(Sender: TObject);
  107. begin
  108.   close;
  109. end;
  110.  
  111.  
  112.  
  113. procedure TFormPrihlaseni.FormActivate(Sender: TObject);
  114. begin
  115.   SetDefaultLang('cs');
  116.   //ShowMessage(GetDefaultLang);
  117.   stationID := GetEnvironmentVariable('COMPUTERNAME') + ' ' +
  118.     GetEnvironmentVariable('USERNAME');
  119.   Debugln('*** Start programu: ' + stationID);
  120.   // Create the object, specifying the the ini file that contains the settings
  121.   INI := TINIFile.Create('DMySQL.ini');
  122.   // Put reading the INI file inside a try/finally block to prevent memory leaks
  123.   try
  124.     // reading values from the INI file.
  125.     DBuser     := INI.ReadString(C_DB_SECTION, 'DBuser', '');
  126.     DBpassword := INI.ReadString(C_DB_SECTION, 'DBpassword', '');
  127.     DBlocation := INI.ReadString(C_DB_SECTION, 'DBlocation', '');
  128.     DBname     := INI.ReadString(C_DB_SECTION, 'DBname', '');
  129.   finally
  130.     // After the ini file was used it must be freed to prevent memory leaks.
  131.     INI.Free;
  132.   end;
  133.   Debugln(DBuser + ' ' + DBpassword + ' ' + DBlocation+' ' + DBname);
  134.   DBpassword := Chrtran(DBpassword, '90m7n8o4pqr1stefgh0ijklu23vwx6yzab5cd',
  135.     '6ab903d1efg8h2ijklm7nop4qrstu5v0wxyz');
  136.   ShowMessage(DBuser + ' ' + DBpassword + ' ' + DBlocation+' ' + DBname);
  137.  
  138.   MySQLConnection1.HostName := DBlocation;
  139.   MySQLConnection1.UserName := DBuser;
  140.   MySQLConnection1.Password := DBpassword;
  141.   MySQLConnection1.DatabaseName := DBname;
  142.  
  143.  
  144.   MySQLConnection1.Transaction := SQLTransaction1;
  145.   Debugln('*** Přihlášení ' + RRRRMMDD(now));
  146.   MySQLConnection1.Open;
  147.   if MySQLConnection1.Connected then
  148.   begin
  149.     Query.Database := MySQLConnection1;
  150.     SQLTransaction1.Database := MySQLConnection1;
  151.  
  152.     Query.Close;
  153.     //Prepared statement:
  154.     Query.SQL.Text := 'Select * from bezi where jmenopoc = (:JMENOPOC)';
  155.     Query.Params.ParamByName('JMENOPOC').AsString := stationID;
  156.     Query.Open;
  157.     while not Query.EOF do
  158.     begin
  159.       Query.Next;
  160.     end;
  161.     if stationID = Query.FieldByName('JMENOPOC').AsString then
  162.     begin
  163.       mySQLQuery := 'update bezi set ZACATEK=''' + RRRRMMDD(Now) + ''', BEZI = "A" WHERE JMENOPOC = '''+ stationID +'''';
  164.       //SQLTransaction1.StartTransaction;
  165.       MySQLConnection1.ExecuteDirect(mySQLQuery);
  166.       SQLTransaction1.Commit;
  167.     end
  168.     else
  169.     begin
  170.       mySQLQuery :=
  171.         'insert into bezi (ID,JMENOPOC,ZACATEK,BEZI)' +
  172.         ' values(0,''' + stationID +''' , ''' + RRRRMMDD(Now) + ''' , "A" )' ;
  173.       MySQLConnection1.ExecuteDirect(mySQLQuery);
  174.       SQLTransaction1.Commit;
  175.     end;
  176.     Query.Close;
  177.   end;
  178.   end;
  179.  
  180. procedure TFormPrihlaseni.FormDestroy(Sender: TObject);
  181. begin
  182.  
  183.   Query.Close;
  184.   //Prepared statement:
  185.   Query.SQL.Text := 'Select * from bezi where jmenopoc = (:JMENOPOC)';
  186.   Query.Params.ParamByName('JMENOPOC').AsString := stationID;
  187.   Query.Open;
  188.   while not Query.EOF do
  189.   begin
  190.     Query.Next;
  191.   end;
  192.   if stationID = Query.FieldByName('JMENOPOC').AsString then
  193.   begin
  194.     mySQLQuery := 'update bezi set ZACATEK=''' + RRRRMMDD(Now) + ''', BEZI = "N" WHERE JMENOPOC = '''+ stationID +'''';
  195.       //SQLTransaction1.StartTransaction;
  196.       MySQLConnection1.ExecuteDirect(mySQLQuery);
  197.       SQLTransaction1.Commit;
  198.   end;
  199.   Query.Close;
  200.  
  201. end;
  202.  
  203.  
  204. end.
It runs but when I stop it debugging like shown on attached picture, I don't see values of DBuser, DBpassword,DBlocation ... only the DBname from ini file in DEBUG window.  All these INI values are show on show message box and code is working. When I debug it on Windows I see all values. When I wrote only a small sample  www.cvrk.com/testini.zip I can see all INI values in debug window as expected. Therefore I think that must be an error in the first snippet of code here. But I can not find it. Please have you any idea?
« Last Edit: March 13, 2017, 01:17:16 pm by xaver13 »
--
Jiri Cvrk