Return-Path: owner-postman
Received: from localhost.Berkeley.EDU (localhost.Berkeley.EDU [127.0.0.1]) by nobozo.CS.Berkeley.EDU (8.6.9/8.6.3) with SMTP id MAA01282 for postgres-redist; Mon, 13 Feb 1995 12:19:31 -0800
Resent-From: POSTGRES mailing list <postman@postgres.Berkeley.EDU>
Resent-Message-Id: <199502132019.MAA01282@nobozo.CS.Berkeley.EDU>
X-Authentication-Warning: nobozo.CS.Berkeley.EDU: Host localhost.Berkeley.EDU didn't use HELO protocol
Sender: owner-postman@postgres.Berkeley.EDU
X-Return-Path: owner-postman
Received: from RASAM.NDIM.EDRC.CMU.EDU (RASAM.NDIM.EDRC.CMU.EDU [128.2.214.234]) by nobozo.CS.Berkeley.EDU (8.6.9/8.6.3) with SMTP id MAA01271 for <postgres@postgres.berkeley.edu>; Mon, 13 Feb 1995 12:18:47 -0800
Received: from Messages.8.5.N.CUILIB.3.45.SNAP.NOT.LINKED.rasam.ndim.edrc.cmu.edu.pmax.ul4
          via MS.5.6.rasam.ndim.edrc.cmu.edu.pmax_ul4;
          Mon, 13 Feb 1995 15:18:17 -0500 (EST)
Message-ID: <wjDvu9e00hPeBErV1s@cs.cmu.edu>
Date: Mon, 13 Feb 1995 15:18:17 -0500 (EST)
From: Robert.Patrick@cs.cmu.edu
To: postgres@postgres.Berkeley.EDU, Pconrad@eecis.udel.EDU
Subject: Re: Utility for tab_separated_text
CC: pconrad@eecis.udel.EDU
In-Reply-To: <9502120556.aa12270@stimpy.eecis.udel.edu>
References: <9502120556.aa12270@stimpy.eecis.udel.edu>
X-Sender: rp2y@RASAM.NDIM.EDRC.CMU.EDU
Resent-To: postgres-redist@postgres.Berkeley.EDU
Resent-Date: Mon, 13 Feb 95 12:19:10 -0800
Resent-XMts: smtp

Excerpts from internet.postgres: 12-Feb-95 Utility for
tab_separated_text Pconrad@eecis.udel.edu (1682)

>    I am working on a project where I will have a need for a utility
> that will take an arbitrary Postquel query and return the result as
> tab separated text, suitable for importing into (for example) a
> spreadsheet. 

Here's a somewhat crude example (please note that I am not being very
rigorous about error checking).

#include <tmp/libpq.h>
#include <stdio.h>
#include <string.h>

int main(int argc, char *argv[])
{
    int i, j, ncols, nrows;
    char *dbname, query[256]; 
    char *retval, *pstring;
    PortalBuffer *pbuf;

    dbname = argv[0];
    sprintf(query, "retrieve (EMP.all) where EMP.name = \"bob\");

    PQsetdb(dbname);
    pstring= PQexec(query);
    pbuf = PQparray(&(pstring[1]));
    ncols = PQnfieldsGroup(pbuf, 0);
    nrows = PQntuplesGroup(pbuf, 0);

    /* Put the code for opening the file here */

    for (i = 0; i < nrows; i++) {
        char tmpbuf[4096];
        int idx;

        idx = 0;
        for (j = 0; j < ncols; j++) {
            retval = PQgetvalue(pbuf, i, j);
	    sprintf (&(tmpbuf[idx]), "%s\t", retval);
             idx += strlen(retval) + 1;
        }
        tmpbuf[idx-1] = '\n';   /* overwriting final tab */

/* Put code for writing tmpbuf to the file */  
    }
/* Close the file */
    PQfinish();
    return(0);
}

I may have overlooked something but you should get the general idea of
how to do it.

Robert

==============================================================================
   To add/remove yourself to/from the POSTGRES mailing list: send mail with 
   the subject line ADD or DEL to "postgres-request@postgres.Berkeley.EDU".
   If this fails, send mail to "post_questions@postgres.Berkeley.EDU" and
   a human will deal with it.  DO NOT post to the "postgres" mailing list.
==============================================================================
              URL: http://s2k-ftp.CS.Berkeley.EDU:8000/postgres/
