C++, libpq y postgresql

From: <ing_enriquebarrios(at)argentina(dot)com>
To: espartano(dot)mail(at)gmail(dot)com
Cc: anjarami(at)gmail(dot)com, pgsql-es-ayuda(at)postgresql(dot)org
Subject: C++, libpq y postgresql
Date: 2006-10-18 11:51:38
Message-ID: 20061018115443.64AB964464A@ns-2.net
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-es-ayuda

<html>

<P>Acá te mando el código con el que podes conectarte a postgresql desde c en linux, y mostrar los datos.</P>
<P>espero que te sirva.</P>
<P>saludos</P>
<P>kicke</P>
<P>para compilarlo yo hago esto:</P>
<P>gcc -I /usr/local/pgsql/include -L /usr/local/pgsql/lib -o nombre_salida -lpq nombre_archivo.c</P>
<P>---------------------------------------------------<BR>
#include &lt;stdio.h&gt;<BR>
#include &lt;libpq-fe.h&gt;<BR>
</P>
<P>static void<BR>
salir(PGconn *psql)<BR>
{<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; PQfinish(psql);<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; exit(1);<BR>
}</P>
<P>int<BR>
main()<BR>
{</P>
<P>PGconn *psql;<BR>
PGresult *result;</P>
<P>int&nbsp; nFields;<BR>
int&nbsp; i,j;</P>
<P><BR>
psql = PQconnectdb("hostaddr = '10.23.184.113' port = '5432' dbname = 'nombrebase' user = 'postgres' password = 'manager' connect_timeout = '10'");<BR>
</P>
<P>if (PQstatus(psql) != CONNECTION_OK)<BR>
{</P>
<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; fprintf(stderr, "libpq error: PQstatus(psql) != CONNECTION_OK\n\n");<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; salir(psql);<BR>
}</P>
<P>result = PQexec(psql,"select * from empresa;");</P>
<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; nFields = PQnfields(result);</P>
<P><BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; for (i = 0; i &lt; nFields; i++)<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; printf("%-15s", PQfname(result, i));<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; printf("\n\n");</P>
<P><BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; for (i = 0; i &lt; PQntuples(result); i++)<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; for (j = 0; j &lt; nFields; j++)<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; printf("%-15s", PQgetvalue(result, i, j));<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; printf("\n");<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</P>
<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; PQclear(result);</P>
<P><BR>
salir(psql);<BR>
system(". sshe.sh");</P>
<P>}<BR>
</P>
</html><BR><br><addr>------------------------------------------------------------------------<br>
<b><a href=http://free.internet.argentina.com/>Conectate a Internet Gratis de Argentina.Com</a></b><br>
</addr>
<br>
<center>
<a href=http://srvads.argentina.com/bs/adclick.php?n=a84973ad target=_blank>
<img src=http://srvads.argentina.com/bs/adview.php?what=zone:8&n=a84973ad border=0 alt=""></a>
</center>

Attachment Content-Type Size
unknown_filename text/html 2.7 KB

Browse pgsql-es-ayuda by date

  From Date Subject
Next Message Alvaro Herrera 2006-10-18 12:47:32 Re: error al crear FK
Previous Message Jaime Casanova 2006-10-18 04:11:25 Re: error al crear FK