当我使用 DBI 的准备/执行非 select 语句时,如何获取受影响的行数?

发布于 2024-11-27 18:54:07 字数 274 浏览 2 评论 0原文

根据 DBI 文档,看来我只能通过 do方法。

$rows_affected = $dbh->do("UPDATE your_table SET foo = foo + 1");

如果我使用prepare/execute如何才能得到相同的结果?

According to the DBI documentation, it seems I can only get the number of affected rows by the do method.

$rows_affected = $dbh->do("UPDATE your_table SET foo = foo + 1");

How can I get the same result if I use prepare/execute?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

指尖凝香 2024-12-04 18:54:07

从有关 DBI 中的 execute 方法 的文档中:

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

From the documentation about the execute method in DBI:

For a non-"SELECT" statement, "execute" returns the number of rows
affected, if known. If no rows were affected, then "execute" returns
"0E0", which Perl will treat as 0 but will regard as true. Note that
it is
not an error for no rows to be affected by a statement. If
the number of rows affected is not known, then "execute" returns -1.

木有鱼丸 2024-12-04 18:54:07

如果您的查询是非SELECT查询(例如UPDATE或DELETE),那么您可以利用

my $query = "...";  # your query
my $sth = $dbh->prepare($query);
$sth->execute();
print "Number of rows affected: " . $sth->rows . "\n";

返回受最后一个影响的行数查询或-1 如果出现错误。但是,根据设计,您不能依赖来执行 SELECT 语句。

请注意,对于非 SELECT 查询,还要执行 返回受影响的行数。但是,如果没有行受到影响,则 execute 返回 "0E0"(Perl 无论如何都应将其视为 0)。

my $query = "...";  # your query
my $sth = $dbh->prepare($query);
my $numrows = $sth->execute();
print "Number of rows affected: " . $numrows . "\n";

相反,如果您的查询是SELECT,那么您就不能依赖

但是,您可以执行以下任一操作:

my $query = "SELECT COUNT(*) AS rows FROM ... WHERE ...";
my $numrows = $dbh->selectrow_array($query, undef);
print "Number of rows: " . $numrows . "\n";

或者,类似地:

my $query = "SELECT COUNT(*) AS rows FROM ... WHERE ...";
my $numrows = $dbh->selectall_arrayref($query, { Slice => {} });
print "Number of rows: " . @$numrows[0]->{rows} . "\n";

If your query is a non-SELECT one (e.g. UPDATE or DELETE), then you can take advantage of rows:

my $query = "...";  # your query
my $sth = $dbh->prepare($query);
$sth->execute();
print "Number of rows affected: " . $sth->rows . "\n";

rows returns the number of rows affected by the last query or -1 in case of error. However, by design, you cannot rely on rows for SELECT statement.

Note that, for non-SELECT queries, also execute returns the number of rows affected. However, if no row is affected, then execute returns "0E0" (which Perl should anyway treat as 0).

my $query = "...";  # your query
my $sth = $dbh->prepare($query);
my $numrows = $sth->execute();
print "Number of rows affected: " . $numrows . "\n";

If, instead, your query is a SELECT, then you cannot rely on rows.

However, you can do either:

my $query = "SELECT COUNT(*) AS rows FROM ... WHERE ...";
my $numrows = $dbh->selectrow_array($query, undef);
print "Number of rows: " . $numrows . "\n";

Or, similarly:

my $query = "SELECT COUNT(*) AS rows FROM ... WHERE ...";
my $numrows = $dbh->selectall_arrayref($query, { Slice => {} });
print "Number of rows: " . @$numrows[0]->{rows} . "\n";
清欢 2024-12-04 18:54:07

正如user153275所说:

“这似乎不再正确,至少在版本 4.007 中是这样。
执行返回的是匹配的行数,而不是受影响的行数。”

我在此链接中找到了一个有用的解决方案,添加了 where 子句 AND (columnName <> newValue):
https://www.perlmonks.org/?node_id=1141381

这样查询只会找到要更改的行。

As user153275 said:

"This doesn't seem to be correct any longer, at least in version 4.007.
Execute is returning the number of matched rows, not the number of affected rows."

I found a useful solution in this link, adding the where clause AND (columnName <> newValue):
https://www.perlmonks.org/?node_id=1141381

In this way the query will find only the rows to change.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文