Pregunta

I'm coding in C and using Libpq library of Postgresql and I would like to store a PNG image into the database in "bytea" type. I have been searching on the net for hours and couldn't find a good example to handle this work, so wanted to write here and ask for your help.

I have 12 params to bind and one of them is PNG image. The rest are char*, and no problem with them.

Below is what I have tried so far. (I'm writing the necessary part of code):

    PGresult   *res;
    PGconn *conn;

    const char *paramValues[12];
    int         paramLengths[12];
    int         paramFormats[12];

    const char* imageFrame=frameImageArray.data();// frameImageArray.data is const char*.
    int imageSize=frameImageArray.size();

    paramFormats[11]=1;
    paramLengths[11]=imageSize;
    paramValues[11]= imageFrame;


// insertplate is a function on db
    res = PQexecParams(conn,
    "SELECT insertplate($1,$2,$3,$4,$5,$6,$7,$8,$9,$10,$11,$12)",
    12,              // param number 
    NULL,            // oid param type 
    paramValues,     // param values
    paramLengths,    // param lengths 
    paramFormats,    // params format, 1 for binary
    1);              //1 for binary result 

It is compiled with no problem but when it comes to store the image to db on runtime, the classical runtime error occures :

"Unhandled exception at 0x6d3dc220 in ..._debug.exe: 0xC0000005: Access violation reading location 0x000000007f91e508."

Seems something about memory handling.

Whatever I tried, I couldn't make it run and I'm not able to see my error. Do I have to use Oids for sending binary data to db with PQexecParams? Or something else I'm missing ? I really appreciate if someone help me with this.

Thanks in advance.

Edit: I just realised that if I use Insert statement, it works well, but this function doesn't. Normally it works. Weird.

¿Fue útil?

Solución

I finally find the error.

//paramFormats[0]=0;
//paramFormats[1]=0;
//paramFormats[2]=0;
//paramFormats[3]=0;
//paramFormats[4]=0;
//paramFormats[5]=0;
//paramFormats[6]=0;
//paramFormats[7]=0;
//paramFormats[8]=0;
//paramFormats[9]=0;
//paramFormats[10]=0;
//paramFormats[11]=1;

It is possible to leave the "parameter format" as NULL, but I wanted to set only "paramFormats[11]" as seen above. I have set the others to 0 as well and it worked. I didn't expect something like this.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top