%% Commit Data to PostgreSQL Database
% Use a PostgreSQL native interface database connection to insert product data from MATLAB® into a new table in a PostgreSQL database. Then, commit the changes to the database.
%
% Create a PostgreSQL native interface database connection to a PostgreSQL database using the data source name, user name, and password. The database contains the tables productTable and suppliers.

datasource = "PostgreSQLDataSource";
username = "dbdev";
password = "matlab";
conn = postgresql(datasource,username,password);

%
% Allow manual committing of changes to the database by setting the AutoCommit property to off.

conn.AutoCommit = "off";

%
% Create a MATLAB table that contains data for two products.

data = table([30;40],[500000;600000],[1000;2000],[25;30], ...
    ["Rubik's Cube";"Doll House"],'VariableNames',["productNumber" ...
    "stockNumber" "supplierNumber" "unitCost" "productDescription"]);

%
% Insert the product data into a new database table toytable.

tablename = "toytable";
sqlwrite(conn,tablename,data)

% Import the contents of the database table into MATLAB and display the rows. The results contain two rows for the inserted products.

rows = sqlread(conn,tablename)

%
% Commit the changes to the database.

commit(conn)

%
% Close the database connection.

close(conn)

% Copyright 2020 The MathWorks, Inc.