%% Import Data from Database Table in SQLite Database File
% Use the MATLAB® interface to SQLite to import all rows of data into MATLAB® from a database table in an SQLite database file. Then, determine the highest unit cost among products in the table.
%
% Create the SQLite connection conn to the existing SQLite database file tutorial.db. The database file contains the table productTable. conn is an sqlite object.

dbfile = 'tutorial.db';
conn = sqlite(dbfile);

%
% Import all the data from productTable. The results output argument contains the imported data as a cell array.

sqlquery = 'SELECT * FROM productTable';
results = fetch(conn,sqlquery)

%
% Determine the highest unit cost of the products. Find the number of products by using the size function. Access unit cost data by looping through the fourth column of the cell array. data is a vector that contains numeric unit costs. Find the maximum unit cost.

rows = size(results);
for i = 1:rows
    data(i) = results{i,4};
end
max(data)

%
% Close the SQLite connection.

close(conn)

% Copyright 2018 The MathWorks, Inc.