X-Original-To: pgsql-sql@postgresql.org Received: from spampd.localdomain (postgresql.org [64.49.215.8]) by postgresql.org (Postfix) with ESMTP id 398BF476332 for ; Wed, 26 Mar 2003 14:10:30 -0500 (EST) Received: from joeconway.com (66-146-172-86.skyriver.net [66.146.172.86]) by postgresql.org (Postfix) with ESMTP id 9117F47633C for ; Wed, 26 Mar 2003 14:09:57 -0500 (EST) Received: from [206.19.64.3] (account jconway HELO joeconway.com) by joeconway.com (CommuniGate Pro SMTP 4.0.4) with ESMTP-TLS id 1676307; Wed, 26 Mar 2003 11:46:18 -0800 Message-ID: <3E81FA6A.8090201@joeconway.com> Date: Wed, 26 Mar 2003 11:07:22 -0800 From: Joe Conway User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.0; en-US; rv:1.2.1) Gecko/20021130 X-Accept-Language: en-us, en MIME-Version: 1.0 To: Tony Reina Cc: pgsql-sql@postgresql.org Subject: Re: How can I pass an array from PostgreSQL to C (libpq)? References: In-Reply-To: Content-Type: text/plain; charset=us-ascii; format=flowed Content-Transfer-Encoding: 7bit X-Spam-Status: No, hits=-38.8 required=5.0 tests=BAYES_01,EMAIL_ATTRIBUTION,IN_REP_TO,QUOTED_EMAIL_TEXT, REFERENCES,REPLY_WITH_QUOTES,USER_AGENT_MOZILLA_UA autolearn=ham version=2.50 X-Spam-Level: X-Spam-Checker-Version: SpamAssassin 2.50 (1.173-2003-02-20-exp) X-Archive-Number: 200303/590 X-Sequence-Number: 12628 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