insert

Add MATLAB data to SQLite database table

Syntax

insert(conn,tablename,colnames,data)

Description

example

insert(conn,tablename,colnames,data) exports data from the MATLAB® workspace and inserts it into an existing database table by using the SQLite database connection conn. You can specify the database table name and column names, and specify the data to be inserted into the database.

Examples

collapse all

Create a table in a new SQLite database file, and then insert a new row of data into the table.

Create the SQLite connection conn to the new SQLite database file tutorial.db. Specify the file name in the current folder.

dbfile = fullfile(pwd,'tutorial.db');

conn = sqlite(dbfile,'create');

Create the table inventoryTable using exec.

createInventoryTable = ['create table inventoryTable ' ...
    '(productNumber NUMERIC, Quantity NUMERIC, ' ...
    'Price NUMERIC, inventoryDate VARCHAR)'];

exec(conn,createInventoryTable)

inventoryTable is an empty table in tutorial.db.

Insert a row of data into inventoryTable.

colnames = {'productNumber','Quantity','Price','inventoryDate'};

insert(conn,'inventoryTable',colnames, ...
    {20,150,50.00,'11/3/2015 2:24:33 AM'})

Close the SQLite connection.

close(conn)

Input Arguments

collapse all

SQLite database connection, specified as an sqlite object created using the sqlite function.

Database table name, specified as a character vector or string scalar denoting the name of a table in the database.

Example: 'employees'

Data Types: char | string

Database table column names, specified as a cell array of one or more character vectors or a string array to denote the columns in the existing database table tablename.

Example: {'col1','col2','col3'}

Data Types: cell | string

Insert data, specified as a numeric matrix, structure, table, or cell array.

Introduced in R2016a