![]() |
mksqlite
2.5
A MATLAB interface to SQLite
|
SQLite is an embedded SQL Engine, which can be used to access SQL databases without a server. MKSQLITE offers an interface to this database engine.
Parameters dbid, rowcount and colnames are optional. dbid is only necessary if one is working with multiple databases at once. If dbid is left out, then database number 1 is used. On fetch queries rowcount will take the number of rows, colnames will take the table column names and row_id the corresponding rowid. If param_wrapping feature is set, row_id catches all ROWIDs in a row vector.
or
Opens the database with name "dataBaseName". If the database does not exist, an empty one will be created. If a dbid is given that is already open, it will be closed before opening again. Providing a dbid of 0 will return the next free dbid.
or
or
Closes the database file. If a dbid is provided, the corresponding database is closed. For dbid 0, all open databases are closed.
or
Returns the version of mksqlite in the output (1), or as a string (2).
or
Returns the version of the SQLite Engine in the output (1), or as a string (2).
or
Carries out the given "SQL-Command"
Example:
Reads all fields from table "testtable" from the database "testdb.db3" into the result
Example:
Shows all tables in the database "testdb.db3"
The SQL syntax allows the use of parameters, which are identified by placeholders, and then filled with contents by trailing arguments.
Allowed placeholders in SQLlite are: ?, ?NNN, :NAME, $NAME, @NAME A placeholder can only stand for one value, not for a command, split-name, or table, etc.
Example:
Instead of a listing of arguments, a cell array or a struct array may be provided that contains the arguments.
If fewer arguments are given then required, the remaining parameters are filled with NULLs. If more arguments are given than necessary, the function reports an error.
If it is intended, that implicit calls with the same command and the remaining arguments shall be done, so called parameter wrapping must be activated:
Examples:
An argument may be a real value (scalar or array) or a string. Non-scalar values are treated as a BLOB (unit8) SQL datatype.
( BLOB = (B)inary (L)arge (OB)ject) )
Example:
BLOBs are always stored as a vector of uint8 values in the database. In order to retrieve the original format (for example, double) and dimensions of the matrix, explicit typecast() and reshape() functions must be used. (Refer to the examples Parameter Binding and Named bound parameters using struct parameter)
Optionally this information (type) can be stored after the BLOB. The indicated post-processing is then no longer necessary, but the database is then no longer compatible with other software!
The typecasting conversion can be activated/deactivated with:
(see also the example Typed BLOBs)
Type conversion only works with numeric arrays and vectors. structs, cell arrays and complex data must be converted beforehand. Matlab can do this conversion through undocumented functions:
getByteStreamFromArray() and getArrayFromByteStream().
This functionality is activated by following command:
The data in a BLOB is stored either uncompressed (standard) or compressed. Automatic compression of the data is only necessary for typed BLOBs, but must be activated:
(See also examples Parameter Binding with compression and MD5 hashing and packaging)
The compression uses BLOSC (http://blosc.pytabales.org/trac) After compression, the data is unpacked and compared with the original. If there is a difference, an error report is given. If this functionality is not desirable, it can be deactivated (data is stored without verification).
Compatibility:
Stored compressed blobs cannot be retrieved with older versions of mksqlite, this will trigger an error report. In contrast, uncompressed BLOBS can be retrieved with older versions. Of course BLOBs stored with older versions can be retrieved with this version.
Remarks on compression rate:
The achievable compression rates depend strongly on the contents of the variables. Although BLOSC is equipped to handle numeric data, its performance on randomized numbers (double) is poor (~95). If there are many identical values, for example from quantization, the compression rate is markedly improved.
Further compression methods:
"QLIN16":
QLIN16 is a lossy compression method. The data is linearly discretize to 65529 steps and stored as 16-bit values. Zero, as well as Infinity and Nan can also be used, as they are stored as special values (65529...65535). Differing compression rates are not supported, so this compressor should always be set to 1.
"QLOG16":
Works like QLIN16, except that the quantization uses logarithmic scaling, therefore storage of negative values is not allowed, but NULL, Nan, and infinity are still accepted. Similarly, differing compression rates are not supported, so should always be set to 1.
Beside the described calling convention, one can retrieve two further often needed results:
Both results are given with the common call already:
Per default an array of structs will be returned for table queries. You can decide between three different kinds of result types:
You can change the default setting (n=0) with following call:
(see Controlling storage format of query results)
mksqlite offers additional SQL functions besides the known "core functions" like replace, trim, abs, round, ... This version offers 10 additional functions:
Example:
Note that in contrast to sqlite only one implementation per function name is allowed in mkslite. Defining a new implemention of one function discards the previous one.
(also see Regular expressions for further examples...)
You can register your own MATLAB functions as SQL functions with one of the following calls:
So you can access your MATLAB code from within SQL queries.
(see Application-defined functions for examples...)
Command | Description | Valid parameter(s) | Default value(s) |
---|---|---|---|
'version mex' | Returns the version of mksqlite | - | - |
'version sql' | Returns the version of SQLite | - | - |
'open' | Opens a database | Database (file-)name | - |
'close' | Closes one or all database(s) | - | - |
'filename' | Returns the filename of the database | 0|1 The argument names the database the filename ist queried for. If no argument is passed the filename for the main database is returned. | - |
'check4uniquefields' | Avoids duplicate field names in MATLAB structs, when set to 1 | 0|1 | 1 |
'convertUTF8' | Enables UTF8 text encoding, when set to 1 | 0|1 | 1 |
'typedBLOBs' | Enables typed BLOB packaging, when set >0
| 0|1|2 | 0 |
'NULLasNaN' | mksqlite returns NULL values as NaN, when set to 1 | 0|1 | 0 |
'param_wrapping' | Enables parameter wrapping, when set to 1 Example | 0|1 | 0 |
'streaming' | Returns 1, when serializing is enabled | - | - |
'result_type' | Chooses the result type of sql queries.
| 0|1|2 | 0 |
'compression' | Set compressor when using typedBLOBS=2 Example | "blosc"|"blosclz"|"qlin16"|"qlog16", 0-9 | "blosclz",0 |
'compression_check' | Enables compressor check, when set to 1. The default decompresses immediately prior to packed data to ensure by comparing the data accuracy | 0|1 | 0 |
'show tables' | Display content of the sqlite_master. That is the column definitions of tables, views and indexes | - | - |
'enable extension' | Enable extension loading at runtime Example | 0|1 | 0 |
'status' | Show status of each mksqlite slot in command window. Different slots are accessed trough its dbids. | - | - |
'setbusytimeout' | Sets the SQLite busy timeout. A SQL statement whose calculation exceeds this time limit leads into failure abort. | time in ms | 1000 |
(c) 2017 by Martin Kortmann mail@, Andreas Martin kort mann. deandim arti n@use rs.s ource forg e.net