Hi,
I'm using CentOS 6.4 (Final) and PostgresSQL 8.4.
I have to get the output of a query to a text file.
If i do the manual way this works however I need to accomplish this using the C - code.
My C code is as below
Code:
int main() {
PGconn *conn;
PGresult *res;
int rec_count;
int row;
int col;
char st[50]="\\o /usr/tmp/posttemp/test.txt";
puts(st);
conn = PQconnectdb("dbname=postgres user=postgres password=postgres");
if (PQstatus(conn) == CONNECTION_BAD) {
puts("We were unable to connect to the database");
exit(0);
}
res = PQexec(conn,st);
res = PQexec(conn,"select * from pg_catalog.pg_user");
if (PQresultStatus(res) != PGRES_TUPLES_OK) {
puts("We did not get any data!");
exit(0);
}
rec_count = PQntuples(res);
printf("We received %d records.\n", rec_count);
puts("==========================");
for (row=0; row<rec_count; row++) {
for (col=0; col<3; col++) {
printf("%s\t", PQgetvalue(res, row, col));
}
puts("");
}
puts("==========================");
PQclear(res);
PQfinish(conn);
return 0;
}
There's no error in code compilation.
Query gives o/p on the terminal via this code however none in file specified.
What's going wrong here?