Skip site navigation (1) Skip section navigation (2)

Peripheral Links

Header And Logo

PostgreSQL
| The world's most advanced open source database.

Site Navigation

Search archives
  Advanced Search

Get double values from binary cursor


  • From: Oleg Semykin <oss-dev(at)rambler(dot)ru>
  • To: pgsql-interfaces(at)postgresql(dot)org
  • Subject: Get double values from binary cursor
  • Date: Thu, 15 Dec 2005 16:10:05 +0300
  • Message-id: <43A16B2D.20203@rambler.ru> <text/plain>

Hi, all.

i want to get a double values from binary cursor

in libpq < 7.4 it was simple
memcpy(&dfVal, PQgetvalue(...), sizeof(dfVal));

but now we have to convert data from MSB to LSB format
it is ok for int
but how to convert a double

i'am tried to write a simple program, but it does not work ....

#include <libpq-fe.h>
#include <netinet/in.h>
#include <stdio.h>
#include <string.h>

typedef unsigned int int4;
typedef double       float8;

int main()
{
   if ( sizeof(int4) * 2  != sizeof(float8) )
   {
       printf("Error");
       return 1;
   }

   PGconn * conn = PQconnectdb("dbname=test host=localhost user=trusted");
   PGresult * res;

PQclear( PQexec(conn, "begin; declare my_cur binary cursor for select 123.123;") );
   res = PQexec(conn,"fetch 1 from my_cur");

   float8 dfVal;
   float8 dfOrig = 123.123;

   union
   {
       float8  f;
       int4    n[2];
   } swap;

   memcpy(&(swap.n), PQgetvalue( res, 0, 0 ), sizeof(int4) * 2 );

   int4 tmp  = ntohl(swap.n[0]);
   swap.n[0] = ntohl(swap.n[1]);
   swap.n[1] = tmp;

   dfVal = swap.f;

   printf("ret = [%f]\norig = [%f]\n", dfVal, orig);

   PQclear(res);
   PQfinish(conn);
   return 0;
}

Is something wrong?
Thank for any help.



Home | Main Index | Thread Index

Privacy Policy | About PostgreSQL
Copyright © 1996 – 2012 PostgreSQL Global Development Group