Subject: PQexec and PQparray questions

From: [email-masked] (Alfredo Sanchez)
Date: Thu, 9 Jan 92 15:54:04 -0800

i'm having trouble with a supposedly very
simple use of PQexec. Following the examples
in the manual and in .../sample, i tried this
code:
----------------------------------
#include <stdio.h>
#include "libpq.h"

void store(obj_type,obj,size,name,uid)
/*stores an object and returns its unique id */
   char *obj_type;
   char *obj;
   unsigned long size;
   char *name;
   unsigned long *uid;

   {

   char postquery[512];
   PortalBuffer *portalbuff;

   char *res;
   char result[512];

   PQsetdb("hb1");
   sprintf (postquery,"append OBJECTS (name=\"%s\",size=%ld, \
                       obj_type=\"%s\")",name, size, obj_type);
   PQexec("begin");
   PQexec(postquery);
   sprintf(postquery,"retrieve portal p1 unique (e.oid)      \
                      from e in OBJECTS where e.name=\"%s\"  \
                      and e.size=%ld and e.obj_type=\"%s\"", 
                      name, size, obj_type);
   res = PQexec(postquery);

   portalbuff = PQparray(++res);

   *uid = atoi(PQgetvalue(portalbuff,1,1));

   PQexec("end");
   PQfinish();
   }

main ()
{
   char uid;
   unsigned long rc;
 
   store("Exec","dummy",151,"myname",uid);

   printf("Just created object with uid %ld \n",uid);

}
------------------------

when compiling it, i was warned about the type of PQexec
(which i expected to be pointer to char):

%> make store
cc -g  -I/home2/postgres/src/lib/H   -I/home2/postgres/src/lib/H/tmp -o store store.c -lpq
"store.c", line 29: warning: illegal combination of pointer and integer, op =
%> 

 so i changed the definition of the variable "res" to char, and
added a pointer to it to get the portal using PQparray. it was
compiled, but PQparray doesn't seem to be working, and i'm
getting a segmentation fault error when executing my program.

any clues?

thanks galore,

J Alfredo Sanchez H
-------------------
Hypertext Research Lab
Department of Computer Science
Texas A&M University

---

From: postarch (Postgres Mailing Archive)
Date: Thu, 09 Jan 92 19:04:10 PST

> getting a segmentation fault error when executing my program.

There are a few things wrong with the code fragment above.  First, when you
retrieve into a portal you don't get the portal name back.  The return string
(if the query succeeds) is "CRETRIEVE".  You must first fetch the tuples
in the portal:

	res = PQexec("fetch all in p1");

Now the return string should be "Pp1" and you can proceed to get the values.

Second, because the tuple groups and tuples are indexed starting from 0 not 1,
PQgetvalue() will return NULL and you will die in atoi().

	*uid = atoi(PQgetvalue(portalbuff,0,0));

As a general rule you want to make sure that what you get back from 
PQgetvalue is non-null before you pass it to conversion routines.  It's a
good idea to use PQntuples() and PQngroups(). You should also check
the first returned character whenever you do a PQexec to make sure that your
query has executed without problems.

Jeff Meredith
[email-masked]