Example: Use the BLOB data type

This is an example of using DB2® for IBM® i BLOB data type with ODBC.

The following is a partial C program that uses the BLOB data type:
BOOL params = TRUE; // TRUE if you want to use parameter markers
SQLINTEGER char_len = 10, blob_len = 400;
SQLCHAR szCol1[21], szCol2[400], szRecCol1[21], szRecCol2[400];
SQLINTEGER cbCol1, cbCol2;
SQLCHAR stmt[2048];

// Create a table with a character column and a BLOB column
rc = SQLExecDirect(hstmt, "CREATE TABLE TABBLOB(COL1 CHAR(10), COL2 BLOB(400))", SQL_NTS); 

strcpy(szCol1, "1234567890");  
if (!params)  // no parameter markers
{
  strcpy(szCol2, "414243444546"); // 0x41 = 'A', 0x42 = 'B', 0x43 = 'C', ...
  wsprintf(stmt, "INSERT INTO TABBLOB VALUES('%s', BLOB(x'%s'))", szCol1, szCol2);
}
else
{
  strcpy(szCol2, "ABCDEF"); // 'A' = 0x41, 'B' = 0x42, 'C' = 0x43, ...        
  strcpy(stmt, "INSERT INTO TABBLOB VALUES(?,?)");
}
  
// Prepare the 'Insert' statement
rc = SQLPrepare(hstmt, stmt, SQL_NTS);  
  
// Bind the parameter markers 
if (params)  // using parameter markers 
{
  cbCol1 = char_len;
  rc = SQLBindParameter(hstmt, 1, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_CHAR, 
                                        char_len, 0, szCol1, char_len + 1, &cbCol1);
    
  cbCol2 = 6;                 
  rc = SQLBindParameter(hstmt, 2, SQL_PARAM_INPUT, SQL_C_BINARY, SQL_LONGVARBINARY, 
                                        blob_len, 0, szCol2, blob_len, &cbCol2);
}
  
// Execute the 'Insert' statement to put a row of data into the table
rc = SQLExecute(hstmt);  
  
// Prepare and Execute a 'Select' statement
rc = SQLExecDirect(hstmt, "SELECT * FROM TABBLOB", SQL_NTS);
  
// Bind the columns
rc = SQLBindCol(hstmt, 1, SQL_C_CHAR, szRecCol1, char_len + 1, &cbCol1);                                                       
rc = SQLBindCol(hstmt, 2, SQL_C_BINARY, szRecCol2, blob_len, &cbCol2); 
  
// Fetch the first row
rc = SQLFetch(hstmt);
szRecCol2[cbCol2] = '\0';
  
// At this point szRecCol1 should contain the data "1234567890"  
// szRecCol2 should contain the data 0x414243444546 or "ABCDEF"