Question about RowsAffected

In this forum you may discuss all issues concerning the Lazarus IDE and Freepascal (both running on Windows or Linux).

Moderators: gto, cipto_kh, EgonHugeist

Post Reply
Noob
Fresh Boarder
Fresh Boarder
Posts: 1
Joined: 03.07.2020, 22:23

Question about RowsAffected

Post by Noob »

Hi.
I'm using Zeos and Lazarus to access a mySql database on the cloud.

This is the code I wrote:

Code: Select all

ZQuery3.SQL.Text:= 'UPDATE 2_banco SET confere="A" , nome = "'+vnome+'", cpftel = "'+ vCpf +'", dia = "'+dia+'", hora = "'+hora+'" where codEntrev = ' + vTexto +' and cpftel is null';
                    
            try
  		       ZQuery3.ExecSQL;
  		    except
  		          On E:Exception do
  		          controle :=1;
  		    end;
  	            if controle <> 1
  		    then
  		    begin                        
                if Zquery3.RowsAffected > 0 then
                Zquery3.
                begin
					Memo1.Lines.Add(vTexto+vcspro+vcspro1);
                     
                    ZQuery9.SQL.Text:= 'INSERT INTO 2_lista_reserva (cpf, idoso, dia, hora, gravacao) VALUES ("'+ vCpf+'", "'+ vtexto +'", "'+dia+'", "'+hora+'", "M")' ;
                    try
						ZQuery9.ExecSQL;
					except
						On E:Exception do
						controle :=0;
					end;
                end;     
			endelse
			begin
			...
			end;
I have many users accessing the table at the same time. Now, my problem is when I compared my log with the databse I noticed that 30 registries were updated without going in the

Code: Select all

if Zquery3.RowsAffected > 0 then


Do you know why that happened? Am I missing something?
Any insight would be apreciated.
User avatar
aehimself
Zeos Dev Team
Zeos Dev Team
Posts: 796
Joined: 18.11.2018, 17:37
Location: Hungary

Re: Question about RowsAffected

Post by aehimself »

That's a really bad way of handling exceptions and program flow as it is. The general rule of exceptions is not to swallow exceptions.

What if you try...

Code: Select all

ZQuery3.SQL.Text:= 'UPDATE 2_banco SET confere="A" , nome = "'+vnome+'", cpftel = "'+ vCpf +'", dia = "'+dia+'", hora = "'+hora+'" where codEntrev = ' + vTexto +' and cpftel is null';
ZQuery3.ExecSQL;
if Zquery3.RowsAffected > 0 then
	begin
		Memo1.Lines.Add(vTexto+vcspro+vcspro1);
		ZQuery9.SQL.Text:= 'INSERT INTO 2_lista_reserva (cpf, idoso, dia, hora, gravacao) VALUES ("'+ vCpf+'", "'+ vtexto +'", "'+dia+'", "'+hora+'", "M")' ;
		ZQuery9.ExecSQL;
	end else
			begin
			...
			end;
[/quote]

...and just catch exceptions in the calling method?
Delphi 12.2, Zeos 8 from latest GIT snapshot
Using:
- MySQL server 8.0.18; libmysql.dll 8.0.40 x64 5.7.19 x68, libmariadb.dll 3.3.11
- Oracle server 11.2.0, 12.1.0, 19.0.0; oci.dll 21.15
- MSSQL 2012, 2019; sybdb.dll FreeTDS_3102
- SQLite 3.47
Post Reply