dbTalk Databases Forums  

Reading Writting BLOB VARBINARY Q.

mailing.database.mysql-plusplus mailing.database.mysql-plusplus


Discuss Reading Writting BLOB VARBINARY Q. in the mailing.database.mysql-plusplus forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
Ankur G35 Saxena
 
Posts: n/a

Default Reading Writting BLOB VARBINARY Q. - 09-23-2006 , 09:16 PM






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

--
MySQL++ Mailing List
For list archives: http://lists.mysql.com/plusplus
To unsubscribe: http://lists.mysql.com/plusplus?unsu...ie.nctu.edu.tw


Reply With Quote
  #2  
Old   
Bill K
 
Posts: n/a

Default Re: Reading Writting BLOB VARBINARY Q. - 09-23-2006 , 10:14 PM






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:
Quote:
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

--
MySQL++ Mailing List
For list archives: http://lists.mysql.com/plusplus
To unsubscribe: http://lists.mysql.com/plusplus?unsu...ie.nctu.edu.tw



Reply With Quote
  #3  
Old   
Warren Young
 
Posts: n/a

Default Re: Reading Writting BLOB VARBINARY Q. - 09-26-2006 , 02:34 AM



Ankur G35 Saxena wrote:
Quote:
Hi, I wanted to read and write a C++ structure to the DB.
Bad, bad idea. For this to be guaranteed to work, you could never
upgrade your compiler version or change compilers. You can't even
change some compiler options even if you were willing to stick with a
single compiler, because there are options that will change the binary
layout of your classes.

Do a Google search for object serialization in C++. There are ways to
get around this problem, many of which will solve the BLOB problem as a
side effect.

Or, switch to a language that understands things like serialization and
reflection. This is one of the things that C++ is just not good at.

--
MySQL++ Mailing List
For list archives: http://lists.mysql.com/plusplus
To unsubscribe: http://lists.mysql.com/plusplus?unsu...ie.nctu.edu.tw



Reply With Quote
  #4  
Old   
Warren Young
 
Posts: n/a

Default Re: Reading Writting BLOB VARBINARY Q. - 09-26-2006 , 02:46 AM



Warren Young wrote:
Quote:
Ankur G35 Saxena wrote:
Hi, I wanted to read and write a C++ structure to the DB.
One other thing: if you're not talking about objects and classes, but
rather plain old C style structures, then it's still a bad idea. C
structure binary data layouts are much more stable than for C++ classes,
but it's a bad idea for a different reason: you are effectively putting
multiple columns' worth of data into a single column. In DBA-speak,
your schema isn't properly normalized. You need to make a column for
each field in the structure.

Then you can use SSQLS, which is made exactly for this purpose.

--
MySQL++ Mailing List
For list archives: http://lists.mysql.com/plusplus
To unsubscribe: http://lists.mysql.com/plusplus?unsu...ie.nctu.edu.tw



Reply With Quote
  #5  
Old   
Bill K
 
Posts: n/a

Default Re: Reading Writting BLOB VARBINARY Q. - 09-26-2006 , 07:14 AM



Warren Young wrote:
Quote:
Ankur G35 Saxena wrote:
Hi, I wanted to read and write a C++ structure to the DB.

Bad, bad idea. For this to be guaranteed to work, you could never
upgrade your compiler version or change compilers. You can't even
change some compiler options even if you were willing to stick with a
single compiler, because there are options that will change the binary
layout of your classes.

Do a Google search for object serialization in C++. There are ways to
get around this problem, many of which will solve the BLOB problem as
a side effect.

I can recommend ICE. I have successfully used ICE by ZeroC to
serialize/de-serialize to/from MySql Blob fields using (interchangeably)
multiple C++ compilers and C#.
Quote:
Or, switch to a language that understands things like serialization
and reflection. This is one of the things that C++ is just not good at.

--
MySQL++ Mailing List
For list archives: http://lists.mysql.com/plusplus
To unsubscribe: http://lists.mysql.com/plusplus?unsu...ie.nctu.edu.tw



Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.