#include #include #include "libpq-fe.h" #include int DBinsertCmd(char *, char *, PGconn *, PGresult *); int DBcontrol(); static void exit_nicely(PGconn *conn) { PQfinish(conn); exit(1); } int main() { int i = 30; int n = 0; char *pghost, *pgport, *pgoptions, *pgtty, *dbName, *login, *pwd, *result; char *tablename = "files"; char *valueOne = "BlahCompatible"; PGconn *conn; PGresult *res; ExecStatusType status; pghost = NULL; /* host name of the backend server */ pgport = NULL; /* port of the backend server */ pgoptions = NULL; /* special options to start up the backend server */ pgtty = NULL; /* debugging tty for the backend server */ dbName = "void"; /* change this to the name of your test * database */ login = "void"; pwd = NULL; /* no pwd for now */ conn = PQsetdbLogin(pghost, pgport, pgoptions, pgtty, dbName, login, pwd); /* make a connection to the database */ if (PQstatus(conn) == CONNECTION_BAD){ /* check to see that the backend connection was successfully made */ fprintf(stderr, "Connection to database '%s' failed.\n", dbName); fprintf(stderr, "%s", PQerrorMessage(conn)); exit_nicely(conn); } res = PQmakeEmptyPGresult(conn, status); //PQclear(res); if ((DBinsertCmd(tablename, valueOne, conn, res)) == -1) fprintf(stderr,"DBinsertCmd exited with -1\n"); PQclear(res); res = PQexec(conn, "SELECT * FROM files"); while (PQresultStatus(res) == PGRES_TUPLES_OK && n < i){ result = PQgetvalue(res, n, 0); n++; fprintf(stdout,"resultaat: %s\n",result); } PQclear(res); /* PQclear(res) should PQclear PGresult whenever it is no longer needed to avoid memory leaks */ PQfinish(conn); /* close the connection to the database and cleanup */ return 0; /* Though PQfinish(conn1) has called exit(1) */ } int DBinsertCmd(char *tablename, char *valueOne, PGconn *conn, PGresult *res) { char finalCmd[512]; if ((snprintf(finalCmd, 511, "INSERT INTO %s VALUES ('%s')", tablename, valueOne)) == -1){ fprintf(stderr,"DBinsertCmd: WARNING! string \"%s\" was longer then allowed, this should not have happened",finalCmd); return -1; } fprintf(stdout,"%s\n",finalCmd); res = PQexec(conn, finalCmd); if (PQresultStatus(res) != PGRES_COMMAND_OK){ fprintf(stderr, "INSERT command failed: error returned by DB is: %s\n",PQresStatus(PQresultStatus(res))); return -1; } return 1; }