Re: Need help on bytea usage

From: Christoph Haller <ch(at)rodos(dot)fzk(dot)de>
To: pgsql-interfaces(at)postgresql(dot)org, Simeon(dot)Mariasoosai(at)StartelCorp(dot)com
Subject: Re: Need help on bytea usage
Date: 2003-07-07 09:27:00
Message-ID: 3F093CE3.BE88DC98@rodos.fzk.de
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-interfaces

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

Browse pgsql-interfaces by date

  From Date Subject
Next Message lerkista 2003-07-07 15:40:03 Migrating DB
Previous Message Nestor A. Diaz L. 2003-07-06 03:08:39 PL/Python char(n) problem