简体   繁体   中英

Insert a Binary string into MYSQL varchar column

As we all know, we will use the mysql_query api to send a query to the server, and the query are passed by a string as the parameter. And we will have to formulate the string outside the mysql_query called by some C functions like sprintf.For example,

sprintf(buffer, “insert into table(describe) values(‘%s’)”, strA);
mysql_query(..., buffer);

The ‘describe’ is a VARCHAR(150).

In some special cases, one of our functions will cat several C style string into a long one remaining all the ending '\\0' to form a binary, ie in C form catting “abc” and “efg” into “abc\\0efg\\0”, of course with the length given out to the caller(in this case, it is 8). However, the out binary can NEVER be used in the sprintf above as strA, as the C functions will truncate the string by meeting the first '\\0'.

Is there anything special we can do to fulfill our needs? We want to insert a binary into a column defined as VARCHAR. We have tried to change all the '\\0' into '\\0' literally, which seems to work good but time and codes consuming. Is there any alternative easier method?

Thanks in advance.

您应该使用mysql_real_escape_string()来转义该字符串。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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