Как использовать двоичный результат postgresql с помощью libpq - PullRequest
0 голосов
/ 23 января 2019

Я пытаюсь связать двоичные результаты PQexecPrepare с такими переменными, как int, float ... Я не знаю, как это сделать. Моя цель: с помощью длины и двоичного значения я могу указать результат на указатель типа int или float. В общем случае я буду использовать это, чтобы связать любую переменную с помощью функции BindResult (T *, int col). Не могли бы вы помочь мне?

#include <iostream>
#include "libpq-fe.h"

/*

CREATE TABLE fruits(Id INTEGER PRIMARY KEY, Name VARCHAR(20), Price FLOAT);
INSERT INTO fruits VALUES(1,'apple',1.9);
INSERT INTO fruits VALUES(2,'banana',1.1);
INSERT INTO fruits VALUES(3,'pear',2.2);
INSERT INTO fruits VALUES(4,'orange',3.2);

*/
void exit_nicely(PGconn *conn)
{
    PQfinish(conn);
    exit(1);
}


int main(int argc, char **argv)
{
    PGconn     *conn;
    PGresult   *res;

    conn =  PQsetdbLogin("127.0.0.1",
                     "5432",
                     NULL,
                     NULL,
                     "testdb",
                     "postgres",
                     "password");
    /* Check to see that the backend connection was successfully made */
    if (PQstatus(conn) != CONNECTION_OK)
    {
        fprintf(stderr, "Connection to database '%s' failed.\n", PQdb(conn));
        fprintf(stderr, "%s", PQerrorMessage(conn));
        exit_nicely(conn);
    }

    const char *paramValues[1];
    int         paramLengths[1];
    int         paramFormats[1];
    paramValues[0] = "1";
    res = PQexecParams(conn,
                   "SELECT * FROM FRUITS WHERE ID>$1",
                   1,       /* one param */
                   NULL,    /* let the backend deduce param type */
                   paramValues,
                   NULL,
                   NULL,
                   1); //binary result

    if(PQresultStatus(res) != PGRES_TUPLES_OK)
        exit_nicely(conn);
    int *iID;
    float *fPrice ;
    for( int row = 0 ; row <  PQntuples(res) ; ++row )
    {

    int length= PQfsize(res,col);
    char *id=PQgetvalue(res,row,0);
    char *price=PQgetvalue(res,row,2);
    //how to bind the value to iID and fPrice?
    //iID 
    //fPrice 

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