在 PostgreSQL 中获取受更新影响的行数 [英] Getting number of rows affected by an UPDATE in PostgreSQL

查看:424
本文介绍了在 PostgreSQL 中获取受更新影响的行数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

已经在 SO 和许多博客上询问了这个问题的变体,但没有一个提供直接的答案.我希望有一个.我正在更新 PostgreSQL 9.0(来自 CodeIgniter,PHP 框架):

variations of this question has been asked on SO and on many blogs but none offers a straight-forward answer. I hope there is one. I am updating PostgreSQL 9.0 (from CodeIgniter, PHP framework):

$sql_order = "UPDATE meters SET billed=true"; 
$query = $this->db->query($sql_order);

我只需要计算受更新影响的行数,但 PostgreSQL 似乎无法做到这一点.查询现在返回一个布尔值 - true.手册和网络讨论参考了 RETURNING 语法、GET DIAGNOSTICS 和来自 UPDATE 的默认返回类型.我一直无法让这些工作.有没有一种直接的方法可以让行影响计数,而不必将这个简单的操作嵌入到过程或事务中.

I simply need a count of rows that were affected by the update, but there seems to be no way to do this with PostgreSQL. The query is now returning a boolean - true. The manual and web talk refer to the RETURNING syntax, to GET DIAGNOSTICS, and to a default return type from UPDATE. I haven't been able to get any of these to work. Is there a straightforward way of getting rows affect count without having to embed this simple operation into a procedure or transaction.

推荐答案

在 Java 中,我会使用以下内容:

In Java I would have used the following:

Statement stmt = connection.createStatement();
int rowsAffected = stmt.executeUpdate("UPDATE ...");

在 PHP 中,我相信 pg_affected_rows 是办法.在您的特定情况下 $this->db->affected_rows()

In PHP I believe pg_affected_rows is the way. And in your particular case $this->db->affected_rows()

这篇关于在 PostgreSQL 中获取受更新影响的行数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