Обсуждение: PGExec returns error without any comment
Hi, <br /><br />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.<br /><br
/>[root@hqsrc]# ./1<br />PQExec failed(2):<br /><br />1.c<br />#include <stdio.h><br />#include
<stdlib.h><br/>#include <string.h><br />#include <errno.h><br />#include <sys/time.h><br
/>#include<libpq-fe.h><br /><br />int main()<br />{<br /> char *conninfo;<br /> PGconn
*conn;<br/> PGresult *res;<br /><br /> /* Prepare connection info */<br /> conninfo = (char *)
malloc(64);<br/> if (conninfo == NULL) {<br /> fprintf(stderr, "malloc error: %s\n",
strerror(errno));<br/> return -1;<br /> }<br /><br /> memset(conninfo, 0, 64);<br
/> strncpy(conninfo, "dbname=llist user=yyy password=xxx", 63);<br /><br /> /* Make a connection to the
database*/<br /> conn = PQconnectdb(conninfo);<br /><br /> /* Check to see that the backend connection
wassuccessfully made */<br /> if (PQstatus(conn) != CONNECTION_OK)<br /> {<br />
fprintf(stderr,"Connection to database failed: %s", PQerrorMessage(conn));<br /> return -1;<br />
}<br/><br /> res = PQexec(conn, "select * from lists;");<br /> if (PQresultStatus(res) !=
PGRES_COMMAND_OK)<br/> {<br /> fprintf(stderr, "PQExec failed(%d): %s\n", PQresultStatus(res),
PQerrorMessage(conn));<br/> PQclear(res);<br /> return -1;<br /> }<br /><br
/> PQclear(res);<br /><br /> PQfinish(conn);<br /><br /> return 0;<br />}<br /><br /><p><hr
size="1"/>Do you Yahoo!?<br /> Get on board. <a
href="http://us.rd.yahoo.com/evt=40791/*http://advision.webevents.yahoo.com/mailbeta">You'reinvited</a> to try the new
Yahoo!Mail.
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 commandsthat can never return rows (INSERT, UPDATE, etc.). -- Michael Fuhr
Thanks,<br /><br />That was the problem!<br /><br /><b><i>Volkan YAZICI <yazicivo@ttnet.net.tr></i></b>
wrote:<blockquoteclass="replbq" style="border-left: 2px solid rgb(16, 16, 255); margin-left: 5px; padding-left: 5px;">
OnSep 28 05:16, erkan kolemen wrote:<br />> char *conninfo;<br />> PGconn *conn;<br />> PGresult *res;<br
/>><br />> /* Prepare connection info */<br />> conninfo = (char *) malloc(64);<br />> if (conninfo ==
NULL){<br />> fprintf(stderr, "malloc error: %s\n", strerror(errno));<br />> return -1;<br />> }<br />> <br
/>>memset(conninfo, 0, 64);<br />> strncpy(conninfo, "dbname=llist user=yyy password=xxx", 63);<br /><br />I'd
prefera<br /><br /> char conninfo[] = "dbname=llist user=yyy password=xxx";<br /><br />instead of above mess.<br /><br
/>>/* Make a connection to the database */<br />> conn = PQconnectdb(conninfo);<br />> <br />> /* Check to
seethat the backend connection was successfully made */<br />> if (PQstatus(conn) != CONNECTION_OK)<br />> {<br
/>>fprintf(stderr, "Connection to database failed: %s", PQerrorMessage(conn));<br />> return -1;<br />> }<br
/>><br />> res = PQexec(conn, "select * from lists;");<br />> if (PQresultStatus(res) != PGRES_COMMAND_OK)<br
/><br/>You should be checking for PGRES_TUPLES_OK, not COMMAND_OK.<br /><br />AFAIU from your name, you're from Turkey.
Haveyou ever heard about<br />"PostgreSQL ile Programlama" book? You can get it freely from here:<br
/>http://www.students.itu.edu.tr/~yazicivo/doc/postgresql-ile-programlama.html<br/><br /><br />Regards.<br
/></blockquote><br/><p><hr size="1" /><a
href="http://us.rd.yahoo.com/evt=43256/*http://advision.webevents.yahoo.com/mailbeta">All-new Yahoo! Mail </a>- Fire up
amore powerful email and get things done faster.