Code covered by the BSD License  

Highlights from
Myblob

Myblob

by

 

07 Jan 2006 (Updated )

Exchange blobs (binary large objects) with MySQL databases

myblob_demo.m
%MYBLOB_DEMO  Demonstrate the use of the MYBLOB tool kit
%
%   MYBLOB_OPEN    : Open the connection to a MySQL database 
%   MYBLOB_COMMAND : Send an SQL statement to a MySQL database 
%   MYBLOB_TO_DB   : Send a blob (binary large object) to a MySQL database
%   MYBLOB_FROM_DB : Retrieve a blob (binary large object) from a MySQL database
%   MYBLOB_CLOSE   : Close the connection to a MySQL database
%
%   The blob code is mainly based on the very instructive paper:
%   Accessing MySQL BLOB columns using Visual Basic 6 by Mike Hillyer
%   http://dev.mysql.com/tech-resources/articles/vb-blob-handling.html
%   
%   See also: myblob_open, myblob_command, myblob_from_db, myblob_to_db, myblob_close.

%   Joerg J. Buchholz, Hochschule Bremen, buchholz@hs-bremen.de
%
%   Version 1.0 (2006-01-07)

% Insert your own mySQL database parameters here:
user = 'xxxxxxxxx';
password = 'xxxxxxxxx';
server = 'xxx.xxx.xxx';
database = 'xxxxxxxxx';

% Open connection to MySQL database
connection = myblob_open (user, password, server, database);

% Drop the demo table if it exists
command = 'DROP TABLE IF EXISTS myblob_demo_table';
myblob_command (connection, command);

% Create a new table with a string column and a blob column
command = 'CREATE TABLE myblob_demo_table (string_column VARCHAR(64), blob_column BLOB)';
myblob_command (connection, command);

% Insert a new row with a description in the string column
% and leave the blob column empty
command = 'INSERT INTO myblob_demo_table (string_column) VALUES ("Matlab logo")';
myblob_command (connection, command);

% Retrieve the whole table into one "big" cell array.
% Note the empty/NULL/NaN blob cell
command = 'SELECT * FROM myblob_demo_table';
table_as_cell_array = myblob_command (connection, command)

% Read a blob (the Matlab logo) from a JPG-file and send it to the database; 
% Therefore define:
% the table where the blob will be stored, 
% the column where the blob will be stored,
% the row where the column will be stored, and 
% the file where the blob comes from.
% Send the file/JPG/blob to the database.  
% (You can now look at the Matlab logo in your MySQL query browser.)
table = 'myblob_demo_table';
column = 'blob_column';
where = 'WHERE string_column = "Matlab logo"';
file = 'matlab.jpg';
myblob_to_db (connection, table, column, where, file);

% Retrieve the blob (the Matlab logo) from the database and display it in Matlab;
% Therefore:
% Ask Matlab for a temporary file to buffer the blob.
% Retrieve the blob from the database and buffer it in the temporary file.
% Read the temporary file and display it's content as an image.
file1 = tempname;
myblob_from_db (connection, table, column, where, file1);
imagesc (imread (file1));

% Insert a new row with a description in the string column
% and leave the blob column empty
command = 'INSERT INTO myblob_demo_table (string_column) VALUES ("Binary MAT-file")';
myblob_command (connection, command);

% Send a Matlab object to the database;
% Therefore:
% Generate a Matlab matrix.
% Ask Matlab for a temporary MAT-file to buffer the matrix.
% Save the matrix to the MAT-file using Matlab's SAVE command.
% Define where (table, column, row, ...) the MAT-file/matrix/blob will be stored.
% Send the blob to the database.  
a = [1 2; 3 4];
file2 = [tempname, '.mat'];
save (file2, 'a');
where = 'WHERE string_column = "Binary MAT-file"';
myblob_to_db (connection, table, column, where, file2);

% Retrieve the matrix from the database back into MATLAB;
% Therefore:
% Delete the matrix from the Matlab workspace.
% Ask Matlab for a temporary MAT-file to buffer the blob.
% Retrieve the blob from the database and buffer it in the temporary file.
% Read the temporary file with Matlab's LOAD command.
% Verify that the "Matrix is Reloaded"...
clear a;
file3 = [tempname, '.mat'];
myblob_from_db (connection, table, column, where, file3);
load (file3);
a

% Close connection to mySQL database
% Don't forget to do this... ;-)
myblob_close (connection);

Contact us