Extracting data from BYTEA column to binary file using libpq

Started by Julia Jacobsonover 15 years ago3 messagesgeneral
Jump to latest
#1Julia Jacobson
julia.jacobson@arcor.de

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

#2Daniel Verite
daniel@manitou-mail.org
In reply to: Julia Jacobson (#1)
Re: Extracting data from BYTEA column to binary file using libpq

Julia Jacobson wrote:

ofstream myFile ("picture.jpg", ios::out | ios::binary);
myFile.write (contents);

You must specify the number of bytes to write.

Best regards,
--
Daniel
PostgreSQL-powered mail user agent and storage: http://www.manitou-mail.org

#3Diego Schulz
dschulz@gmail.com
In reply to: Julia Jacobson (#1)
Re: Extracting data from BYTEA column to binary file using libpq

On Tue, Sep 14, 2010 at 6:01 PM, Julia Jacobson <julia.jacobson@arcor.de> wrote:

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

--
Sent via pgsql-general mailing list (pgsql-general@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-general

Hi,

In addition to what Daniel Verite said, I think you should use numeric
IP address instead of the host name, or consider replacing 'hostaddr'
with 'host' if you plan to use host names.
'hostaddr' is meant to be used when you want to avoid the name resolution step.

conn = PQconnectdb("host='databaseserver.com' port='5432'
dbname='test_db' user='test_user' password='secret'");

regards,

diego