Main Content

Deploy MySQL Native Interface Database Application with MATLAB Compiler

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

The example uses the MySQL native interface to create the database connection.

Before you begin, note the following:

  • You must first install the MySQL/C++ Connector file on each machine where you plan to run the standalone application. For details about configuring the data source, see Configure MySQL Native Interface Data Source.

  • You must have administrator privileges on each machine where you plan to run the standalone application.

Create Function in MATLAB

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

You must use the syntax with name-value arguments when you connect to the database using the mysql function.

type importMySQLNative.m
function maxProdNum = importMySQLNative
%  IMPORTMYSQLNATIVE The importMySQLNative function connects to a MySQL®
%  database using the MySQL native interface, imports data from the
%  database into MATLAB®, performs a simple data analysis, and closes the
%  database connection. The database contains a table named |productTable|.

%%
% Connect to the database by using name-value pair arguments of the |mysql|
% function to specify a connection to a MySQL database. For example, this
% code assumes that you are using the user name |username|, password |pwd|,
% database |dbname|, database server |sname|, and port number |3306|.
conn = mysql("username","pwd", ...
    "DatabaseName","dbname",  ... 
    "Server","sname", ... 
    "PortNumber",3306);
%%
% 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 importMySQLNative.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

| |

Topics

External Websites