pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Joe Conway <mail@joeconway.com>
To: Tony Reina <gar8@pitt.edu>
Cc: pgsql-sql@postgresql.org
Subject: Re: How can I pass an array from PostgreSQL to C (libpq)?
Date: Wed, 26 Mar 2003 11:07:22 -0800
Message-ID: <3E81FA6A.8090201@joeconway.com> (raw)
In-Reply-To: <a1688e61.0303261024.38b1b31e@posting.google.com>
References: <a1688e61.0303261024.38b1b31e@posting.google.com>

Tony Reina wrote:
> I'm using the C library (lipq) to grab data from a field that contains
> a float array (float4[]). I'd like to fill an array in my C program
> with these values. Unfortunately, Postgres returns the array as a
> large string instead of individual values.
> 
> Now I could perform "select array_var[1], array_var[2], array_var[3],
> ..., array_var[4]" and have each array value come back as a different
> field, but this seems fairly messy. I suppose I could also go through
> and parse the characters in the string returned.
> 
> Has anyone else used any other strategies to fill their C array with
> an array returned from the libpq? Perhaps there are some SQL functions
> for arrays that might exist?
> 

PQgetvalue is always going to give you the string representation of the 
field value *unless* you specify a binary cursor. In that case you'll 
get all requested fields in binary form, and it's your responsibility to 
deal with making sense of what you get. There are some backend macros 
and functions that can help you get at array values. Take a look at 
src/utils/adt/arrayfuncs.c and src/include/utils/array.h. You might want 
to read through contrib/array and/or contrib/intarray for examples.

You could also consider writing your array manipulation code in a user 
defined C function and just return the end result to the libpq program. 
It's hard to know if this is an option without more detail on what 
you're trying to do.

Joe




view thread (4+ messages)  latest in thread

Message-ID: <3E81FA6A.8090201@joeconway.com>
Permalink:  ../3E81FA6A.8090201@joeconway.com/
Also on:    postgresql.org/message-id/3E81FA6A.8090201@joeconway.com

 · 

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-sql@postgresql.org
  Cc: mail@joeconway.com, gar8@pitt.edu
  Subject: Re: How can I pass an array from PostgreSQL to C (libpq)?
  In-Reply-To: <3E81FA6A.8090201@joeconway.com>

* 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