Perl300 has asked for the wisdom of the Perl Monks concerning the following question:
I am using "use DBI" on perl v5.8.8 to update MySQL database table.
Statement I am using is like this:
my $sql6 = "UPDATE table_name SET good_high = $threshold_good_high, c +ritical_low = $threshold_critical_low, warning_low = $threshold_warni +ng_low, warning_high = $threshold_warning_high WHERE user = ? AND nam +e = '$threshold_name'"; my $sth6 = $dbh->prepare($sql6); $sth6->execute($userName); $update_result1 = $sth6->rows; print "\$update_result1: $update_result1\n";
Here I am actualy trying to get number of rows changed by this update command (which will vary from 0 to 29 in my case). But rows is always giving me 29 as result even if no row was changed. Which I think is expected as per documentation, but I didn't understand it 100%.
Per documentation of DBI: Returns the number of rows affected by the last row affecting command, or -1 if the number of rows is not known or not available. Generally, you can only rely on a row count after a non-SELECT execute (for some specific operations like UPDATE and DELETE), or after fetching all the rows of a SELECT statement.
So, I tried using:
my $sth6 = $dbh->do("UPDATE table_name SET good_high = $threshold_good +_high, critical_low = $threshold_critical_low, warning_low = $thresho +ld_warning_low, warning_high = $threshold_warning_high WHERE user = ? + AND name = '$threshold_name'"); print "\$sth6: $sth6\n";
UPDATE(discard):But in the above case it prints nothing as $sth6
UPDATE(consider):But in the above case ALSO it prints 29 as $sth6. I had missed providing $userName earlier.
Can you please suggest me what am I missing here and a way to get count of rows changed by UPDATE command.
|
|---|