title: serializing objects in C++ and storing as a blob type in mysql
body: <p>I am using mysql/C++ connector to connect to a mysql database. I have some complex data structures so I need to serialize those and save in the database. </p>
  
  <p>I tried something like the following.</p>
  
  <pre><code>vector&lt;int&gt; vectorTest(10,100);
  istream *blob = NULL;
  ostringstream os;
  int size_data = sizeof(vector&lt;int&gt;);
  
  blob = new istringstream((char*)&amp;vectorTest, istringstream::in | istringstream::binary);
  string qry = "INSERT INTO vector(id,object) VALUES (?,?)";
  
  prep_stmt = con-&gt;prepareStatement(qry);
  
  prep_stmt-&gt;setInt(1,1);
  prep_stmt-&gt;setBlob(2,blob);
  prep_stmt-&gt;execute();
  </code></pre>
  
  <p>I just tried a small example here. However the vector object is not getting saved. </p>
  
  <p>Alternatively can I can use the following approach.</p>
  
  <pre><code>ostringstream os;
  int size_data = sizeof(vector&lt;int&gt;);
  os.write((char*)&amp;vectorTest, size_data);
  </code></pre>
  
  <p>However I don't know how to redirect the outputstream to an inputstream, because the setBlob() method needs an istream as the input parameter.</p>
  
  <p>Can I know how to get any of this examples working ? If my approach is incorrect can anyone provide a code example or improve the given code segment ? Your immediate response is greatly appreciated.</p>
  
  <p>Thanks</p>