<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>JDBC &#8211; Undocumented Matlab</title>
	<atom:link href="https://undocumentedmatlab.com/articles/tag/jdbc/feed" rel="self" type="application/rss+xml" />
	<link>https://undocumentedmatlab.com</link>
	<description>Professional Matlab consulting, development and training</description>
	<lastBuildDate>Wed, 27 Dec 2017 21:53:54 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.7.2</generator>
	<item>
		<title>Using SQLite in Matlab</title>
		<link>https://undocumentedmatlab.com/articles/using-sqlite-in-matlab?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=using-sqlite-in-matlab</link>
					<comments>https://undocumentedmatlab.com/articles/using-sqlite-in-matlab#comments</comments>
		
		<dc:creator><![CDATA[Yair Altman]]></dc:creator>
		<pubDate>Wed, 27 Dec 2017 21:53:54 +0000</pubDate>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[Low risk of breaking in future versions]]></category>
		<category><![CDATA[Undocumented feature]]></category>
		<category><![CDATA[Database]]></category>
		<category><![CDATA[JDBC]]></category>
		<category><![CDATA[Performance]]></category>
		<guid isPermaLink="false">http://undocumentedmatlab.com/?p=7255</guid>

					<description><![CDATA[<p>SQLite databases can be accessed in a variety of different ways in Matlab. </p>
<p>The post <a rel="nofollow" href="https://undocumentedmatlab.com/articles/using-sqlite-in-matlab">Using SQLite in Matlab</a> appeared first on <a rel="nofollow" href="https://undocumentedmatlab.com">Undocumented Matlab</a>.</p>
<div class='yarpp-related-rss'>
<h3>Related posts:</h3><ol>
<li><a href="https://undocumentedmatlab.com/articles/secure-ssl-connection-between-matlab-and-postgresql" rel="bookmark" title="Secure SSL connection between Matlab and PostgreSQL">Secure SSL connection between Matlab and PostgreSQL </a> <small>It is tricky, but quite possible, to use SSL to connect Matlab to a PostgreSQL database. ...</small></li>
<li><a href="https://undocumentedmatlab.com/articles/speeding-up-matlab-jdbc-sql-queries" rel="bookmark" title="Speeding up Matlab-JDBC SQL queries">Speeding up Matlab-JDBC SQL queries </a> <small>Fetching SQL ResultSet data from JDBC into Matlab can be made significantly faster. ...</small></li>
<li><a href="https://undocumentedmatlab.com/articles/matlab-toolstrip-part-5-icons" rel="bookmark" title="Matlab toolstrip – part 5 (icons)">Matlab toolstrip – part 5 (icons) </a> <small>Icons can be specified in various ways for toolstrip controls and the app window itself. ...</small></li>
<li><a href="https://undocumentedmatlab.com/articles/sending-html-emails-from-matlab" rel="bookmark" title="Sending HTML emails from Matlab">Sending HTML emails from Matlab </a> <small>Matlab's sendmail only sends simple text messages by default; a simple hack can cause it to send HTML-formatted messages. ...</small></li>
</ol>
</div>
]]></description>
										<content:encoded><![CDATA[<p>MathWorks invests a huge amount of effort in recent years on supporting large distributed databases. The business case for this focus is entirely understandable, but many Matlab users have much simpler needs, which are often served by the light-weight open-source <a href="http://sqlite.org" rel="nofollow" target="_blank">SQLite database</a> (which claims to be the most widely-used database worldwide). Although SQLite is very widely used, and despite the fact that built-in support for SQLite is included in Matlab (for its internal use), MathWorks has chosen not to expose any functionality or wrapper function that would enable end-users to access it. In any case, I recently came across a need to do just that, when a consulting client asked me to create an interactive data-browser for their SQLite database that would integrate with their Matlab program:<br />
<center><img decoding="async" src="https://undocumentedmatlab.com/images/DataBrowser_2.png" alt="SQLite data browser" title="SQLite data browser" width="80%" style="max-width:716px;" /></center><br />
In today&#8217;s post I will discuss several possible mechanisms to integrate SQLite in Matlab code, and you can take your pick among them. Except for the Database Toolbox, all the alternatives are free (open-source) libraries (even the commercial Database Toolbox relies on one of the open-source libraries, by the way).<br />
<span id="more-7255"></span></p>
<h3 id="sqlite4java">sqlite4java</h3>
<p><code>sqlite4java</code> is a Java package by <a href="http://almworks.com" rel="nofollow" target="_blank">ALM Works</a> that is bundled with Matlab for the past several years (in the <i>%matlabroot%/java/jarext/sqlite4java/</i> folder). This is a lightweight open-source package that provides a minimalist and fast (although not very convenient) interface to SQLite. You can either use the package that comes with your Matlab installation, or download and use the latest version from <a href="https://bitbucket.org/almworks/sqlite4java" rel="nofollow" target="_blank">the project repository</a>, where you can also find documentation.<br />
Mark Mikofski <a href="http://poquitopicante.blogspot.co.il/2015/03/sqlite-in-matlab.html" rel="nofollow" target="_blank">exposed</a> this hidden nugget back in 2015, and you are welcome to view his post for additional details. Here&#8217;s a sample usage:</p>
<pre lang="matlab">
% Open the DB data file
db = com.almworks.sqlite4java.SQLiteConnection(java.io.File('C:\Yair\Data\IGdb 2017-11-13.sqlite'));
db.open;
% Prepare an SQL query statement
stmt = db.prepare(['select * from data_table where ' conditionStr]);
% Step through the result set rows
row = 1;
while stmt.step
   numericValues(row) = stmt.columnInt(0);    % column #0
   stringValues{row}  = stmt.columnString(1); % column #1
end
% Cleanup
stmt.dispose
db.dispose
</pre>
<p>Note that since <code>sqlite4java</code> uses a proprietary interface (similar, but not identical, to JDBC), it can take a bit of time to get used to it. I am generally a big fan of preferring built-in components over externally-installed ones, but in this particular case I prefer other alternatives.</p>
<h3 id="JDBC">JDBC</h3>
<p><a href="http://www.oracle.com/technetwork/java/javase/jdbc/index.html" rel="nofollow" target="_blank">JDBC</a> (Java Database Connectivity) is the industry standard for connectivity to databases. Practically all databases nowadays have at least one JDBC connector, and many DBs have multiple JDBC drivers created by different groups. As long as they all adhere to the JDBC interface standard, these drivers are all equivalent and you can choose between them based on availability, cost, support, license, performance and other similar factors. SQLite is no exception to this rule, and has several JDBC driver implementations, including xerial&#8217;s <a href="https://bitbucket.org/xerial/sqlite-jdbc" rel="nofollow" target="_blank"><code>sqlite-jdbc</code></a> (also <a href="http://poquitopicante.blogspot.co.il/2015/03/sqlite-in-matlab.html" rel="nofollow" target="_blank">discussed by Mark Mikofski</a>) and <a href="https://storage.googleapis.com/google-code-archive-downloads/v2/code.google.com/sqlitebot/sqlitejdbc-v056.jar" rel="nofollow" target="_blank"><code>sqlitejdbc</code></a>. If you ask me,<br />
 <code>sqlite-jdbc</code> is better as it is being maintained with new versions released periodically.<br />
The example above would look something like this with <code>sqlite-jdbc</code>:</p>
<pre lang="matlab">
% Add the downloaded JAR library file to the dynamic Java classpath
javaaddpath('C:\path\to\sqlite\sqlite-jdbc-3.21.0.jar')
% Open the DB file
jdbc = org.sqlite.JDBC;
props = java.util.Properties;
conn = jdbc.createConnection('jdbc:sqlite:C:\Yair\Data\IGdb 2017-11-13.sqlite',props);  % org.sqlite.SQLiteConnection object
% Prepare and run an SQL query statement
sqlStr = ['select * from data_table where ' conditionStr];
stmt = conn.createStatement;     % org.sqlite.jdbc4.JDBC4Statement object
rs = stmt.executeQuery(sqlStr);  % org.sqlite.jdbc4.JDBC4ResultSet object
% Step through the result set rows
rows = 1;
while rs.next
   numericValues(row) = rs.getLong('ID');
   stringValues{row}  = rs.getString('Name');
end
% Cleanup
rs.close
stmt.close
conn.close
</pre>
<h3 id="toolbox">Database toolbox</h3>
<p>In addition to all the above, MathWorks sells the Database Toolbox which has an <a href="https://www.mathworks.com/help/database/ug/working-with-the-matlab-interface-to-sqlite.html" rel="nofollow" target="_blank">integral SQLite connector</a>, in two flavors &#8211; native and JDBC (the JDBC connector is simply <code>sqlite-jdbc</code> that I mentioned above, see a <a href="https://gist.github.com/cbcunc/e2bc3ef170544e4bf0f0" rel="nofollow" target="_blank">short discussion here</a>).<br />
I assume that the availability of this feature in the DB toolbox is the reason why MathWorks has never created a documented wrapper function for the bundled <code>sqlite4java</code>. I could certainly understand this from a business perspective. Still, with so many free alternatives available as discussed in this post, I see not reason to purchase the toolbox merely for its SQLite connector. Then again, if you need to connect to several different database types, not just SQLite, then getting the toolbox might make sense.</p>
<h3 id="mksqlite">mksqlite</h3>
<p>My personal favorite is actually none of these Java-based connectors (surprise, surprise), but rather the open-source <a href="http://mksqlite.sourceforge.net" rel="nofollow" target="_blank"><code>mksqlite</code> connector</a> by <a href="http://www.kortmann.de" rel="nofollow" target="_blank">Martin Kortmann</a> and <a href="https://github.com/AndreasMartin72" rel="nofollow" target="_blank">Andreas Martin</a>. This is a native (Mex-file) connector that acts as a direct Matlab function. The syntax is pretty straight-forward and supports SQL queries. IMHO, its usage is a much simpler than with any of the other alternatives:</p>
<pre lang="matlab">
% Open the DB file
mksqlite('open', 'C:\Yair\Data\IGdb 2017-11-13.sqlite');
% Query the database
results = mksqlite(['select * from data_table where ' conditionStr]);
numericValues = [results.ID];
stringValues  = {results.Name};
% Cleanup
mksqlite('close');
</pre>
<p>Can it be any simpler than this!?<br />
However, the main benefit of <code>mksqlite</code> over the other connectors is not its simplicity but the connector&#8217;s speed. This speed is due to the fact that the query is vectorized and we do not need to loop over all the separate data rows and fields. With the other connectors, it is actually not the loop that takes so long in Matlab, but rather the overheads and inefficiencies of numerous library calls to fetch one single value at a time from the result-set &#8211; this is avoided in <code>mksqlite</code> where there is only a single call. This results in lightning speed: A couple of years ago I consulted to a client who used a JDBC connector to an SQLite database; by switching from a JDBC connector to <code>mksqlite</code>, I reduced the execution time from 7 secs to 70 msecs &#8211; a 100x speedup! In that specific case, this made the difference between an unusable program and a highly interactive/responsive one.</p>
<h3 id="alternatives">Other alternatives</h3>
<p>In addition to all the above, we can also use a <a href="http://www.codingtricks.biz/working-with-sqlite-database-in-matlab/" rel="nofollow" target="_blank">.NET-based connector</a> or a <a href="https://www.pythoncentral.io/introduction-to-sqlite-in-python/" rel="nofollow" target="_blank">Python one</a> &#8211; I leave these as an exercise for the reader&#8230;<br />
Have I forgotten some important alternative? Or perhaps you have have some related tip you&#8217;d like to share? If so, then please leave a comment below.<br />
Happy New Year everybody!</p>
<p>The post <a rel="nofollow" href="https://undocumentedmatlab.com/articles/using-sqlite-in-matlab">Using SQLite in Matlab</a> appeared first on <a rel="nofollow" href="https://undocumentedmatlab.com">Undocumented Matlab</a>.</p>
<div class='yarpp-related-rss'>
<h3>Related posts:</h3><ol>
<li><a href="https://undocumentedmatlab.com/articles/secure-ssl-connection-between-matlab-and-postgresql" rel="bookmark" title="Secure SSL connection between Matlab and PostgreSQL">Secure SSL connection between Matlab and PostgreSQL </a> <small>It is tricky, but quite possible, to use SSL to connect Matlab to a PostgreSQL database. ...</small></li>
<li><a href="https://undocumentedmatlab.com/articles/speeding-up-matlab-jdbc-sql-queries" rel="bookmark" title="Speeding up Matlab-JDBC SQL queries">Speeding up Matlab-JDBC SQL queries </a> <small>Fetching SQL ResultSet data from JDBC into Matlab can be made significantly faster. ...</small></li>
<li><a href="https://undocumentedmatlab.com/articles/matlab-toolstrip-part-5-icons" rel="bookmark" title="Matlab toolstrip – part 5 (icons)">Matlab toolstrip – part 5 (icons) </a> <small>Icons can be specified in various ways for toolstrip controls and the app window itself. ...</small></li>
<li><a href="https://undocumentedmatlab.com/articles/sending-html-emails-from-matlab" rel="bookmark" title="Sending HTML emails from Matlab">Sending HTML emails from Matlab </a> <small>Matlab's sendmail only sends simple text messages by default; a simple hack can cause it to send HTML-formatted messages. ...</small></li>
</ol>
</div>
]]></content:encoded>
					
					<wfw:commentRss>https://undocumentedmatlab.com/articles/using-sqlite-in-matlab/feed</wfw:commentRss>
			<slash:comments>5</slash:comments>
		
		
			</item>
	</channel>
</rss>
