采用这种连接的方式进行插入,还是会遇到erro 1064 syntax error的提示。

由于在数据表中定义了一个字段是varbinary,需要利用C API进行插入操作,但是试了N种方法均不行,下面列举一下我们使用过的方式:(bina为char数组,也即待插入的二进制数据,其中可能含有各种字符;)
1.

sprintf(str,"INSERT INTO test(id,test)VALUES(73,‘%s’)",bina);int res=mysql_query(conn_ptr,str);

这种方式经常出现错误,是因为使用了字符串的格式化符%s,当作字符串进行插入,但是遇到bina中的\0字符就认为结束,故该方式不行;
2.

char *test="INSERT INTO test(id,test)VALUES(12,'";
strcpy(str,test);
mysql_real_escape_string(conn_ptr,str+37,bina,5);
strcat(str,"')");int res=mysql_real_query(conn_ptr,str,47);

请问大家有没有什么好的解决方案?


汪汪一只猫
浏览 77回答 1
1回答

Helenr

把bina做一次编码,比如bin2hex#include&nbsp;<math.h>#include&nbsp;<string.h>#include&nbsp;<stdio.h>#include&nbsp;<stdlib.h>#include&nbsp;<conio.h> &nbsp;#define&nbsp;FirstDigit&nbsp;48 &nbsp;void&nbsp;BinToHex(char*&nbsp;BinData,&nbsp;char*&nbsp;HexData){&nbsp;&nbsp;&nbsp;long&nbsp;int&nbsp;Number&nbsp;=&nbsp;0;&nbsp;&nbsp;&nbsp;int&nbsp;BinLength&nbsp;=&nbsp;strlen(BinData);&nbsp; &nbsp;&nbsp;&nbsp;for(int&nbsp;i=0;&nbsp;i<BinLength;&nbsp;i++) &nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Number&nbsp;+=&nbsp;((BinData[BinLength&nbsp;-&nbsp;i&nbsp;-&nbsp;1]&nbsp;-&nbsp;FirstDigit)&nbsp;*&nbsp;pow(2,&nbsp;i)); &nbsp;&nbsp;&nbsp;}&nbsp; &nbsp;&nbsp;&nbsp;ltoa(Number,&nbsp;HexData,&nbsp;16); }&nbsp; int&nbsp;main(){&nbsp;&nbsp;&nbsp;char*&nbsp;BinBuffer&nbsp;=&nbsp;"11110101";&nbsp;&nbsp;&nbsp;char&nbsp;HexBuffer[256];
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

MySQL