On Thu, 10 Sep 1998, Chris Johnson wrote:
> I usually use pg_result... here is a snipped of some C I have used:
>
> <<<>>>
> for (i = 0; i < PQntuples(res); i++)
> {
> dbName = PQgetvalue(res, i, 0);
> <<<>>>
>
> It works perfectly on the Linux, IRIX and Digital Unix (OSF/1)... Assuming
> all the regression tests worked when you installed, and that you are
> linking your program with the proper libpq, I guess the next step is to
> see more of the code and to know exactly how you compiled it.
I'm running Linux (RH4.2, 2.0.34) , latest version of pg (6.3.2?) and
linking AFAIK with
the correct libpq.a - I only have the one copy on my system so it should
be correct. The code is below. Everything else works but for the
getvalue()
dbresult is just a list-type structure with char* and dbresult* members.
dbconn is pre-initialised in another function.
Note: code has been stripped of non-essential stuff.
/* This function returns a _single_ column value from a table
* The return value is a list to allow multiple results
* When you've finished with the returned list, call db_freevalue(list)
*/
dbresult * db_getvalue(char * table, char * column, char * where_clause)
{
dbresult *values, *v;
char query[1024]; /* max is 8192 */
PGresult *result;
ExecStatusType status;
int tuples;
int count = 0;
if (!table || !column) return NULL;
sprintf(query, "SELECT %s FROM %s", column, table);
if (where_clause) sprintf(query, "%s %s", query, where_clause);
result = PQexec(dbconn, query);
if ((status = PQresultStatus(result)) != PGRES_TUPLES_OK) {
char temp[1024];
sprintf(temp, "SELECT returned %s\n Query was: %s",
PGerror[status], query);
/* logerr("DB", temp); */
return NULL;
}
tuples = PQntuples(result);
values = malloc(sizeof(dbresult));
v = values;
count = 0;
PQprintTuples(result, stderr, 1, 1, 0);
while (count <= tuples) {
/*
v->data = malloc(PQgetlength(result, count, 1));
*/
v->data = malloc(256);
v->next = NULL;
strcpy(v->data, PQgetvalue(result, count, 1));
if (--count > 0) {
v->next = malloc(sizeof(dbresult));
v = v->next;
}
}
PQclear(result);
return values;
}
Simon.
>
> Chris
>
> --
> A language that doesn't affect the way you think about programming is
> not worth knowing.
>
> On Thu, 10 Sep 1998, Simon Drabble wrote:
>
> >
> >
> > On Thu, 10 Sep 1998, Chris Johnson wrote:
> >
> > > Postgres numbers the rows and fields starting at 0 - your call to get the
> > > first row and column would be PQgetvalue(result, 0, 0)
> > >
> > > Chris
> >
> > Thanks for your reply, but..
> >
> > If I have a zero for the field number (last parameter) I get a seg fault
> > immediately - gdb shows it to be in PQgetvalue().
> >
> > If I have a row number of zero (second param) I get the same error as
> > below (if the column number is >0 )
> >
> >
> > Simon.
> >
> >
> >
> >
> > >
> > > On Thu, 10 Sep 1998, Simon Drabble wrote:
> > >
> > > >
> > > > I'm executing a (simple) query using libpq, and this is the error I get
> > > > from PQgetvalue(result, 1, 1):
> > > >
> > > > PQgetvalue: There is no field 1 in the query results. The highest
> > > > numbered field is 0.
> > > >
> > > > I get a similar error if I call getlength().
> > > >
> > > > There _is_ data there - PQprintTuples() prints everything correctly.
> > > >
> > > >
> > > > Can anyone shed any light? PQtrace() doesn't reveal anything useful..
> > > >
> > > >
> > > > Simon.
> > > >
> > > >
> > > > --
> > > > ...oooOOOooo...
> > > > Simon Drabble mailto:madlather@syspac.com
> > > > http://fiddletrudge.dyn.ml.org/
> > > > KreatAPayj -- ~simon/KreatAPayj/login.html
> > > > LizardCam -- LizardCam/LizardCam.html
> > > > Linux Knowledge Base -- ~simon/LinuxKB/index.html
> > > >
> > > >
> > > >
> > >
> >
> > --
> > ...oooOOOooo...
> > Simon Drabble mailto:madlather@syspac.com
> > http://fiddletrudge.dyn.ml.org/
> > KreatAPayj -- ~simon/KreatAPayj/login.html
> > LizardCam -- LizardCam/LizardCam.html
> > Linux Knowledge Base -- ~simon/LinuxKB/index.html
> >
> >
>
--
...oooOOOooo...
Simon Drabble mailto:madlather@syspac.com
http://fiddletrudge.dyn.ml.org/
KreatAPayj -- ~simon/KreatAPayj/login.html
LizardCam -- LizardCam/LizardCam.html
Linux Knowledge Base -- ~simon/LinuxKB/index.html