Re: C code problem

From: "D(dot) Duccini" <duccini(at)backpack(dot)com>
To: John Burski <John(dot)Burski(at)911ep(dot)com>
Cc: pgsql-novice(at)postgresql(dot)org
Subject: Re: C code problem
Date: 2001-04-17 14:07:38
Message-ID: Pine.GSO.4.03.10104170905470.28457-100000@ra.bpsi.net
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-novice


here's some code i wrote for generically dealing with postgres in C

--> modify the 'connect' as you see fit -- mine was based on SUID user
processing for some daemons we were using

void SQLShutdown(PGconn *conn)
{
PQfinish(conn);
}

int SQLString(char *is, char *os)
{
char *ch=is,
*ch2=os;

while (*ch) {
if (*ch == '\'')
*ch2++ = '\'';
*ch2++ = *ch++;
}
*ch2 = 0;
return(0);
}

PGconn *SQLConnect(void)
{
PGconn *conn;
char *dbName = "",
*pghost = NULL, /* host name of the backend server */
*pgport = NULL, /* port of the backend server */
*pgoptions = NULL, /* special options to start up the backend
server
*/
*pgtty = NULL; /* debugging tty for the backend server */

Debugf("SQLConnect()\n");

conn = PQsetdb(pghost, pgport, pgoptions, pgtty, dbName);
if (PQstatus(conn) == CONNECTION_BAD) {
printf("Connection to database '%s' failed.\n", dbName);
printf("%s",PQerrorMessage(conn));
return(NULL);
}
return(conn);
}

int SQLDisconnect(PGconn *conn)
{
PQfinish(conn);
}

PGresult *SQLExecute(PGconn *conn, char *xBuf, int resultOK)
{
PGresult *res;

printf("%s\n", xBuf);

res = PQexec(conn, xBuf);
if (PQresultStatus(res) != resultOK) {
printf("PQresultStatus(res) : %d\n", PQresultStatus(res));
printf("! Command failed\n");
PQclear(res);
return(NULL);
}
else
return(res);
}

PGresult *SQLExecuteQuietly(PGconn *conn, char *xBuf, int resultOK)
{
PGresult *res;

res = PQexec(conn, xBuf);
if (PQresultStatus(res) != resultOK) {
printf("PQresultStatus(res) : %d\n", PQresultStatus(res));
printf("! Command failed\n");
PQclear(res);
return(NULL);
}
else
return(res);
}

-duck

On Tue, 17 Apr 2001, John Burski wrote:

> I'm running PostgreSQL 7.0.3 on a Red Hat 6.1 box and am having a bit of
> trouble with some C code.
>
> I'm able to connect to the database OK (host = localhost, hostaddr =
> 127.0.0.1, dbname = template1, user = postgres (the superuser), password
> = whatever), but a PGRES_NONFATAL_ERROR status results from the
> following query:
>
> result = PQexec ( dbc, "SELECT * FROM pg_user" );
>
> If I run the query from an interactive session it works fine. I've
> other programs that work fine, but none of them connect to "template1"
> or attempt to access the system tables. Is there some sort of access
> restriction related to the system tables?
>
> Thanks.
>
> --
> John Burski
> I.T. Manager
> 911 Emergency Products
> 25 Sixth Avenue North
> St. Cloud, MN 56303
> (320) 656 0076 www.911ep.com
>
> ++++++++++++++++++++++++++++++++++
> + How's your cheese holding out? +
> ++++++++++++++++++++++++++++++++++
>
>
>
>
> ---------------------------(end of broadcast)---------------------------
> TIP 4: Don't 'kill -9' the postmaster
>

-----------------------------------------------------------------------------
david(at)backpack(dot)com BackPack Software, Inc. www.backpack.com
+1 651.645.7550 voice "Life is an Adventure.
+1 651.645.9798 fax Don't forget your BackPack!"
-----------------------------------------------------------------------------

In response to

Responses

Browse pgsql-novice by date

  From Date Subject
Next Message John Burski 2001-04-17 15:45:32 Re: C code problem
Previous Message John Burski 2001-04-17 13:42:43 C code problem