mksqlite  2.5
A MATLAB interface to SQLite
Typed BLOBs
sqlite_test_bind_typed

Contents

function sqlite_test_bind_typed
    clear all
    close all
    clc
    dummy = mksqlite('version mex');
    fprintf( '\n\n' );
mksqlite Version 2.5 build: 133, ein MATLAB Interface zu SQLite
(c) 2008-2017 by Martin Kortmann <mail@kortmann.de>
                 Andreas Martin  <andimartin@users.sourceforge.net>
basierend auf SQLite Version 3.16.2 - http://www.sqlite.org
mksqlite verwendet:
 - DEELX perl kompatible regex engine Version 1.3 (Sswater@gmail.com)
 - BLOSC/LZ4 1.3.0-rc3.dev zur Datenkompression (Francesc Alted / Yann Collett) 
 - MD5 Message-Digest Algorithm (RFC 1321) Implementierung von Alexander Peslyak
   
Platform: PCWIN64, little endian



Create an in-memory database

    mksqlite( 'open', ':memory:' );
    mksqlite( 'param_wrapping', 0 );

    % mksqlite stores numeric arrays always as an array of bytes (uint8),
    % since SQLite only supports native byte array (BLOB). When you fetch
    % a BLOB you will always get your result as byte array of type uint8.
    % With typed BLOBs mksqlite stores additionally informations (dimensions and
    % numeric type), so it can be returned in the same format as it was before.
    % No casting or reshaping will be necessary.

    mksqlite( 'typedBLOBs', 1 );

    %          |First name |Last name    |City         |Random data
    mydata = { ...
               'Gunther',  'Meyer',      'Munich',     []; ...
               'Holger',   'Michelmann', 'Garbsen',    rand( 1, 10 ); ...
               'Knuth',    'Almeroth',   'Wehnsen',    'coworker' ...
             };

    % create table
    mksqlite( 'CREATE TABLE demo (Col_1, Col_2, Col_3, Data)' );

    % create records
    % uses MATLAB "cell expansion" for command
    % shortening: mydata{i,:} expands to 4 arguments
    for i = 1:size( mydata, 1 )
        mksqlite( 'INSERT INTO demo VALUES (?,?,?,?)', mydata{i,:} );
    end

Take a screenshot (figure) as RGB-matrix...

    h = figure;
    set( h, 'units', 'normalized', 'position', [0.5,0.5,0.2,0.2] );
    x = linspace( 0, 2*pi, 20 );
    plot( x, sin(x), 'r-', 'linewidth', 2 );
    legend on
    F = getframe(h);
    delete(h);
    data = F.cdata;

Store using typed BLOB feature

    mksqlite( 'INSERT INTO demo (Data) VALUES (?)', data );

    try
        % enable advanced typed BLOBs. So now structures cell arrays
        % and complex numbers can be stored as BLOB in the database.
        mksqlite( 'typedBLOBs', 2 );
    catch
        % If your MATLAB version doesn't support streaming (serialization)
        % of variables, mksqlite will report an error. mksqlite uses mode 1
        % then. You can read the current mode with:
        %   mode = mksqlite( 'typedBLOBs' );
        % Discarding the fail, because this will be checked next...
    end

    % If your MATLAB version doesn't support streaming, then skip writing
    % structures and complex numbers (nested variables)...
    % (mksqlite( 'typedBLOBs' ) will be 1 then)
    if mksqlite( 'typedBLOBs' ) == 2
        data_complex = struct;
        data_complex.String = 'Name';
        data_complex.Complex = 3+4i;
        data_complex.Cell = { 1:10, 'Text', 1+2i };

        mksqlite( 'INSERT INTO demo (Data) VALUES (?)', data_complex );
    end

Now read back values

    clc
    fprintf( 'Get BLOB with the original data types from the database...\n\n' )

    query = mksqlite( 'SELECT * FROM demo' );

    fprintf( '---> Empty array: ' ), ...
             query(1).Data

    fprintf( '---> 10 random numbers between 0 and 1: ' ), ...
             query(2).Data

    fprintf( '---> Text: ' ), ...
             query(3).Data

    fprintf('---> Image: (see figure) \n\n')

    % Due to typed BLOBs the image data haven't to be reshaped or casted.
    img = query(4).Data;

    h = image( img );
    axis off
    set( gcf, 'units', 'normalized', 'position', [0.5,0.5,0.2,0.2] );
    drawnow

    % bring recent figure to top
    try
        warning( 'off', 'MATLAB:HandleGraphics:ObsoletedProperty:JavaFrame' );
        jh = get( h, 'JavaFrame' );
        jh.fFigureClient.getWindow.setAlwaysOnTop( true );
        jh.fFigureClient.getWindow.setVisible( true );
    catch
    end

    % if streaming is supported, fetch the nested variable
    if mksqlite( 'typedBLOBs' ) == 2
        fprintf( '---> Nested variable: ' );
        query(5).Data
    else
        fprintf( ['\nThis MATLAB version doesn''t support serialization.\n', ...
                  'The test of handling nested variables will be skipped.\n'] );
    end

    mksqlite( 'close' );
Get BLOB with the original data types from the database...

---> Empty array: 
ans =

     []

---> 10 random numbers between 0 and 1: 
ans =

  Columns 1 through 5

   0.652205269883068   0.481757451678895   0.382593638230219   0.326885840206537   0.103039917466465

  Columns 6 through 10

   0.370412379306934   0.377441539597471   0.481798320776792   0.795619562877393   0.691393696170815

---> Text: 
ans =

coworker

---> Image: (see figure) 

---> Nested variable: 
ans = 

     String: 'Name'
    Complex: 3.000000000000000 + 4.000000000000000i
       Cell: {[1 2 3 4 5 6 7 8 9 10]  'Text'  [1.000000000000000 + 2.000000000000000i]}