pg.ddx.io pgsql-sql@postgresql.org mailing list archive
help / color / mirror / Atom feedjoin question - requesting for a simple C program where it can INSERT data into database as reference
2+ messages / 2 participants
[nested] [flat]
* Re: join question - requesting for a simple C program where it can INSERT data into database as reference
@ 2002-06-14 02:03 joo <joo@sebasasia.com>
1970-01-01 00:00 ` join question - requesting for a simple C program where it can INSERT data into database as reference Christoph Haller <ch@rodos.fzk.de>
0 siblings, 1 reply; 2+ messages in thread
From: joo @ 2002-06-14 02:03 UTC (permalink / raw)
To: pgsql-sql
My problem is, i am writing a simple retrieval program using C, to retrieve
a set of records from database. Then i'll make certain calculation based on
the data that i have retrieved and write it on a new database. I have follow
the sample program to do the retrieval and it works. But i don't seems to
have any samples to do an INSERT to the database? please anyone who have
such simple or working programs where it can INSERT to any database , please
reply to me as a reference.
here is the sample program that i used to do my retrieval:
#include <stdio.h>
#include "libpq-fe.h"
void
exit_nicely(PGconn *conn)
{
PQfinish(conn);
exit(1);
}
main()
{
char *pghost,
*pgport,
*pgoptions,
*pgtty;
char *dbName;
int nFields;
int i,
j;
PGconn *conn;
PGresult *res;
pghost = NULL;
pgport = NULL;
pgoptions = NULL;
pgtty = NULL;
dbName = "template1";
conn = PQsetdb(pghost, pgport, pgoptions, pgtty, dbName);
if (PQstatus(conn) == CONNECTION_BAD)
{
fprintf(stderr, "Connection to database '%s' failed.\n", dbName);
fprintf(stderr, "%s", PQerrorMessage(conn));
exit_nicely(conn);
}
res = PQexec(conn, "BEGIN");
if (!res || PQresultStatus(res) != PGRES_COMMAND_OK)
{
fprintf(stderr, "BEGIN command failed\n");
PQclear(res);
exit_nicely(conn);
}
PQclear(res);
res = PQexec(conn, "DECLARE mycursor CURSOR FOR select * from event");
if (!res || PQresultStatus(res) != PGRES_COMMAND_OK)
{
fprintf(stderr, "DECLARE CURSOR command failed\n");
PQclear(res);
exit_nicely(conn);
}
PQclear(res);
res = PQexec(conn, "FETCH ALL in mycursor");
if (!res || PQresultStatus(res) != PGRES_TUPLES_OK)
{
fprintf(stderr, "FETCH ALL command didn't return tuples
properly\n");
PQclear(res);
exit_nicely(conn);
}
nFields = PQnfields(res);
for (i = 0; i < nFields; i++)
printf("%-15s", PQfname(res, i));
printf("\n\n");
for (i = 0; i < PQntuples(res); i++)
{
for (j = 0; j < nFields; j++)
printf("%-15s", PQgetvalue(res, i, j));
printf("\n");
}
PQclear(res);
res = PQexec(conn, "CLOSE mycursor");
PQclear(res);
res = PQexec(conn, "COMMIT");
PQclear(res);
PQfinish(conn);
return 0;
}
Thanks,
joo
^ permalink raw reply [nested|flat] 2+ messages in thread
* join question - requesting for a simple C program where it can INSERT data into database as reference
2002-06-14 02:03 Re: join question - requesting for a simple C program where it can INSERT data into database as reference joo <joo@sebasasia.com>
@ 1970-01-01 00:00 ` Christoph Haller <ch@rodos.fzk.de>
0 siblings, 0 replies; 2+ messages in thread
From: Christoph Haller @ 1970-01-01 00:00 UTC (permalink / raw)
To: joo@sebasasia.com; +Cc: pgsql-sql
joo,
it's just like
res = PQexec(conn, "BEGIN");
do
res = PQexec(conn, "insert into events values('blabla', ...)");
if (!res || PQresultStatus(res) != PGRES_COMMAND_OK)
tells you if your command was ok
if you do an update or delete
atoi(PQcmdTuples(res))
tells you how many rows are affected
I hope this helps.
Regards, Christoph
> My problem is, i am writing a simple retrieval program using C, to retrieve
> a set of records from database. Then i'll make certain calculation based on
> the data that i have retrieved and write it on a new database. I have follow
> the sample program to do the retrieval and it works. But i don't seems to
> have any samples to do an INSERT to the database? please anyone who have
> such simple or working programs where it can INSERT to any database , please
> reply to me as a reference.
>
>
> here is the sample program that i used to do my retrieval:
>
>
> #include <stdio.h>
> #include "libpq-fe.h"
>
> void
> exit_nicely(PGconn *conn)
> {
> PQfinish(conn);
> exit(1);
> }
>
> main()
> {
> char *pghost,
> *pgport,
> *pgoptions,
> *pgtty;
> char *dbName;
> int nFields;
> int i,
> j;
>
>
>
> PGconn *conn;
> PGresult *res;
>
> pghost = NULL;
> pgport = NULL;
> pgoptions = NULL;
>
> pgtty = NULL;
> dbName = "template1";
>
>
> conn = PQsetdb(pghost, pgport, pgoptions, pgtty, dbName);
>
>
> if (PQstatus(conn) == CONNECTION_BAD)
> {
> fprintf(stderr, "Connection to database '%s' failed.\n", dbName);
> fprintf(stderr, "%s", PQerrorMessage(conn));
> exit_nicely(conn);
> }
>
>
> res = PQexec(conn, "BEGIN");
> if (!res || PQresultStatus(res) != PGRES_COMMAND_OK)
>
> {
> fprintf(stderr, "BEGIN command failed\n");
> PQclear(res);
> exit_nicely(conn);
> }
>
>
> PQclear(res);
>
>
> res = PQexec(conn, "DECLARE mycursor CURSOR FOR select * from event");
> if (!res || PQresultStatus(res) != PGRES_COMMAND_OK)
> {
> fprintf(stderr, "DECLARE CURSOR command failed\n");
> PQclear(res);
> exit_nicely(conn);
> }
> PQclear(res);
> res = PQexec(conn, "FETCH ALL in mycursor");
> if (!res || PQresultStatus(res) != PGRES_TUPLES_OK)
> {
> fprintf(stderr, "FETCH ALL command didn't return tuples
> properly\n");
> PQclear(res);
> exit_nicely(conn);
> }
>
>
> nFields = PQnfields(res);
> for (i = 0; i < nFields; i++)
> printf("%-15s", PQfname(res, i));
> printf("\n\n");
>
>
> for (i = 0; i < PQntuples(res); i++)
> {
> for (j = 0; j < nFields; j++)
> printf("%-15s", PQgetvalue(res, i, j));
> printf("\n");
> }
> PQclear(res);
>
>
> res = PQexec(conn, "CLOSE mycursor");
> PQclear(res);
>
> res = PQexec(conn, "COMMIT");
> PQclear(res);
>
>
> PQfinish(conn);
>
> return 0;
>
> }
> Thanks,
> joo
^ permalink raw reply [nested|flat] 2+ messages in thread
end of thread, other threads:[~2002-06-14 02:03 UTC | newest]
Thread overview: 2+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2002-06-14 02:03 Re: join question - requesting for a simple C program where it can INSERT data into database as reference joo <joo@sebasasia.com>
1970-01-01 00:00 ` Christoph Haller <ch@rodos.fzk.de>
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