pg.ddx.io  pgsql-interfaces@postgresql.org mailing list archive  
help / color / mirror / Atom feed
Counting updates with libpq
7+ messages / 4 participants
[nested] [flat]

* Counting updates with libpq
@ 1999-03-05 16:03  Bartek Teodorczyk <barteo@it.pl>
  0 siblings, 1 reply; 7+ messages in thread

From: Bartek Teodorczyk @ 1999-03-05 16:03 UTC (permalink / raw)
  To: pgsql-interfaces

Interface: libpq

Is it possible to get number of updated records using the PQexec command
and processing the PQresult record?

BarTeo



^ permalink  raw  reply  [nested|flat] 7+ messages in thread

* Re: [INTERFACES] Counting updates with libpq
@ 1999-03-05 22:36  Tom Lane <tgl@sss.pgh.pa.us>
  parent: Bartek Teodorczyk <barteo@it.pl>
  0 siblings, 1 reply; 7+ messages in thread

From: Tom Lane @ 1999-03-05 22:36 UTC (permalink / raw)
  To: Bartek Teodorczyk <barteo@it.pl>; +Cc: pgsql-interfaces

Bartek Teodorczyk <barteo@it.pl> writes:
> Is it possible to get number of updated records using the PQexec command
> and processing the PQresult record?

See PQcmdTuples() ...

			regards, tom lane



^ permalink  raw  reply  [nested|flat] 7+ messages in thread

* Re: [INTERFACES] Counting updates with libpq
@ 1999-03-06 12:54  Michael Meskes <meskes@postgreSQL.org>
  parent: Tom Lane <tgl@sss.pgh.pa.us>
  0 siblings, 1 reply; 7+ messages in thread

From: Michael Meskes @ 1999-03-06 12:54 UTC (permalink / raw)
  To: pgsql-interfaces

On Fri, Mar 05, 1999 at 05:36:46PM -0500, Tom Lane wrote:
> Bartek Teodorczyk <barteo@it.pl> writes:
> > Is it possible to get number of updated records using the PQexec command
> > and processing the PQresult record?
> 
> See PQcmdTuples() ...

For ecpg I've beed using PQntuples(). Is it the same function? 

Michael
-- 
Michael Meskes                         | Go SF 49ers!
Th.-Heuss-Str. 61, D-41812 Erkelenz    | Go Rhein Fire!
Tel.: (+49) 2431/72651                 | Use Debian GNU/Linux!
Email: Michael.Meskes@gmx.net          | Use PostgreSQL!



^ permalink  raw  reply  [nested|flat] 7+ messages in thread

* Re: [INTERFACES] Counting updates with libpq
@ 1999-03-07 16:20  Tom Lane <tgl@sss.pgh.pa.us>
  parent: Michael Meskes <meskes@postgreSQL.org>
  0 siblings, 1 reply; 7+ messages in thread

From: Tom Lane @ 1999-03-07 16:20 UTC (permalink / raw)
  To: Michael Meskes <meskes@postgresql.org>; +Cc: pgsql-interfaces

Michael Meskes <meskes@postgresql.org> writes:
>>>> Is it possible to get number of updated records using the PQexec command
>>>> and processing the PQresult record?
>> 
>> See PQcmdTuples() ...

> For ecpg I've beed using PQntuples(). Is it the same function? 

No.  PQntuples() is the number of rows in the returned result.  It will
always be zero if the SQL command was not SELECT (or FETCH).

PQcmdTuples() is just a dumb little function that looks at the command
status string (the same thing psql prints at the end of a query) and
pulls out the number that appears in "UPDATE n" or "DELETE n" or
"INSERT oid n".  Since the questioner asked about updated records,
I assumed that was the number he wanted.

			regards, tom lane



^ permalink  raw  reply  [nested|flat] 7+ messages in thread

* Re: [INTERFACES] Counting updates with libpq
@ 1999-03-08 07:46  Michael Meskes <meskes@postgresql.org>
  parent: Tom Lane <tgl@sss.pgh.pa.us>
  0 siblings, 1 reply; 7+ messages in thread

From: Michael Meskes @ 1999-03-08 07:46 UTC (permalink / raw)
  To: pgsql-interfaces

On Sun, Mar 07, 1999 at 11:20:39AM -0500, Tom Lane wrote:
> > For ecpg I've beed using PQntuples(). Is it the same function? 
> 
> No.  PQntuples() is the number of rows in the returned result.  It will
> always be zero if the SQL command was not SELECT (or FETCH).

I'm afraid this is incorrect. I tried it on an insert (via exec sql execute
immediate) and it got the correct result.

> PQcmdTuples() is just a dumb little function that looks at the command
> status string (the same thing psql prints at the end of a query) and
> pulls out the number that appears in "UPDATE n" or "DELETE n" or
> "INSERT oid n".  Since the questioner asked about updated records,
> I assumed that was the number he wanted.

