Evgueni Kolossov
Evgueni Kolossov

Reputation:

How to INSERT binary std::string into BLOB

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!!!

Upvotes: 5

Views: 4410

Answers (1)

anon
anon

Reputation:

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

Upvotes: 2

Related Questions