pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Laurenz Albe <laurenz.albe@cybertec.at>
To: Dominique Devienne <ddevienne@gmail.com>
To: pgsql-general@lists.postgresql.org
Subject: Re: Number of updated rows with LibPQ
Date: Wed, 05 Oct 2022 18:09:15 +0200
Message-ID: <830241525f7fefeab2000203b6fcf0a6b815ee6e.camel@cybertec.at> (raw)
In-Reply-To: <CAFCRh-_Z+ox_W4+yZTOC31d39b7w23jWStvK0SXV1qfht9tRLQ@mail.gmail.com>
References: <CAFCRh-_Z+ox_W4+yZTOC31d39b7w23jWStvK0SXV1qfht9tRLQ@mail.gmail.com>

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





view thread (8+ messages)  latest in thread

Message-ID: <830241525f7fefeab2000203b6fcf0a6b815ee6e.camel@cybertec.at>
Permalink:  ../830241525f7fefeab2000203b6fcf0a6b815ee6e.camel@cybertec.at/
Also on:    postgresql.org/message-id/830241525f7fefeab2000203b6fcf0a6b815ee6e.camel@cybertec.at

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-general@postgresql.org
  Cc: laurenz.albe@cybertec.at, ddevienne@gmail.com
  Subject: Re: Number of updated rows with LibPQ
  In-Reply-To: <830241525f7fefeab2000203b6fcf0a6b815ee6e.camel@cybertec.at>
  References: <CAFCRh-_Z+ox_W4+yZTOC31d39b7w23jWStvK0SXV1qfht9tRLQ@mail.gmail.com> <830241525f7fefeab2000203b6fcf0a6b815ee6e.camel@cybertec.at>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox