简体   繁体   English

检查更新查询是否影响perl行

[英]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. 我有一个“ UPDATE”查询,我想检查该查询是否影响了任何行,是否希望向自己发送电子邮件。 I'm not really experienced with perl or sql. 我对Perl或sql没有真正的经验。 (I'm using postgresql). (我正在使用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. 编辑:我知道UPDATE返回“计数”,它告诉多少行,如果有任何已更新,但我仍然不知道如何获取它。

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? 问题是我不知道要在if中放入什么,应该设置什么标志? Is there any easy check that answers "has effected rows"? 是否有简单的答案可以回答“已影响行”?

Shortcut version using do() 使用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. 完整的prepare() / execute()版本。

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
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM