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_to_db (ado_connection, table, column, where, file)
function myblob_to_db (ado_connection, table, column, where, file)
%MYBLOB_TO_DB  Send a blob (binary large object) to a MySQL database
%       sends the blob (JPG-file, MAT-file, ...) from the file
%       defined by the file name string FILE to the MySQL database. 
%       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_from_db, myblob_close.

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

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

% SELECT a row (set) of the specified table
% (including the blob column)
% using the specified WHERE clause
command_text = ['SELECT * 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');

% Client-side cursors can only be static
ado_recordset.CursorType = 'adOpenStatic';

% The lock type must not be read-only,
% since we want to stream (write) to the record set 
ado_recordset.LockType = 'adLockOptimistic';

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

% Instantiate ADO stream object
ado_stream = actxserver ('ADODB.Stream');

% The stream transports binary data
ado_stream.Type = 'adTypeBinary';

% Open the stream object

% Load the file content into the stream object
ado_stream.LoadFromFile (file);

% Use the stream's read method to 
% populate the specified field of the current record with the blob 
% (i. e. the file content)
% (Implement a loop here, if you want to write the file to all selected records) 
ado_recordset.Fields.Item(column).Value = ado_stream.Read;

% Update the record set
% (write the blob to the data base)
% Close the stream object

% Close record set object

Contact us