List:MySQL++« Previous MessageNext Message »
From:Bill K Date:September 24 2006 3:13am
Subject:Re: Reading Writting BLOB VARBINARY Q.
View as plain text  
The question for reading from a blob field is answered here:
http://lists.mysql.com/plusplus/5644

To write to a blob field, it is something like this:

BEGIN CODE
string strByteStream = "...";
...
mysqlpp::Query useQuery = conn.query();

useQuery << "INSERT INTO "
<< "tablename(ByteStream) "
<< "VALUES(CONVERT('" << strByteStream << "' USING binary)" <<
")";

useQuery.execute();
END CODE

Where strByteStream needs to be an escaped string. You can escape it 
yourself, or you can use the MySql C API function, "mysql_escape_string".
The characters that need escaping are (expressions typed in C++ syntax):

0x00 can be escaped with "\\0"
0x1A can be escaped with "\\Z"
'\n' can be escaped with "\\n"
'\r' can be escaped with "\\r"
'\\' can be escaped with "\\\\"
'\'' can be escaped with "\\'"
'\"' can be escaped with "\\\"";



- Bill Krahmer



Ankur G35 Saxena wrote:
> Hi, I wanted to read and write a C++ structure to the DB. I went
> through the example given and from this sinppet of code I got from the
> user mannual, had a small Q.
>
> I thought that std::string is a /0 delimited string, if so, how does
> this work? If the binary data has a /0 inbetween somwhere in there,
> wouldnt the string fill be shortened when pushed into the strbuf?
>
> ostringstream strbuf;
> char *read_buffer = new char[blen];
> In.read(read_buffer, blen);
> string fill(read_buffer, blen);
> strbuf << "INSERT INTO " << MY_TABLE << " (" << MY_FIELD
> << ")
> VALUES(\"" << mysqlpp::escape << fill << "\")" << ends;
>
> Thanks
> Ankur
>
Thread
Reading Writting BLOB VARBINARY Q.Ankur G35 Saxena24 Sep
  • Re: Reading Writting BLOB VARBINARY Q.Bill K24 Sep
  • Re: Reading Writting BLOB VARBINARY Q.Warren Young26 Sep
    • Re: Reading Writting BLOB VARBINARY Q.Warren Young26 Sep
    • Re: Reading Writting BLOB VARBINARY Q.Bill K26 Sep