Main Content


Add MATLAB data to SQLite database table

The insert function will be removed in a future release. Use the sqlwrite function instead. For details, see Compatibility Considerations.



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.


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)'];


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.


Input Arguments

collapse all

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

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

Example: "employees"

Data Types: string | char

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.

Version History

Introduced in R2016a

collapse all

R2022a: insert function will be removed

The insert function will be removed in a future release. Use the sqlwrite function to import data. Some differences between the workflows might require updates to your code.

Update Code

Use the sqlwrite function with the sqlite object to export data from a SQLite database.

In prior releases, you exported data using the insert function. For example:

tablename = 'inventoryTable';
colnames = {'productNumber','Quantity','Price','inventoryDate'};
insert(conn,tablename,colnames, ...
    {20,150,50.00,'11/3/2015 2:24:33 AM'})

Now you can export data using the sqlwrite function.

tablename = "productTable";
data = table(30,500000,1000,25,"Rubik's Cube", ...
    'VariableNames',["productNumber" "stockNumber" ...
    "supplierNumber" "unitCost" "productDescription"]);