Main Content

Deploy MATLAB Interface to SQLite Database Application with MATLAB Compiler

This example shows how to write a script to analyze data stored in an SQLite database using the MATLAB® interface to SQLite, and then deploy the script as a standalone application. For this example, you write code that connects to a database, imports data from the database into MATLAB, analyzes the data, and closes the database connection. Then, you deploy the code by compiling it as a standalone application using the Standalone Application Compiler (MATLAB Compiler) app, and by running the application on other machines.

The example uses the MATLAB interface to SQLite to create the database connection.

Create Function in MATLAB

Create a MATLAB script named importSQLiteInterface.m and save it in a file location of your choice. The script contains the importSQLiteInterface function, which returns the maximum product number from the data in the productTable database table. The function connects to the SQLite database and imports all data from productTable. Then, the function calculates the maximum product number.

type importSQLiteInterface.m
function maxProdNum = importSQLiteInterface
% IMPORTSQLITEINTERFACE The importSQLiteInterface function connects to an
% SQLite database using the MATLAB(R) interface to SQLite, imports data
% from the database into MATLAB, performs a simple data analysis, and
% closes the database connection. The database contains a table named
% |productTable|.

%%
% Create the SQLite connection |conn| to the existing SQLite database file
% |tutorial.db|. The SQLite connection is an |sqlite| object.
dbfile = "tutorial.db";
conn = sqlite(dbfile);
%%
% Import data from the |productTable| database table.
tablename = "productTable";
data = sqlread(conn,tablename);
%%
% Determine the highest product number among products.
prodNums = data.productNumber;
maxProdNum = max(prodNums);
%%
% Close the database connection.
close(conn)
end

Create Standalone Application Using Application Compiler App

On the MATLAB Apps tab, on the far right of the Apps section, click the arrow to open the apps gallery. Under Application Deployment, click Standalone Application Compiler.

After you open the app, the Create Compiler Task dialog box prompts you to add a compiler task to a new or an existing MATLAB® project. For this example, select Start a new project and create a compiler task and create a new project in your working folder. For more information on creating and using MATLAB® projects, see Create Projects.

A new compiler task named StandaloneDesktopApp1 opens in the Editor. You can compile code for other deployment targets by opening the Compiler Task Manager app or going to the Manage Tasks tab and creating a new compiler task.

For this example, in the Main File section of the compiler task, click Add Main File and select importSQLiteInterface.m. In the Project panel, the file now has the labels Design and Main Function. To set up the rest of the standalone application, see Specify Build Options (MATLAB Compiler).

See Also

Objects

Functions

Topics

External Websites