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