%% Determine If Database Connection Is Open
% Connect to a MySQL® database using the MySQL native interface and verify the database connection. Then, import data from the database into MATLAB® using the database table productTable. Determine the highest unit cost among the retrieved products in the table. Close the database connection and ensure that the connection is closed.
%
% Create a MySQL native interface database connection using a data source, user name, and password. The MySQL database contains the table productTable.

datasource = "MySQLNative";
username = "root";
password = "matlab";
conn = mysql(datasource,username,password);

%
% Determine if the database connection is open. The isopen function returns the logical 1, which means the database connection is open.

i = isopen(conn)

%
% Select all the data from productTable and sort it by the product number. data is a table containing the imported data that results from executing the SQL SELECT statement.

sqlquery = "SELECT * FROM productTable ORDER BY productNumber";
data = fetch(conn,sqlquery);

%
% Display the first three rows of data.

head(data,3)

%
% Determine the highest unit cost in the table.

max(data.unitCost)

%
% Close the database connection.

close(conn)

%
% Determine if the database connection is closed. The isopen function returns the logical 0, which means the database connection is closed. If the database connection is invalid, the isopen function returns the same result.

i = isopen(conn)

% Copyright 2020 The MathWorks, Inc.