Extracting data from BYTEA column to binary file using libpq

From: Julia Jacobson <julia(dot)jacobson(at)arcor(dot)de>
To: pgsql-general(at)postgresql(dot)org
Subject: Extracting data from BYTEA column to binary file using libpq
Date: 2010-09-14 22:01:18
Message-ID: 4C8FF0AE.5040506@arcor.de
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Hello everybody out there using PostgreSQL,

What is the problem with the following C++ code for the extraction of
data from a BYTEA column to a binary file?

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

main ()
{
PGconn *conn;
conn = PQconnectdb("hostaddr='databaseserver.com' port='5432'
dbname='test_db' user='test_user' password='secret'");
int size;
const char* contents;
PGresult* res;
res = PQexecParams(conn,
"SELECT filecontent FROM pictures WHERE picture_id='3'",
0, NULL,NULL,NULL,NULL,
1);

if (res && PQresultStatus(res)==PGRES_TUPLES_OK)
{
size = PQgetlength(res, 0, 0);
contents = PQgetvalue(res, 0, 0);
}
ofstream myFile ("picture.jpg", ios::out | ios::binary);
myFile.write (contents);
myFile.close();
}

Thanks in advance,
Julia

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Daniel Verite 2010-09-14 22:37:11 Re: Extracting data from BYTEA column to binary file using libpq
Previous Message Gary Fu 2010-09-14 21:56:46 select sql slow inside function