Code covered by the BSD License  

Highlights from



Joerg Buchholz (view profile)


07 Jan 2006 (Updated )

Exchange blobs (binary large objects) with MySQL databases

myblob_from_db (ado_connection, table, column, where, file)
function myblob_from_db (ado_connection, table, column, where, file)
%MYBLOB_FROM_DB  Retrieve a blob (binary large object) from a MySQL database
%       retrieves a blob from the MySQL database
%       and saves it in a file (JPG-file, MAT-file, ...)
%       defined by the file name string FILE.
%       CONNECTION is the handle to the ADODB connection object;
%       it is returned by a previous call to MYBLOB_OPEN. 
%       The strings TABLE, COLUMN, and WHERE define the location of the blob in
%       the database. WHERE is a usual SQL WHERE clause, defining one table row.
%   Examples: 
%       myblob_demo
%   See also: myblob_demo, myblob_open, myblob_command, myblob_to_db, myblob_close.

%   Joerg J. Buchholz, Hochschule Bremen,
%   Version 1.0 (2006-01-07)

% Instantiate ADO command object
ado_command = actxserver ('ADODB.Command');

% SELECT the blob column in the specified table
% using the specified WHERE clause
command_text = ['SELECT ', column, ' FROM ', table, ' ', where];

% Communicate the sql command string to the command object
ado_command.CommandText = command_text;

% Communicate the connection object to the command object
ado_command.ActiveConnection = ado_connection;

% Instantiate ADO record set object
ado_recordset = actxserver ('ADODB.Recordset');

% Communicate the command object to the record set object
% and open the record set object
ado_recordset.Open (ado_command);

% Open the specified file for writing
file_handle = fopen (file, 'w');

% Write the blob 
% (i. e. the content of the specified field of the current record) to the file
% (If the SELECT statement returns more than one record, 
% only blob in the first record is written.
% Implement a loop here, if you want to write all selected records) 
fwrite (file_handle, ado_recordset.Fields.Item(column).Value);

% Close the file
fclose (file_handle);

% Close record set object

Contact us