How to check if values ​​were changed after upgrade?

Assuming I'm doing something like the following:

my $rows = $dbh->do("UPDATE MYTABLE SET amount=1200 WHERE id =123"); 

$rowsreturns 1, even if the number is already 1200. Thus, this is considered an updated string.
My question is: is there a way to check if the update really changed the values ​​in the row besides executing the request before updating?

+4
source share
3 answers

Change the SQL query to:

UPDATE MYTABLE SET amount=1200 WHERE id = 123 AND amount <> 1200

The table will be identical, but returns the number of rows that actually changed.

+8
source

Twinkles , prepare, execute .

my $update_if_changed = $dbh->prepare('UPDATE mytable SET amount = ? WHERE id = ? AND amount != ?')

$update_if_changed->execute($amount, $id, $amount)
+1

By default, DBD :: mysql returns the number of rows matched in UPDATE, not the number of physically modified rows. You can change this behavior by disabling mysql_client_found_rowsin your call to connect:

my $dsn = "DBI:mysql:;mysql_client_found_rows=0";
my $dbh = DBI->connect($dsn, $user, $password);
+1
source

Source: https://habr.com/ru/post/1531163/


All Articles