Username Username - 7 months ago 12
Perl Question

Checking if update query affected rows perl

I have an "UPDATE" query and I want to check if this query has affected any rows and if it has I would like to send an email to myself. I'm not really experienced with perl or sql. (I'm using postgresql).

EDIT: I know that UPDATE returns "count" which tells how many rows if any have been updated, but I still don't know how to get to it.

The code looks like this:

my $updateQuery - //UPDATE query
if(//updateQuery has effected rows){
//send mail
}else
//do nothing


The problem is I don't know what to put into the if, what flag should I set? Is there any easy check that answers "has effected rows"?

Answer

Shortcut version using do()

my $update_query = '...';

my $ret = $dbh->do($update_query);

if ($ret) {
  if ($ret eq '0E0') {
    # no rows updated
  } else {
    # rows updated
  }
} else {
  # error
}

Full prepare()/execute() version.

my $update_query = '...';
my $sth = $dbh->prepare($update_query);

my $ret = $sth->execute;

if ($ret) {
  if ($ret eq '0E0') {
    # no rows updated
  } else {
    # rows updated
  }
} else {
  # error
}
Comments