简体   繁体   English

使用libpq库将图像发送到Postgresql数据库中的bytea列的错误

[英]Error about sending an image into bytea column in postgresql db with libpq library

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. 我在C中进行编码,并使用Postgresql的Libpq库,我想将PNG图像以“ bytea”类型存储到数据库中。 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. 我有12个要绑定的参数,其中之一是PNG图像。 The rest are char*, and no problem with them. 其余的都是char *,它们没有问题。

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 : 它的编译没有问题,但是在运行时将映像存储到db时,会发生经典的运行时错误:

"Unhandled exception at 0x6d3dc220 in ..._debug.exe: 0xC0000005: Access violation reading location 0x000000007f91e508." “ ..._ debug.exe中的0x6d3dc220处未处理的异常:0xC0000005:访问冲突读取位置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? 我是否必须使用Oids通过PQexecParams将二进制数据发送到db? 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. 编辑:我刚刚意识到,如果我使用Insert语句,它可以很好地工作,但是此功能不能。 Normally it works. 通常正常。 Weird. 奇怪的。

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. 可以将“参数格式”保留为NULL,但是如上所述,我只想设置“ paramFormats [11]”。 I have set the others to 0 as well and it worked. 我也将其他设置为0并成功。 I didn't expect something like this. 我没想到会这样。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM