%% Create DatabaseDatastore Object Using Database Table
% Retrieve a large data set from a database table by creating a DatabaseDatastore object. This example uses a MySQL® database.
%
% Create a database connection to a MySQL database with the user name and password.

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

%
% Load flight information in the MATLAB® workspace.

flights = readtable('airlinesmall_subset.xlsx');

% Create the flights database table using the flight information.

tablename = 'flights';
sqlwrite(conn,tablename,flights)

%
% Create a DatabaseDatastore object using a database connection and the flights database table.

dbds = databaseDatastore(conn,tablename)

% dbds is a DatabaseDatastore object with these properties:
% Connection — Database connection object
% Query — Executed SQL query
% VariableNames — List of column names from the executed SQL query
% ReadSize — Maximum number of records to read from the executed SQL query
%
% Display the database connection property.

dbds.Connection

% The Message property is blank when the database connection is successful.
%
% Preview the first eight records in the data set returned by executing the SQL query in the DatabaseDatastore object.

preview(dbds)

%
% Close the DatabaseDatastore object and the database connection.

close(dbds)

% Copyright 2018 The MathWorks, Inc.