Search Postgresql Archives

Extracting data from BYTEA column to binary file using libpq

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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@xxxxxxxxxxxxxx)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-general


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Index of Archives]     [Postgresql Jobs]     [Postgresql Admin]     [Postgresql Performance]     [Linux Clusters]     [PHP Home]     [PHP on Windows]     [Kernel Newbies]     [PHP Classes]     [PHP Books]     [PHP Databases]     [Postgresql & PHP]     [Yosemite]
  Powered by Linux