LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Server
User Name
Password
Linux - Server This forum is for the discussion of Linux Software used in a server related context.

Notices


Reply
  Search this Thread
Old 02-17-2014, 02:18 AM   #1
Nikhil_Lagwankar
Member
 
Registered: Aug 2013
Location: Pune
Distribution: Centos, Ubuntu, Fedora
Posts: 36

Rep: Reputation: 0
postgres output from query to file via c code


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?
 
Old 02-17-2014, 07:47 AM   #2
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,702

Rep: Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895
The \o is only an option of psql. You need to use standard c to open/write files.

What is the res value when you execute PQexec(conn,st)?

Last edited by michaelk; 02-17-2014 at 07:49 AM.
 
1 members found this post helpful.
Old 02-18-2014, 10:56 PM   #3
Nikhil_Lagwankar
Member
 
Registered: Aug 2013
Location: Pune
Distribution: Centos, Ubuntu, Fedora
Posts: 36

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by michaelk View Post
The \o is only an option of psql. You need to use standard c to open/write files.

What is the res value when you execute PQexec(conn,st)?
There's nothing in res....I did a printf of res and it came blank.
 
Old 02-19-2014, 12:52 AM   #4
Nikhil_Lagwankar
Member
 
Registered: Aug 2013
Location: Pune
Distribution: Centos, Ubuntu, Fedora
Posts: 36

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by michaelk View Post
The \o is only an option of psql. You need to use standard c to open/write files.
Thanks, This works ...
 
Old 02-19-2014, 05:00 AM   #5
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,702

Rep: Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895
A little but this should work too.
http://www.postgresql.org/docs/9.1/static/sql-copy.html
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
MySQL query output to excel file using perl script listenerit Linux - Newbie 3 02-17-2014 02:35 AM
[SOLVED] postgres output from query to file... Nikhil_Lagwankar Linux - Server 1 02-16-2014 07:10 AM
hibernate - postgres: translated query syntax is not correct eantoranz Programming 0 02-07-2014 07:44 PM
postgres - query from 9.x client to a 8.x server hangs eantoranz Linux - Software 1 05-14-2012 09:24 PM
Query a postgres database with Java cov Linux - Server 1 12-12-2009 09:29 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Server

All times are GMT -5. The time now is 07:20 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration