pg.ddx.io  pgsql-interfaces@postgresql.org mailing list archive  
help / color / mirror / Atom feed
Get double values from binary cursor
2+ messages / 2 participants
[nested] [flat]

* Get double values from binary cursor
@ 2005-12-15 13:10  Oleg Semykin <oss-dev@rambler.ru>
  0 siblings, 1 reply; 2+ messages in thread

From: Oleg Semykin @ 2005-12-15 13:10 UTC (permalink / raw)
  To: pgsql-interfaces

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.



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

* Re: Get double values from binary cursor
@ 2005-12-15 18:02  Tom Lane <tgl@sss.pgh.pa.us>
  parent: Oleg Semykin <oss-dev@rambler.ru>
  0 siblings, 0 replies; 2+ messages in thread

From: Tom Lane @ 2005-12-15 18:02 UTC (permalink / raw)
  To: oss-dev@rambler.ru; +Cc: pgsql-interfaces

Oleg Semykin <oss-dev@rambler.ru> writes:
> i want to get a double values from binary cursor

>     PQclear( PQexec(conn, "begin; declare my_cur binary cursor for 
> select 123.123;") );

This is most likely returning a numeric, not a float8.  Try casting
the value to float8 explicitly.

			regards, tom lane



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


end of thread, other threads:[~2005-12-15 18:02 UTC | newest]

Thread overview: 2+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2005-12-15 13:10 Get double values from binary cursor Oleg Semykin <oss-dev@rambler.ru>
2005-12-15 18:02 ` Tom Lane <tgl@sss.pgh.pa.us>

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