Forum > Databases
Update table at run-time
(1/1)
babycode:
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 [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---procedure GravarMD5P(session_p: string; u_id: integer);var qryTemp: TZQuery; conTemp: TZConnection;begin qryTemp := TZQuery.Create(Nil); conTemp := CriarConexaoSec; qryTemp.Connection := conTemp; qryTemp.CachedUpdates := False; ConTemp.Connect; try with qryTemp do begin SQL.Clear; Connection.StartTransaction; SQL.Add('update usuarios set sessao_p = :sSession_p where usuario_id = :iUsuarioID;'); ParamByName('sSession_p').AsString := 'ttaassttes'; ParamByName('iUsuarioID').AsInteger := 1; try ExecSQL; ApplyUpdates; Connection.Commit; except on E: Exception do begin ShowMessage('Erro ao gravar dados: ' + E.Message); Connection.Rollback; // Desfaz a transação em caso de erro end; end; end; finally conTemp.Free; qryTemp.Free; end;end;
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:
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:
--- Quote from: egsuh 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.
--- End quote ---
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 [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---procedure GravarMD5P(session_p: string; u_id: integer);var qryTemp: TZQuery; // defines the type of TZQuery variable conTemp: TZConnection; begin 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.
Navigation
[0] Message Index