Database Toolbox    
insert

Export MATLAB cell array data into database table

Syntax

Description

insert(conn, 'table', colnames, exdata) exports records from the MATLAB cell array exdata, into new rows in an existing database table tab, via the connection conn. Specify the column names for tab as strings in the MATLAB cell array, colnames.

The status of the AutoCommit flag determines if insert automatically commits the data or if you need to commit the data following the insert. View the AutoCommit flag status for the connection using get and change it using set. Commit the data using commit or issue an SQL commit statement via an exec function. Roll back the data using rollback or issue an SQL rollback statement via an exec function.

To replace existing data instead of adding new rows, use update.

Examples

Example 1 - Insert a Record

Insert one record consisting of two columns, City and Avg_Temp, into the Temperatures table. The data is San Diego, 88 degrees. The database connection is conn.

Assign the data to the cell array.

Create a cell array containing the column names in Temperatures.

Perform the insert.

The row of data is added to the Temperatures table.

Example 2 - Insert Multiple Records

Insert a cell array, exdata, containing 28 rows of data with three columns, into the Growth table. The data columns are Date, Avg_Length, and Avg_Wt. The database connection is conn.

Insert the data.

The records are inserted in the table.

Example 3 - Import Records, Perform Computations, and Export Data

Perform calculations on imported data and then export the data. First import all of the data in the products table.

Assign the variable id to the first column of data.

Assign the variable price to the sixth column of data.

Calculate the discounted price (25% off) and assign it to the variable sale_price. You must convert the cell array price to a numeric array in order to perform the calculation.

To export the data, it must be in a cell array. The variable sale_price is a numeric array because it was the result of the discount calculation. You must convert sale_price to a cell array. To convert the columns of data in sale_price to a cell arrays, type

Create an array, exdata, that contains the three columns of data to be exported. Put the id data in column one, price in column two, and sale_price in column three.

Assign the column names to a string array, colnames.

Export the data to the Sale table.

All rows of data are inserted into the Sale table.

Example 4 - Insert Followed by commit

This example demonstrates the use of the SQL commit function following an insert. The AutoCommit flag is off.

Insert the cell array exdata into the column names colnames of the Error_Rate table.

Commit the data using the commit function.

Alternatively, you could commit the data using the exec function with an SQL commit statement.

See Also
commit, database, exec, rollback, set, update


 indexinfo isconnection