dbTalk Databases Forums  

Write binary data from database to file using libpq

comp.databases.postgresql comp.databases.postgresql


Discuss Write binary data from database to file using libpq in the comp.databases.postgresql forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
Julia Jacobson
 
Posts: n/a

Default Write binary data from database to file using libpq - 09-04-2010 , 02:19 AM






Dear PostgreSQL users,

I wrote a C++ program which should write binary data from a column of
type 'BYTEA' to a file. It looks like this:

#include <cstdlib>
#include <iostream>
#include <cstdio>
#include <fstream>
#include "libpq-fe.h"
using namespace std;

int main () {
PGconn *conn;
PGresult *res;
conn = PQconnectdb("hostaddr='my.database.host' port='5432'
dbname='my_db' user='user' password='secret' connect_timeout='9'");
if (PQstatus(conn) != CONNECTION_OK)
{
fprintf(stderr, "Connection to database failed: %s",
PQerrorMessage(conn));
PQfinish(conn);
exit(1);
}
res = PQexec(conn, "SELECT bindat FROM my_table WHERE bindat_id='3'");
if (PQresultStatus(res) != PGRES_TUPLES_OK)
{
fprintf(stderr, "Error: %s", PQerrorMessage(conn));
fprintf(stderr, "Error: %s", PQresultErrorMessage(res));
PQclear(res);
PQfinish(conn);
}
ofstream myfile ("data.bin", ios:ut | ios::binary);
myfile.write(PQgetvalue(res,0,0));
myfile.close();
}

Unfortunately, it doesn't work. The problem is the line
"myfile.write(PQgetvalue(res,0,0));'. Even after searching the web, I
have no idea how to write the result of PQgetvalue to a file.

Thanks in advance,
Julia

Reply With Quote
Reply




Thread Tools
Display Modes

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

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



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.