Recent

Author Topic: Update table at run-time  (Read 539 times)

babycode

  • New Member
  • *
  • Posts: 17
Update table at run-time
« on: May 22, 2024, 01:17:53 am »
I'm trying to update a table at runtime using a temporary Query. In the end it does not show any errors or exceptions, but when I query the table the information has not been updated. Basically I'm updating just one field, I created the connection and the query at run time, I execute the sql, and I destroy the components, but the information is not updated in the database.  :o

Code: Pascal  [Select][+][-]
  1. procedure GravarMD5P(session_p: string; u_id: integer);
  2. var
  3.         qryTemp: TZQuery;
  4.         conTemp: TZConnection;
  5. begin
  6.         qryTemp := TZQuery.Create(Nil);
  7.         conTemp := CriarConexaoSec;
  8.  
  9.         qryTemp.Connection := conTemp;
  10.         qryTemp.CachedUpdates := False;
  11.         ConTemp.Connect;
  12.  
  13.         try
  14.                 with qryTemp do
  15.                 begin
  16.                         SQL.Clear;
  17.  
  18.                         Connection.StartTransaction;
  19.  
  20.                         SQL.Add('update usuarios set sessao_p = :sSession_p where usuario_id = :iUsuarioID;');
  21.                         ParamByName('sSession_p').AsString := 'ttaassttes';
  22.                         ParamByName('iUsuarioID').AsInteger := 1;
  23.  
  24.                         try
  25.                                 ExecSQL;
  26.                                 ApplyUpdates;
  27.                                 Connection.Commit;
  28.                         except
  29.                                 on E: Exception do
  30.                                 begin
  31.                                         ShowMessage('Erro ao gravar dados: ' + E.Message);
  32.                                         Connection.Rollback; // Desfaz a transação em caso de erro
  33.                                 end;
  34.                         end;
  35.                 end;
  36.         finally
  37.                 conTemp.Free;
  38.                 qryTemp.Free;
  39.         end;
  40. end;
  41.  


I checked if SQL.Text is correct and if it is receiving the values ​​in the procedure parameters and they are. It simply doesn't show an error or exception, it doesn't record  %). I'm using ZeosLib 8. Sorry for not including a minimal example, I'm using PostgreSQL.

egsuh

  • Hero Member
  • *****
  • Posts: 1381
Re: Update table at run-time
« Reply #1 on: May 22, 2024, 05:28:41 am »
1. Where is the qryTemp created?
2. Do not use ApplyUpdates if you are directly operating on a SQL DB with SQL statements.
3. Not sure these are supported in ZQuery, but basics are Transaction.StartTransaction and Transaction.Commit. You are using Connection.StartTransaction and Connection.Commit.

babycode

  • New Member
  • *
  • Posts: 17
Re: Update table at run-time
« Reply #2 on: May 22, 2024, 02:57:34 pm »
1. Where is the qryTemp created?
2. Do not use ApplyUpdates if you are directly operating on a SQL DB with SQL statements.
3. Not sure these are supported in ZQuery, but basics are Transaction.StartTransaction and Transaction.Commit. You are using Connection.StartTransaction and Connection.Commit.

The TZConnection component has the transaction option, but I did a test after your answer and also created a TZTransaction at run time and placed it for the TZConnection. I explicitly controlled the transaction using the transaction created and the information was saved in the bank correctly. Apparently I need the transaction even at run time.

1. The temporary query is created here:

Code: Pascal  [Select][+][-]
  1. procedure GravarMD5P(session_p: string; u_id: integer);
  2. var
  3.         qryTemp: TZQuery; // defines the type of TZQuery variable
  4.         conTemp: TZConnection;
  5. begin
  6.         qryTemp := TZQuery.Create(Nil); //creates an instance of the TZQuery class and assigns it to the variable.

2. I only used ApplyUpdates as a test, but in ZeosLib I generally use Query.CommitUpdates
Transaction.Commit.

 

TinyPortal © 2005-2018