title: How to get a MySQL blob as binary data using SQLGetData in C++
body: <p>I have a MySQL database with a <code>mediumblob</code> column. I want to get binary data from the blob field into a C++ stringstream using ODBC on Windows.</p>
  
  <p>I first call <code>SQLDescribeCol</code> and it indicates that it is of type <code>SQL_LONGVARBINARY</code>.</p>
  
  <p>I then  make a call to <code>SQLGetData</code> as follows:</p>
  
  <pre><code>SQLLEN indicator;
  SQLCHAR SqlChar[8000];
  SQLGetData(m_sqlstatementhandle, i, SQL_CHAR, &amp;SqlChar, sizeof(SqlChar), (SQLLEN*)&amp;indicator);
  </code></pre>
  
  <p>I then go on and write the data into a <code>stringstream</code>:</p>
  
  <pre><code>stringstream ss;
  ss.write((char*)&amp;SqlChar, indicator);
  </code></pre>
  
  <p>This does give me the blob data, but it is stored in <code>SqlChar</code> as a HEX string.</p>
  
  <p>My program expects the data in the stringstream to be stored as binary. Now I could convert the HEX string to binary first and then write it to the stringstream, but that doesn't feel right to me. I would really like to get it as binary straight away from <code>SQLGetData</code>.</p>
  
  <p>So a couple of questions:</p>
  
  <ol>
  <li>Am I using <code>SQLGetData</code> correctly for a blob type?</li>
  <li>Is a <code>SQLCHAR</code> array the right container to write into?</li>
  <li>Is there a way to get the data as binary directly from MySQL via ODBC?</li>
  </ol>
  
  <p>Thanks.</p>