Re: C-Functions using SPI - Missing Magic Block

Поиск
Список
Период
Сортировка
От Sam Mason
Тема Re: C-Functions using SPI - Missing Magic Block
Дата
Msg-id 20100705104953.GM7584@samason.me.uk
обсуждение исходный текст
Ответ на Re: C-Functions using SPI - Missing Magic Block  (Saitenheini@web.de)
Список pgsql-general
On Mon, Jul 05, 2010 at 10:20:30AM +0200, Saitenheini@web.de wrote:
> Datum count_person(PG_FUNCTION_ARGS) {
> SPI_connect();
> int ret = SPI_exec("SELECT count(*) FROM person", 0);
> SPI_finish();
> PG_RETURN_INT32(ret);
> }
>
> But I guess I still did something wrong, because no matter how many
> rows exist in my table "person" the result in always 5.

5 is actually the value you want to be getting back!

The problem is that you're treating the status value of SPI_exec as the
result of the query.  You need to check the result to see if there was
an error and only if it's OK can you call something like SPI_getvalue to
actually get the count out.  You could probably steal some code from:

  http://developer.postgresql.org/pgdocs/postgres/spi-examples.html

--
  Sam  http://samason.me.uk/

В списке pgsql-general по дате отправления: