pg.ddx.io  pgsql-interfaces@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Michael Fuhr <mike@fuhr.org>
To: Oliver Elphick <olly@lfix.co.uk>
Cc: pgsql-interfaces@postgresql.org
Subject: Re: PL/Python: How do I use result methods?
Date: Sat, 16 Oct 2004 10:56:00 -0600
Message-ID: <20041016165600.GA71873@winnie.fuhr.org> (raw)
In-Reply-To: <1097937269.12716.28.camel@linda>
References: <1097937269.12716.28.camel@linda>

On Sat, Oct 16, 2004 at 03:34:29PM +0100, Oliver Elphick wrote:
> 
> According to the docs (8.0):
> 
>         Calling plpy.execute with a query string and an optional limit
>         argument causes that query to be run and the result to be
>         returned in a result object. The result object emulates a list
>         or dictionary object. The result object can be accessed by row
>         number and column name. It has these additional methods: nrows
>         which returns the number of rows returned by the query, and
>         status which is the SPI_execute() return value.
> 
> What exactly is the syntax for using the nrows and status methods?
> 
> I am trying to use it like this:
> 
> ...
> arec = plpy.execute(plan, [ addrno ], 1)
> plpy.info("Executed plan")
> n = plpy.nrows(arec)

The documentation describes nrows as being a method of the result
object, so I'd guess it should be used thusly:

  n = arec.nrows()

I tried that with the following function:

  CREATE OR REPLACE FUNCTION test() RETURNS INTEGER AS $$
  rv = plpy.execute('SELECT usename FROM pg_user')
  n = rv.nrows()
  return 1
  $$ LANGUAGE plpythonu;

The function failed:

  test=> SELECT test();
  ERROR:  plpython: function "test" failed
  DETAIL:  exceptions.SystemError: error return without exception set

Digging through plpython.c I discovered the following:

  #ifdef NOT_USED
  /* Appear to be unused */
  static PyMethodDef PLy_result_methods[] = {
          {"fetch", (PyCFunction) PLy_result_fetch, METH_VARARGS, NULL,},
          {"nrows", (PyCFunction) PLy_result_nrows, METH_VARARGS, NULL},
          {"status", (PyCFunction) PLy_result_status, METH_VARARGS, NULL},
          {NULL, NULL, 0, NULL}
  };
  #endif

It looks like nrows(), status(), and fetch() aren't available as
result object methods.  I don't know what would happen if you
un-#ifdef-ed that block and the two others that are related;
perhaps one of the developers can tell us what's going on.

If the methods aren't available then they probably shouldn't be
mentioned in the doc.

-- 
Michael Fuhr
http://www.fuhr.org/~mfuhr/



view thread (6+ messages)  latest in thread

Message-ID: <20041016165600.GA71873@winnie.fuhr.org>
Permalink:  ../20041016165600.GA71873@winnie.fuhr.org/
Also on:    postgresql.org/message-id/20041016165600.GA71873@winnie.fuhr.org

 · 

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 using the --to and --cc options:
  reply via email

  To: pgsql-interfaces@postgresql.org
  Cc: mike@fuhr.org, olly@lfix.co.uk
  Subject: Re: PL/Python: How do I use result methods?
  In-Reply-To: <20041016165600.GA71873@winnie.fuhr.org>

* 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