title: Read Binary Data in C++ and Write to MySQL
body: <p>I have an Insert function which opens image file and inserts to database.</p>
  
  <pre><code>void insertBlob()
  {
          sql::Driver *driver;
          sql::Connection *con;
          sql::PreparedStatement *pstmt;  
  
      ifstream file ("/home/malper/NetBeansProjects/BlobTesting/2", ios::in | ios::binary | ios::ate);
          ifstream::pos_type fileSize;
          char* fileContents;
  
          char buff[1024];
  
  
          if(file.is_open())
          {
              fileSize = file.tellg();
              fileContents = new char[fileSize];
              file.seekg(0, ios::beg);
              if(!file.read(fileContents, fileSize))
              {
                  cout &lt;&lt; "fail to read" &lt;&lt; endl;
              }
  
  
              file.close();
          }
          istringstream str(fileContents);
  
          /////////////////////////////////////////////////
  
          /* Create a connection */
          driver = get_driver_instance();
          con = driver-&gt;connect("tcp://192.168.2.5:3306", "root", "root");
  
          /* Connect to the MySQL test database */
          con-&gt;setSchema("test");
  
          pstmt=con-&gt;prepareStatement("INSERT INTO Dao VALUES(57,4,'2014-12-23 11:55:54',?,1,1)");
  
          pstmt-&gt;setBlob(1,&amp;str);
          pstmt-&gt;executeUpdate();
  
  }
  </code></pre>
  
  <p>Database part works well but i can't get binary image to stringstream after open file. How can i store binary in a string in this function?</p>