%% Connect to MySQL Using ODBC Driver
% Connect to a MySQL® database. Then, import data from the database into MATLAB®. Perform simple data analysis, and then close the database connection.
% To create a database connection using an ODBC driver, you must configure an ODBC data source.
% This example assumes that you are connecting to a MySQL Version 5.7.22 database using the MySQL Driver 5.3.
%
% Create a database connection to the ODBC data source MySQL ODBC. Specify the user name and password.

datasource = "MySQL ODBC";
username = "username";
password = "password";
conn = database(datasource,username,password)

% 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 sqlread function. Display the first eight rows of data.

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

%
% Determine the highest product quantity in the table.

max(data.Quantity)

%
% Close the database connection.

close(conn)

% Copyright 2020 The MathWorks, Inc.