pg.ddx.io  pgsql-interfaces@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Christoph Haller <ch@rodos.fzk.de>
To: pgsql-interfaces@postgresql.org
To: Simeon.Mariasoosai@StartelCorp.com
Subject: Re: Need help on bytea usage
Date: Mon, 07 Jul 2003 11:27:00 +0200
Message-ID: <3F093CE3.BE88DC98@rodos.fzk.de> (raw)

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





view thread (2+ messages)

Message-ID: <3F093CE3.BE88DC98@rodos.fzk.de>
Permalink:  ../3F093CE3.BE88DC98@rodos.fzk.de/
Also on:    postgresql.org/message-id/3F093CE3.BE88DC98@rodos.fzk.de

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: pgsql-interfaces@postgresql.org
  Cc: ch@rodos.fzk.de, Simeon.Mariasoosai@StartelCorp.com
  Subject: Re: Need help on bytea usage
  In-Reply-To: <3F093CE3.BE88DC98@rodos.fzk.de>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

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