Обсуждение: C code problem
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? + ++++++++++++++++++++++++++++++++++
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@backpack.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!" -----------------------------------------------------------------------------
Thanks for the code. It works great. It looks like there is a definite difference between the way that PQsetdb and PQconnectdb "open" the database connection. Until now I've been using PQconnectdb with no problems. "D. Duccini" wrote: > 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@backpack.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!" > ----------------------------------------------------------------------------- -- 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? + ++++++++++++++++++++++++++++++++++