This post was originally published on April 13th, 2008. It was imported from a previous version of the site.

I normally don’t delve too much into the programmatic details of my work, but I’ve seen enough interest in this topic that I figured I’d lend a helping hand to those fellow frustrated souls.

There are a few awkward, non-native MEX implementations of various database interfaces. However, Matlab has its own database toolbox built around ODBC/JDBC, and when developing distributable software, one always hopes to minimize third-party library usage. As a result, I’ve put a good deal of effort into integrating Matlab with both MySQL and SQLite. In fact, the data for every post on this site is stored in a 2GB MySQL database server running on my laptop.

As you can see, this requires only that you distribute the platform-independent JAR. No DLLs, no MEX compilation.

% Check to make sure that we successfully connectedif isconnection(dbConn)% Fetch the symbol, market cap, and last close for the 10 largest% market cap ETFs result = get(fetch(exec(dbConn, 'SELECT info.symbol,info.marketcap,series.close FROM info, series WHERE info.type=''ETF'' AND info.id = series.symbolid AND series.date = ''2008-04-11'' ORDER BY marketcap DESC LIMIT 10')), 'Data'); disp(result);

% If the connection failed, print the error messageelse disp(sprintf('Connection failed: %s', dbConn.Message));end% Close the connection so we don't run out of MySQL threadsclose(dbConn);