%% Connect to MySQL Using ODBC Driver with Additional Options
% Connect to the MySQL® database using an ODBC driver. Then, import data from the database into MATLAB®, perform simple data analysis, and then close the database connection. The example assumes that you are connecting to the MySQL database version 5.7.22 and MySQL ODBC 5.3 ANSI driver.
%
% Create a database connection to a MySQL database and a login timeout of 5 seconds. Specify the user name and password.

databasename = "toystore_doc";
username = "username";
password = "password";
conn = database(databasename,username,password,'Vendor','MySQL', ...
    'Server','dbtb01','PortNumber',3306,'LoginTimeout',5)

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

selectquery = "SELECT * FROM inventoryTable";
data = select(conn,selectquery);
head(data,3)

%
% Determine the highest quantity in the table.

max(data.Quantity)

%
% Close the database connection.

close(conn)

% Copyright 2020 The MathWorks, Inc.