Re: Number of updated rows with LibPQ

From: Laurenz Albe <laurenz(dot)albe(at)cybertec(dot)at>
To: Dominique Devienne <ddevienne(at)gmail(dot)com>, pgsql-general(at)lists(dot)postgresql(dot)org
Subject: Re: Number of updated rows with LibPQ
Date: 2022-10-05 16:09:15
Message-ID: 830241525f7fefeab2000203b6fcf0a6b815ee6e.camel@cybertec.at
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On Wed, 2022-10-05 at 16:38 +0200, Dominique Devienne wrote:
> Hi,
>
> Is there a way to programmatically now how many rows an UPDATE did update?
> I've read about [PQcmdTuples][1], but surely I shouldn't have to parse
> that string, no?
> For selects, I have [PQnTuples][2], but what to do on INSERT, UPDATE, DELETE?
> Parse the result of PQcmdTuples myself??? If so, what's the 0 in the
> INSERT below?
> Is the output of PQcmdTuples "stable", i.e. "official"? Not locale dependent?
>
> Thanks, --DD
>
> [1]: https://www.postgresql.org/docs/14/libpq-exec.html#LIBPQ-PQCMDTUPLES
> [2]: https://www.postgresql.org/docs/14/libpq-exec.html#LIBPQ-PQNTUPLES
>
> postgres=# create table foo (v int);
> CREATE TABLE
> postgres=# insert into foo values (1), (2), (3);
> INSERT 0 3
> postgres=# update foo set v = 2*v where v = 2;
> UPDATE 1
> postgres=# delete from foo where v = 3;
> DELETE 1
> postgres=# select v from foo where v > 1;
> ...
> (1 row)
> postgres=# delete from foo;
> DELETE 2
> postgres=# drop table foo;
> DROP TABLE
> postgres=#

Yes, you have to use PQcmdTuples(), and you have to convert the string to an integer.

But don't worry: the result will *not* be "INSERT 0 5", it will be just "5", so
you won't have to parse anything.

Yours,
Laurenz Albe
--
Cybertec | https://www.cybertec-postgresql.com

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Kaushal Shriyan 2022-10-05 16:32:03 Replication between Master PostgreSQL database version 9.6.1 and Standby/Slave PostgreSQL database version 10.17.
Previous Message Dominique Devienne 2022-10-05 14:38:03 Number of updated rows with LibPQ