# sqlite

SQLite connection

## Description

The `sqlite` function creates an `sqlite` object. You can use this object to connect to an SQLite database file using the MATLAB® interface to SQLite. The MATLAB interface to SQLite enables you to work with SQLite database files without installing and administering a database or driver. For details, see Interact with Data in SQLite Database Using MATLAB Interface to SQLite.

## Creation

### Syntax

``conn = sqlite(dbfile)``
``conn = sqlite(dbfile,mode)``

### Description

example

````conn = sqlite(dbfile)` connects to an existing SQLite database file.```

example

````conn = sqlite(dbfile,mode)` connects to an existing database file or creates and connects to a new database file, depending on the mode type.```

### Input Arguments

expand all

SQLite database file, specified as a character vector or string scalar. You can use the database file to store data and import and export it to MATLAB.

Data Types: `char` | `string`

SQLite database file mode, specified as one of these values.

ValueDescription

`"connect"`

Connect to an existing SQLite database file.

`"readonly"`

Create a read-only connection to an existing SQLite database file.

`"create"`

Create and connect to a new SQLite database file.

The file mode determines whether you connect to an existing SQLite database file or create a new one. For existing database files, the file mode determines whether the database connection is read-only and sets the IsReadOnly property. You can specify the file mode as a string scalar or character vector.

## Properties

expand all

SQLite database file name, specified as a character vector that contains the full path to the SQLite database file.

The `dbfile` input argument sets this property.

Example: `'C:\tutorial.db'`

Data Types: `char`

Database connection indicator, specified as a logical `0` when the database connection is closed or invalid, or a logical `1` when the database connection is open. This property is hidden from the display.

Read-only database file indicator, specified as a logical `0` when the SQLite database file can be modified, or a logical `1` when the database file is read-only.

Data Types: `logical`

Flag to autocommit transactions, specified as one of these values:

• `'on'` — Database transactions are automatically committed to the database.

• `'off'` — Database transactions must be committed to the database manually.

## Object Functions

expand all

 `isopen` Determine if SQLite connection is open `close` Close SQLite connection
 `sqlread` Import data into MATLAB from SQLite database table `fetch` Import data into MATLAB workspace using SQLite connection
 `sqlwrite` Insert MATLAB data into SQLite database table
 `commit` Make changes to SQLite database file permanent `execute` Execute SQL statement using SQLite database connection `rollback` Undo changes to SQLite database file

## Examples

collapse all

Create an SQLite connection to the MATLAB® interface to SQLite using the existing database file `tutorial.db`. Specify the file name in the current folder.

```dbfile = fullfile(pwd,"tutorial.db"); conn = sqlite(dbfile)```
```conn = sqlite with properties: Database: '/tmp/Bdoc22b_2134332_586485/tp43b717c8/database-ex96650978/tutorial.db' IsReadOnly: 0 AutoCommit: 'on' ```

`conn` is an `sqlite` object with these properties:

• `Database` — SQLite database file name.

• `IsOpen` — SQLite connection is open.

• `IsReadOnly` — SQLite connection is writable.

To import data from the database file, you can use the `fetch` function.

Close the SQLite connection.

`close(conn)`

Create an SQLite connection to the MATLAB® interface to SQLite using a new database file named `mysqlite.db`. Specify the file name in the current folder.

```dbfile = fullfile(pwd,"mysqlite.db"); conn = sqlite(dbfile,"create")```
```conn = sqlite with properties: Database: '/tmp/Bdoc22b_2134332_586485/tp43b717c8/database-ex61952421/mysqlite.db' IsReadOnly: 0 AutoCommit: 'on' ```

`conn` is an `sqlite` object with these properties:

• `Database `$\text{—}$ SQLite database file name.

• `IsOpen` $\text{—}$ SQLite connection is open.

• `IsReadOnly` $\text{—}$ SQLite connection is writable.

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

Close the SQLite connection.

`close(conn)`

Create a read-only SQLite connection to the MATLAB® interface to SQLite using the existing database file `tutorial.db`. Specify the file name in the current folder.

```dbfile = fullfile(pwd,"tutorial.db"); conn = sqlite(dbfile,"readonly")```
```conn = sqlite with properties: Database: '/tmp/Bdoc22b_2134332_586485/tp43b717c8/database-ex41829813/tutorial.db' IsReadOnly: 1 AutoCommit: 'on' ```

`conn` is an `sqlite` object with these properties:

• `Database` — SQLite database file name.

• `IsOpen` — SQLite connection is open.

• `IsReadOnly` — SQLite connection is read-only.

To import data from the database file, you can use the `fetch` function.

Close the SQLite connection.

`close(conn)`

## Alternative Functionality

Instead of the `sqlite` object, the `connection` object enables you to connect to various relational databases using ODBC and JDBC drivers that you install and administer. You can create the `connection` object by using the `database` function. To use the JDBC driver, close the SQLite connection and create a database connection using the URL string. For details, see these topics depending on your platform:

## Version History

Introduced in R2016a