tags:

views:

1591

answers:

1

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 aded. I do not want to use mysql_real_escape_string staff because i want to keep it not only for MySQL. Anybody to HELP PLEASE!!!

+2  A: 

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

anon
Yes, (LPCSTR) s is created using strSQL.c_str() but first we need to create this strSQL (std::string) like: strSQL = "INSERT INTO mydatabase.mytable (col1, BLOBCol2) VALUES('"; strSQL += strForCol1; strSql += "', '"; and now we need to add this binary string...
The main problem is an escape characters and the main question is - can we do it without using mysql_real_escape_string? Any examples will be a great help. Talking about binding values - can you give an example, please?