How to INSERT binary std::string into BLOB

谁说胖子不能爱 提交于 2020-01-02 05:19:05

问题


I have binary std::string and I need insert it into the BLOB (MySQL) using simple data layer I have. So, I need to execute query: ExecuteSQL((LPTSTR)strQ).

When I am creating this query string (strQ) I cannot add anything to the string after I add this binary string - it just kind if terminated and nothing can be added. I do not want to use mysql_real_escape_string because i want to keep it not only for MySQL.

Anybody to HELP PLEASE!!!


回答1:


Assuming you have code that looks something like this:

std::string s = ...      // populate string somehow
ExecuteSQL( (LPCSTR) s );

Then you have several problems. Forstly, the cast won't work. In C++, whenever you use a cast you are almost certainly doing something incorrect which will break your code. You need to create a null-terminated string using the std::string member function c_str():

ExecuteSQL( s.c_str() );

However, this may not fix all your your problems because you say you hava a binary string. If that string contains the zero byte, then your SQL will terminate at that character rather than the end of string. In that case you probably need to investigate binding your values explicitly.

Edit: For details of how to bind a parameter to a MySQL statement, see http://dev.mysql.com/doc/refman/5.1/en/mysql-stmt-bind-param.html



来源:https://stackoverflow.com/questions/733262/how-to-insert-binary-stdstring-into-blob

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!