【问题标题】:Gives error DBD::mysql::st fetchrow_array failed: fetch() without execute() only for DELETE statement给出错误 DBD::mysql::st fetchrow_array failed: fetch() without execute() only for DELETE statement
【发布时间】:2015-12-11 08:11:52
【问题描述】:

这个isdb连接,

$DBH = DBI->connect("dbi:mysql:$DATABASE","$USER","$PASS",{ RaiseError => 1, AutoCommit => 1 }) or die "Connection Error: $DBI::errstr\n";

以及下面的代码,

$sth=$DBH->prepare("DELETE FROM sample where id=1 ") or warn $DBH->errstr;
$sth->execute or die "can't execute the query: $sth->errstr";
while(@row = $sth->fetchrow_array()){
                $count+=$sth->rows;
}

上面的代码给了我错误,

DBD::mysql::st fetchrow_array 失败:fetch() without execute()..

但是,当我使用 select * from sample where id=1 时,它会运行。它只给我delete statement 的错误。为什么这样? 帮助 !谢谢!

【问题讨论】:

  • delete 语句不返回任何行。它只返回它已删除的行数。
  • @Tensibai 我们可以计算删除的行数
  • 尝试搜索文档,我没有做 perl 但我在 DBI 文档中找到了this

标签: perl dbi


【解决方案1】:

您不能使用删除指令执行 fetchrow,它仅用于检索数据。
你认为它应该返回什么?

来自 DBI 文档

对于非 SELECT 语句,execute 返回受影响的行数(如果已知)。如果没有行受到影响,则执行返回“0E0”,Perl 将其视为 0 但将视为真。请注意,没有行受语句影响并不是错误。如果不知道受影响的行数,则执行返回 -1。

您不需要执行 fetchrow,只需:

my $affected_row = $sth->execute or die "can't execute the query: $sth->errstr";

【讨论】:

  • 我想计算删除行数。它不返回受影响的行。也删除一些影响事件吧。
  • 我要统计删除的行数。是否可以统计受影响的行数。
  • @Pranay:正如文档中所说,0E0 的返回值意味着没有行受到影响(即没有行被删除)。
  • 如果行被删除,那么它会计算行数。
  • @Pranay:是的,会的。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-09-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-06-13
  • 2023-04-11
相关资源
最近更新 更多