%% Connect to MySQL Database Using MySQL Native Interface
% Create a MySQL® native interface connection to a MySQL database. Then, import data from the database into MATLAB® and perform simple data analysis. Close the database connection.
% This example assumes that you are connecting to a MySQL database version 5.7.22 using the MySQL Connector/C++ driver version 8.0.15.
%
% Connect to the database using the data source name, user name, and password.

datasource = "MySQLNative";
username = "root";
password = "matlab";

conn = mysql(datasource,username,password)

% The property sections of the connection object are:
% Database Properties — Information about the database configuration
% Catalog and Schema Information — Names of catalogs and schemas in the database
% Database and Driver Information — Names and versions of the database and driver
%
% Import all data from the table inventoryTable into MATLAB using the sqlread function. Display the first three rows of data.

tablename = "inventoryTable";
data = sqlread(conn,tablename);
head(data,3)

%
% Determine the highest product quantity from the table.

max(data.Quantity)

%
% Close the database connection conn.

close(conn)

%
% Copyright 2020 The MathWorks, Inc.