%% Connect to MySQL Using ODBC Database Connection
% Connect to a MySQL® database using an ODBC database connection. Then, import data from the database into MATLAB®, perform a simple data analysis, and close the database connection.
% This example assumes that you are connecting to a MySQL Version 5.7.22 database using the MySQL ODBC 5.3 Driver.
%
% Create a database connection to a MySQL database. Specify the user name and password.

datasource = "MySQL ODBC";
conn = odbc(datasource,"root","matlab")

% conn has an empty Message property, which indicates a successful connection.
% The property sections of the conn 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 fetch function. Display the first three rows of data.

query = "SELECT * FROM inventoryTable";
data = fetch(conn,query);
head(data,3)

%
% Determine the highest product quantity in the table.

max(data.Quantity)

%
% Close the database connection.

close(conn)

% Copyright 2020 The MathWorks, Inc.