# 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 Working with 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.

## Properties

expand all

This property is read-only.

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`

This property is read-only.

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.

Data Types: `logical`

This property is read-only.

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`

## Object Functions

 `insert` Add MATLAB data to SQLite database table `exec` Execute SQL statement using SQLite connection `fetch` Import data into MATLAB workspace using SQLite connection `close` Close SQLite connection

## 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/Bdoc20a_1326390_61855/tped7f6ec5/database-ex96650978/tutorial.db' IsOpen: 1 IsReadOnly: 0 ```

`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/Bdoc20a_1326390_55558/tp181ff5a3/database-ex61952421/mysqlite.db' IsOpen: 1 IsReadOnly: 0 ```

`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, you can create a table using the `exec` function, and then insert data using the `insert` 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/Bdoc20a_1326390_54902/tpfe3d8f2e/database-ex41829813/tutorial.db' IsOpen: 1 IsReadOnly: 1 ```

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

The `sqlite` object provides limited Database Toolbox™ functionality. For full functionality, create a database connection to the SQLite database file using the JDBC driver. To use the JDBC driver, close the SQLite connection and create a database connection using the URL string. For details, see these links depending on your platform: