pg.ddx.io pgsql-interfaces@postgresql.org mailing list archive
help / color / mirror / Atom feedPGExec returns error without any comment
3+ messages / 2 participants
[nested] [flat]
* PGExec returns error without any comment
@ 2006-09-28 12:16 erkan kolemen <erkankolemen@yahoo.com>
0 siblings, 1 reply; 3+ messages in thread
From: erkan kolemen @ 2006-09-28 12:16 UTC (permalink / raw)
To: pgsql-interfaces
Hi,
Following code fails at PQExec. I am using PQerrorMessage() function to print error message but no output. Error code is 2. What could be the problem with select query? I tried with "drop table" it worked.
[root@hq src]# ./1
PQExec failed(2):
1.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <sys/time.h>
#include <libpq-fe.h>
int main()
{
char *conninfo;
PGconn *conn;
PGresult *res;
/* Prepare connection info */
conninfo = (char *) malloc(64);
if (conninfo == NULL) {
fprintf(stderr, "malloc error: %s\n", strerror(errno));
return -1;
}
memset(conninfo, 0, 64);
strncpy(conninfo, "dbname=llist user=yyy password=xxx", 63);
/* Make a connection to the database */
conn = PQconnectdb(conninfo);
/* Check to see that the backend connection was successfully made */
if (PQstatus(conn) != CONNECTION_OK)
{
fprintf(stderr, "Connection to database failed: %s", PQerrorMessage(conn));
return -1;
}
res = PQexec(conn, "select * from lists;");
if (PQresultStatus(res) != PGRES_COMMAND_OK)
{
fprintf(stderr, "PQExec failed(%d): %s\n", PQresultStatus(res), PQerrorMessage(conn));
PQclear(res);
return -1;
}
PQclear(res);
PQfinish(conn);
return 0;
}
---------------------------------
Do you Yahoo!?
Get on board. You're invited to try the new Yahoo! Mail.
^ permalink raw reply [nested|flat] 3+ messages in thread
* Re: PGExec returns error without any comment
@ 2006-09-28 13:37 Michael Fuhr <mike@fuhr.org>
parent: erkan kolemen <erkankolemen@yahoo.com>
0 siblings, 0 replies; 3+ messages in thread
From: Michael Fuhr @ 2006-09-28 13:37 UTC (permalink / raw)
To: erkan kolemen <erkankolemen@yahoo.com>; +Cc: pgsql-interfaces
On Thu, Sep 28, 2006 at 05:16:10AM -0700, erkan kolemen wrote:
> res = PQexec(conn, "select * from lists;");
> if (PQresultStatus(res) != PGRES_COMMAND_OK)
You're checking for the wrong result status. Here are some excerpts
from the libpq documentation:
PGRES_COMMAND_OK
Successful completion of a command returning no data.
PGRES_TUPLES_OK
Successful completion of a command returning data (such as a SELECT
or SHOW).
Note that a SELECT command that happens to retrieve zero rows still
shows PGRES_TUPLES_OK. PGRES_COMMAND_OK is for commands that can
never return rows (INSERT, UPDATE, etc.).
--
Michael Fuhr
^ permalink raw reply [nested|flat] 3+ messages in thread
* Re: PGExec returns error without any comment
@ 2006-09-28 18:33 erkan kolemen <erkankolemen@yahoo.com>
0 siblings, 0 replies; 3+ messages in thread
From: erkan kolemen @ 2006-09-28 18:33 UTC (permalink / raw)
To: pgsql-interfaces
Thanks,
That was the problem!
Volkan YAZICI <yazicivo@ttnet.net.tr> wrote: On Sep 28 05:16, erkan kolemen wrote:
> char *conninfo;
> PGconn *conn;
> PGresult *res;
>
> /* Prepare connection info */
> conninfo = (char *) malloc(64);
> if (conninfo == NULL) {
> fprintf(stderr, "malloc error: %s\n", strerror(errno));
> return -1;
> }
>
> memset(conninfo, 0, 64);
> strncpy(conninfo, "dbname=llist user=yyy password=xxx", 63);
I'd prefer a
char conninfo[] = "dbname=llist user=yyy password=xxx";
instead of above mess.
> /* Make a connection to the database */
> conn = PQconnectdb(conninfo);
>
> /* Check to see that the backend connection was successfully made */
> if (PQstatus(conn) != CONNECTION_OK)
> {
> fprintf(stderr, "Connection to database failed: %s", PQerrorMessage(conn));
> return -1;
> }
>
> res = PQexec(conn, "select * from lists;");
> if (PQresultStatus(res) != PGRES_COMMAND_OK)
You should be checking for PGRES_TUPLES_OK, not COMMAND_OK.
AFAIU from your name, you're from Turkey. Have you ever heard about
"PostgreSQL ile Programlama" book? You can get it freely from here:
http://www.students.itu.edu.tr/~yazicivo/doc/postgresql-ile-programlama.html
Regards.
---------------------------------
All-new Yahoo! Mail - Fire up a more powerful email and get things done faster.
^ permalink raw reply [nested|flat] 3+ messages in thread
end of thread, other threads:[~2006-09-28 18:33 UTC | newest]
Thread overview: 3+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2006-09-28 12:16 PGExec returns error without any comment erkan kolemen <erkankolemen@yahoo.com>
2006-09-28 13:37 ` Michael Fuhr <mike@fuhr.org>
2006-09-28 18:33 Re: PGExec returns error without any comment erkan kolemen <erkankolemen@yahoo.com>
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox