pg.ddx.io  pgsql-interfaces@postgresql.org mailing list archive  
help / color / mirror / Atom feed
Need help on bytea usage
2+ messages / 2 participants
[nested] [flat]

* Need help on bytea usage
@ 2003-06-30 17:27  Simeon Mariasoosai <Simeon.Mariasoosai@StartelCorp.com>
  0 siblings, 0 replies; 2+ messages in thread

From: Simeon Mariasoosai @ 2003-06-30 17:27 UTC (permalink / raw)
  To: pgsql-interfaces

Hi,

I am trying to use bytea in postgres to store binary data such as a
structure. I used sql insert statement. I tried malloc and PQescapeBytea in
vain.

i tried palloc but get undefined references to CurrentMemoryContext and
MemoryContextAlloc. 

I am trying to find any sample code that inserts a binary data using bytea
storage type in postgres. I am using linux, and libpq libraries.

I couldnt find any sample code so far. I greatly appreciate your help in
pointing me towards some sample code that does this.

Thanks

Simon



^ permalink  raw  reply  [nested|flat] 2+ messages in thread

* Re: Need help on bytea usage
@ 2003-07-07 09:27  Christoph Haller <ch@rodos.fzk.de>
  0 siblings, 0 replies; 2+ messages in thread

From: Christoph Haller @ 2003-07-07 09:27 UTC (permalink / raw)
  To: pgsql-interfaces; Simeon.Mariasoosai@StartelCorp.com

Simon,
My reply is pretty late, maybe you've already find out on your own.
Here is what I can show you

...
struct measurestation {
 int Id ;
 char Name[NAMELEN] ;
 char ProviderName[NAMELEN] ;
 char ProviderTime[15] ;
 float Longitude ;
 float Latitude ;
 int Height ;
 int nValue ;
 float Value ;
 int MeasureHght ;
 struct measurestation* next ;
} ;
typedef struct measurestation MeasureValues ;
...
    char CmdLine[1024];
    PGconn     *conn;
    PGresult   *res;

    MeasureValues mval ;
    char ByteaString[1024];
    size_t binarylen;

    /* insert a C struct as bytea type */
    memset ( &mval , 0 , sizeof(MeasureValues) ) ;
    strcpy ( mval.Name , "stat-name" ) ;
    strcpy ( mval.ProviderName , "prov-name" ) ;

/*
keep in mind libpq is not thread-safe,
so what I use as buffer 'ByteaString' should be 4 times the
sizeof(MeasureValues)
to be on the safe side (all non-printables bytes become four byte octets
\nnn)
*/
    strcpy ( ByteaString , (const char *)
                           PQescapeBytea((unsigned char *) &mval ,
                                         sizeof(MeasureValues),
&binarylen));
    sprintf(CmdLine, "INSERT INTO bytea_tab ( bytea_col ) VALUES
('%s');",
                       ByteaString);
    res = PQexec(conn, CmdLine);
    if (!res || PQresultStatus(res) != PGRES_COMMAND_OK)
    {
        fprintf(stderr, "INSERT command failed\n");
        PQclear(res);
        exit_nicely(conn);
    }

This works for me.
Regards, Christoph





^ permalink  raw  reply  [nested|flat] 2+ messages in thread


end of thread, other threads:[~2003-07-07 09:27 UTC | newest]

Thread overview: 2+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2003-06-30 17:27 Need help on bytea usage Simeon Mariasoosai <Simeon.Mariasoosai@StartelCorp.com>
2003-07-07 09:27 Re: Need help on bytea usage Christoph Haller <ch@rodos.fzk.de>

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox