Database Toolbox User's Guide
  Go to function:
    Search    Help Desk 
insert    Examples   See Also

Export MATLAB cell array data into database table

Syntax

Description

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

The status of the autocommit flag determines if insert automatically commits the data or if an SQL commit command is needed following the insert. View the autocommit flag status for the connection using get and change it using set. Perform an SQL commit or rollback using exec.

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.

Create a cell array that will contain the record to be inserted. It is one row by two columns:

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, G, containing 28 rows of data with three columns, into the Growth table. The data columns are Birthdate, Avg_Length, and Avg_Weight. The database connection is conn1.

Assign the column name strings to a cell array named colnamesG:

Insert the data:

The records are inserted in the table.

Example 3 - Import records, perform computations, and then 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 and assign it to the variable sale_price:

In order to export the data, you must convert it to a cell array. To convert the columns of data into cell arrays, type:

Create an array, data, 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 SQL commit

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

Insert the cell array data1 into the column names C of the Error_Rate table:

Commit the data:

See Also

database, exec, set, update

[ Previous | Help Desk | Next ]