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

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: 2005-12-15 13:10:05
Message-ID: 43A16B2D.20203@rambler.ru (view raw or flat)
Thread:
Lists: 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.

Responses

pgsql-interfaces by date

Next:From: Daniel ArielDate: 2005-12-15 13:27:26
Subject: Re: Obtaining information on the schema of tables which
Previous:From: Daniel ArielDate: 2005-12-15 10:50:11
Subject: Re: Obtaining information on the schema of tables which

Privacy Policy | About PostgreSQL
Copyright © 1996-2014 The PostgreSQL Global Development Group