Ejercicios vistos en la platica de libpq

Juan Pablo Rosas Durán lux@mail.internet.com.mx
Wed, 12 Aug 1998 09:09:41 -0500


This is a multi-part message in MIME format.
--------------485152242EC8BBA5C2A4B3C0
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit

He aqui los programas con los que Bruno Unna nos ilustro el usp de las
libpq, si hace falta algo les ruego me lo indiquen. 

--
Saludos
Juan Pablo Rosas Duran
--------------485152242EC8BBA5C2A4B3C0
Content-Type: text/plain; charset=iso-8859-1; name="insertMiembro.c"
Content-Transfer-Encoding: 8bit
Content-Disposition: inline; filename="insertMiembro.c"

#include <math.h>
#include <stdio.h>
#include <time.h>
#include <libpq-fe.h>

PGconn *conn;
PGresult *res;

void exit_nicely(int status) {
	PQfinish(conn);
	exit(status);
}

void query(char *qStr) {
	printf("Query: %s\n", qStr);
	res = PQexec(conn, qStr);
	if(res==NULL) {
		fprintf(stderr, "Error: res==NULL\n");
		fprintf(stderr, "Query: %s\n\n", qStr);
		PQclear(res);
		exit_nicely(1);
	}
	if(PQresultStatus(res)!=PGRES_COMMAND_OK && PQresultStatus(res)!=PGRES_TUPLES_OK) {
		fprintf(stderr, "Error: %d\n", PQresultStatus(res));
		fprintf(stderr, "Query: %s\n\n", qStr);
		PQclear(res);
		exit_nicely(1);
	}
}

main(int argc, char *argv[]) {
	char *pghost=NULL, *pgport=NULL, *pgoptions=NULL, *pgtty=NULL;
	char q[256];

	if(argc!=5) {
		fprintf(stderr, "Uso: %s <nombres> <apellidos> <fecha nacimiento> <empresa id>\n", argv[0]);
		return -1;
	}

	/* abrir la conexión */
	conn = PQsetdb(pghost, pgport, pgoptions, pgtty, "grupopg");
	if (PQstatus(conn) == CONNECTION_BAD) {
		fprintf(stderr,"La conexión a 'grupopg' falló.\n");
		fprintf(stderr,"%s",PQerrorMessage(conn));
		exit_nicely(1);
	}

	/* comenzar un bloque transaccional */
	query("begin;");
	/* PQclear limpia el PGresult cuando ya no se necesita */
	PQclear(res);

	/* meter aqui el choro del ejemplo */
	sprintf(q, "insert into miembros values(nextval('s_miembros'), '%s', '%s', '%s', %d);", argv[1], argv[2], argv[3], atoi(argv[4]));
	query(q);

	/* cerrar la transacción */
	query("commit;");
	PQclear(res);

	exit_nicely(0);

	return 0;	// Sí, ya sé que nunca llegaremos aquí. ¿Y qué?
}

--------------485152242EC8BBA5C2A4B3C0
Content-Type: text/plain; charset=us-ascii; name="Makefile"
Content-Transfer-Encoding: 7bit
Content-Disposition: inline; filename="Makefile"

PROGS=insertMiembro showMiembros
CFLAGS=-g -I/usr/local/pgsql/include
LDFLAGS=-L/usr/local/pgsql/lib
LIBS=-lcrypt -lpq -lm

.c:
	$(CC) $< -o $* $(CFLAGS) $(LDFLAGS) $(LIBS)

all: $(PROGS)

--------------485152242EC8BBA5C2A4B3C0
Content-Type: text/plain; charset=iso-8859-1; name="showMiembros.c"
Content-Transfer-Encoding: 8bit
Content-Disposition: inline; filename="showMiembros.c"

#include <math.h>
#include <stdio.h>
#include <time.h>
#include <libpq-fe.h>

PGconn *conn;
PGresult *res;

void exit_nicely(int status) {
	PQfinish(conn);
	exit(status);
}

void query(char *qStr) {
	res = PQexec(conn, qStr);
	if(res==NULL) {
		fprintf(stderr, "Error: res==NULL\n");
		fprintf(stderr, "Query: %s\n\n", qStr);
		PQclear(res);
		exit_nicely(1);
	}
	if(PQresultStatus(res)!=PGRES_COMMAND_OK && PQresultStatus(res)!=PGRES_TUPLES_OK) {
		fprintf(stderr, "Error: %d\n", PQresultStatus(res));
		fprintf(stderr, "Query: %s\n\n", qStr);
		PQclear(res);
		exit_nicely(1);
	}
}

main(int argc, char *argv[]) {
	char *pghost=NULL, *pgport=NULL, *pgoptions=NULL, *pgtty=NULL;
	char q[256];

	/* abrir la conexión */
	conn = PQsetdb(pghost, pgport, pgoptions, pgtty, "grupopg");
	if (PQstatus(conn) == CONNECTION_BAD) {
		fprintf(stderr,"La conexión a 'grupopg' falló.\n");
		fprintf(stderr,"%s",PQerrorMessage(conn));
		exit_nicely(1);
	}

	/* comenzar un bloque transaccional */
	query("begin;");
	/* PQclear limpia el PGresult cuando ya no se necesita */
	PQclear(res);

	/* meter aqui el choro del ejemplo */
	query("declare c cursor for select trim(nombres) as nombres, trim(apellidos) as apellidos, trim(nombre) as empresa from miembros, empresas where empresa=empresas.id;");
	PQclear(res);

	for(query("fetch in c;");
		PQntuples(res)>0;
		PQclear(res), query("fetch in c;")) {

		printf("%s %s (%s)\n", PQgetvalue(res, 0, 0), PQgetvalue(res, 0, 1), PQgetvalue(res, 0, 2));
	}
	PQclear(res);

	query("close c;");
	PQclear(res);

	/* cerrar la transacción */
	query("commit;");
	PQclear(res);

	exit_nicely(0);

	return 0;	// Sí, ya sé que nunca llegaremos aquí. ¿Y qué?
}

--------------485152242EC8BBA5C2A4B3C0--

--------- Pie de mensaje -------------------------------------------
Archivo historico: http://tlali.iztacala.unam.mx/maillist/pgsql-ayuda
Cancelar inscripcion:
mail to: majordomo@tlali.iztacala.unam.mx
text   : cancelacion pgsql-ayuda