Обсуждение: Need help on bytea usage

Поиск
Список
Период
Сортировка

Need help on bytea usage

От
Simeon Mariasoosai
Дата:
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


Re: Need help on bytea usage

От
Christoph Haller
Дата:
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
;floatLatitude ;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