exec

Execute SQL statement using SQLite connection

Syntax

exec(conn,sqlquery)

Description

example

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.

Examples

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

exec(conn,createInventoryTable)

inventoryTable is an empty table in tutorial.db.

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

Close the SQLite connection.

close(conn)

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 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

Introduced in R2016a