Main Content


Execute SQL statement using SQLite connection

The exec function will be removed in a future release. Use the execute function to manage the SQLite database instead.



exec(conn,sqlquery) performs database operations on an SQLite database file by executing the SQL statement sqlquery for the SQLite connection conn using the MATLAB® interface to SQLite. For example, use this syntax to create database tables in the SQLite database file. To import data into MATLAB from the SQLite database file, use the fetch function.


collapse all

Using the MATLAB® Interface to SQLite, create a table in a new SQLite database file.

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.

To insert data into the database file, use the insert function.

Close the SQLite connection.


Input Arguments

collapse all

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

SQL statement, specified as a character vector or string scalar. The SQL statement can be any valid SQL statement, including nested queries. The SQL statement can be a stored procedure, such as {call sp_name (parm1,parm2,...)}. For stored procedures that return one or more result sets, use the exec function. For procedures that return output arguments, use runstoredprocedure.

For information about the SQL query language, see the SQL Tutorial.

Data Types: char | string

Version History

Introduced in R2016a