I need this number as well.

Michael
-- 
Michael Meskes                         | Go SF 49ers!
Th.-Heuss-Str. 61, D-41812 Erkelenz    | Go Rhein Fire!
Tel.: (+49) 2431/72651                 | Use Debian GNU/Linux!
Email: Michael.Meskes@gmx.net          | Use PostgreSQL!



^ permalink  raw  reply  [nested|flat] 7+ messages in thread

* Re: [INTERFACES] Counting updates with libpq
@ 1999-03-08 15:47  Tom Lane <tgl@sss.pgh.pa.us>
  parent: Michael Meskes <meskes@postgresql.org>
  0 siblings, 1 reply; 7+ messages in thread

From: Tom Lane @ 1999-03-08 15:47 UTC (permalink / raw)
  To: Michael Meskes <meskes@postgreSQL.org>; +Cc: pgsql-interfaces

Michael Meskes <meskes@postgreSQL.org> writes:
>> No.  PQntuples() is the number of rows in the returned result.  It will
>> always be zero if the SQL command was not SELECT (or FETCH).

> I'm afraid this is incorrect. I tried it on an insert (via exec sql execute
> immediate) and it got the correct result.

No, I'm afraid *you* are incorrect: a test program like

    res = PQexec(conn, "INSERT INTO test SELECT * FROM test");
    if (PQresultStatus(res) != PGRES_COMMAND_OK)
    {
        fprintf(stderr, "INSERT command failed\n");
        PQclear(res);
        exit_nicely(conn);
    }

    printf("INSERT result PQntuples = %d\n", PQntuples(res));
    printf("INSERT result PQcmdTuples = %s\n", PQcmdTuples(res));

produces the expected results (ntuples = 0, cmdTuples = number of
inserted rows) for either variant of INSERT.

It seems likely that ecpg is interposing some additional behavior
not seen in bare libpq, although I'm at a loss to guess what.
Surely ecpg wouldn't be trying to implicitly fetch the inserted rows?

			regards, tom lane



^ permalink  raw  reply  [nested|flat] 7+ messages in thread

* Re: [INTERFACES] Counting updates with libpq
@ 1999-03-08 19:34  Michael Meskes <meskes@postgreSQL.org>
  parent: Tom Lane <tgl@sss.pgh.pa.us>
  0 siblings, 0 replies; 7+ messages in thread

From: Michael Meskes @ 1999-03-08 19:34 UTC (permalink / raw)
  To: pgsql-interfaces

On Mon, Mar 08, 1999 at 10:47:38AM -0500, Tom Lane wrote:
> No, I'm afraid *you* are incorrect: a test program like
> 
>     res = PQexec(conn, "INSERT INTO test SELECT * FROM test");
>     if (PQresultStatus(res) != PGRES_COMMAND_OK)
>     {
>         fprintf(stderr, "INSERT command failed\n");
>         PQclear(res);
>         exit_nicely(conn);
>     }
> 
>     printf("INSERT result PQntuples = %d\n", PQntuples(res));
>     printf("INSERT result PQcmdTuples = %s\n", PQcmdTuples(res));
> 
> produces the expected results (ntuples = 0, cmdTuples = number of
> inserted rows) for either variant of INSERT.

Strange. Have to check my code again. ... Argh! I'm stupid. I just checked
via pattern matching and found only the position where the return value was
PGRES_TUPLES_OK. In this case I use PQntuples(). If the result was
PGRES_COMMAND_OK I do use PQcmdTuples. So I guess that explains it. :-)

Michael
-- 
Michael Meskes                         | Go SF 49ers!
Th.-Heuss-Str. 61, D-41812 Erkelenz    | Go Rhein Fire!
Tel.: (+49) 2431/72651                 | Use Debian GNU/Linux!
Email: Michael.Meskes@gmx.net          | Use PostgreSQL!



^ permalink  raw  reply  [nested|flat] 7+ messages in thread


end of thread, other threads:[~1999-03-08 19:34 UTC | newest]

Thread overview: 7+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
1999-03-05 16:03 Counting updates with libpq Bartek Teodorczyk <barteo@it.pl>
1999-03-05 22:36 ` Tom Lane <tgl@sss.pgh.pa.us>
1999-03-06 12:54   ` Michael Meskes <meskes@postgreSQL.org>
1999-03-07 16:20     ` Tom Lane <tgl@sss.pgh.pa.us>
1999-03-08 07:46       ` Michael Meskes <meskes@postgresql.org>
1999-03-08 15:47         ` Tom Lane <tgl@sss.pgh.pa.us>
1999-03-08 19:34           ` Michael Meskes <meskes@postgreSQL.org>

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