mksqlite  2.5
A MATLAB interface to SQLite
Examples

Speed test

An example showing the speed improvements by using an in-memory database instead of an on-disc one. Also demonstrates, how to copy entire tables easily with one SELECT statement.

Parameter Binding

Demonstration of passing arguments to a sql statement through "parameter binding". So numbers don't have to be converted to strings to pass with an SQL statement. In case of BLOB datatypes, aruments are stored as byte arrays. With a screenshot this is shown in this example.
Introducing "cell extraction", function calls to mksqlite can extremely shortened.

Controlling access rights

Databases can be opened with different access rights. This example gives a short insight.

Controlling storage format of query results

There are 3 types of result for table queries:
"array of structs",
"struct of arrays" and
"(cell) matrix"
This example explains, how to get fetched results in either of these formats. Further mksqlite has two additional return parameters if needed, which introduced in this example.

Regular expressions

With mksqlite you have the ability of using regular expressions in queries. Here's not yet another explanation of regular expression, but a short overview, how to use with mksqlite and SQL databases.
Also you get an example how to scan recursively a path and store the results into a database to easy fetch summaries (or details) later. The algorithm is not an example doing it "right", "efficient" or "fast", it's only an example.

Typed BLOBs

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.

Wrapping bind parameters

When mksqlite is called with a statement containing bind parameters ('?', ':name', ...), you have to pass arguments with the exact count needed, or less. In latter case missing arguments are assumed to be NULL.
With the "parameter wrapping" switch (param_wrapping) set, you may pass more arguments than needed. Then the current statement will be processed again and again, until all arguments have been passed. In this mode, arguments for all parameters for each subsequent call must be given.

Named bound parameters using struct parameter

When mksqlite is called with a statement containing named bind parameters (':name' i.e.), you may pass a struct with fieldnames as used in the query. It values will be passed in the right place (no ordering is necessary).
With the "parameter wrapping" switch (param_wrapping) set, you may pass more arguments than needed. Then the current statement will be processed again and again, until all arguments have been passed. In this mode, arguments for all parameters for each subsequent call must be given. sql.m extends the capabilities of named parameters.

Compression (BLOB)

An example how to use data compression in SQL databases. This feature is only supported for typed BLOBs. mksqlite delivers lossy ('QLIN16' and 'QLOG16') and some lossless (BLOSC) compressors. This example demonstrates the lossless compressors only and calculates some statistics about time consumption and compression ratios.

Parameter Binding with compression

Another example of using compression.

MD5 hashing and packaging

mksqlite comes with some user defined functions. md5() computes the md5 hashing value, what is shown here.
Further some measurings for different compression levels are shown and the differences in use of lossy compression is shown.

Using language extension

A short example, how to use SQLites' language extension feature.

A real example (statistics over log file)

A logging file (text,csv) is read and pushed into a SQL table. Some optional columns are build by parsing (regex). Then some statistics can easily be made through SQL access.

A real example no usage of regular expressions (statistics over log file)

A logging file (text,csv) is read and pushed into a SQL table. Then some statistics can easily be made through SQL access.

Exception handling with mksqlite

Demonstration of catching exceptions with mksqlite.

Exception handling 64-bit integer types

MATLAB 64-bit integer type values are stored as SQL integer type. When refetching such values, mksqlite will prefer double type, except when the value is such huge, that it can't be lossless represented by a double.
This example shows these two conditions.

Application-defined functions

You can register your own MATLAB functions as SQL functions with one of the following calls:

mksqlite( 'create function', <name>, function_handle );
mksqlite( 'create aggregation', <name>, step_function_handle, final_function_handle );

So you can access your MATLAB code from within SQL queries.

Remarks
If you create application defined SQL functions with CreateFunction or CreateAggegation, mksqlite holds persistent variables for this database. Hence the behaviour of mksqlite (and sql_object) is conform to using persistent variables in MATLAB:
This object is not deleted (and this destructor not called) until these persistent variables are released.

The functions from the following list do remove them:
  • mksqlite( dbid, 'close' ) % (Removal only for databases defined by dbid!)
  • clear functions
  • clear mksqlite
  • clear all
  • clear persistent
  • Exiting MATLAB

Accessing databases via objects

If mksqlite is encapsulated in a class, you may deal with databases by objects.

db = sql_object( 'mydatabase.db' );
y = db.Select( '* FROM myTable' );
clear db