Retrieving image file from Postgresql database returns two times bigger data inserted (by libpq library)

StackOverflow https://stackoverflow.com/questions/15448596

  •  24-03-2022
  •  | 
  •  

문제

I'm using libpq library of Postgresql database and I have a problem about retrieving the bytea data from db. I couldn't solve my problem, so wanted to ask you. Whenever I try to insert an image file into db and get it back, the size of returning data is always 2 times of the data I sent. Actually, the exact equation is : retrieved data = sent data * 2 + 2.(in bytes). So, I couldn't figure it out yet, I'm probably missing something :/

An example code I use is like below :

PGresult*    res;
const char*  paramValues[1] = {plateImageArray.data()}; // data type is const char*
int          paramLengths[1] = {plateImageArray.size()}; // size of image is 58573 bytes
int          paramFormats[1] = {1}; // 1 for byte, 0 for text

res = PQexecParams(conn,
        "INSERT INTO \"plates\" (plateImage) VALUES ($1::bytea)",
        1,               /* param number */
        NULL,            /* param type */
        paramValues,     /* param values*/
        paramLengths,    /* param lengths */
        paramFormats,    /* default to all text params */
        1);              /* return type, 1 for text. */

res = PQexec(conn,"Select * from \"plate_images\" WHERE ...."); /*returns 1 tuple : something / something / image(bytea) */

int lengthOfPlateImage=PQgetlength(res,0,2); // indicates 117148 bytes, exactly 2 times of sent data +2 

I appreciate if you guys help me. I'm deadly bored of searching about the reason of it.

Thanks in advance.

Edit: I tracked the data stored in disk and the size of inserted data is the same (58573 byte). Two times bigger data becomes while getting it ...

도움이 되었습니까?

해결책

Okay, the query made with "PQexec" command returns the value in text format and it has no parameter to tell whether you want the result in text format or byte. I am not sure if a better and more suitable command exists to query it but I used "PQexecParams" and set the return type 1 (1 for byte, 0 for text) and it returned the exact data with the expected size. Thanks anyhow.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top