sqlite - SQLite connection - MATLAB (original) (raw)

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

Description

`conn` = sqlite([dbfile](#bvmbs2w-dbfile)) connects to an existing SQLite database file.

example

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

example

Input Arguments

expand all

dbfile — SQLite database file

character vector | string scalar

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

mode — SQLite database file mode

"connect" (default) | "readonly" | "create"

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

Value Description
"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

Database — SQLite database file name

character vector

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

IsOpen — Database connection indicator

0 (default) | 1

This property is read-only.

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

IsReadOnly — Read-only database file indicator

0 (default) | 1

This property is read-only.

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

Data Types: logical

AutoCommit — Flag to autocommit transactions

'on' (default) | 'off'

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

Object Functions

expand all

SQLite Database Connection

isopen Determine if SQLite connection is open
close Close SQLite connection

Import Data into MATLAB

sqlread Import data into MATLAB from SQLite database table
fetch Import data into MATLAB workspace using SQLite connection

Export Data from MATLAB

sqlwrite Insert MATLAB data into SQLite database table

Database Operations

commit Make changes to SQLite database file permanent
execute Execute SQL statement using SQLite database connection
rollback Undo changes to SQLite database file
sqlupdate Update rows in SQLite database table

Examples

collapse all

Create SQLite Connection to Existing Database File

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/Bdoc24b_2855429_3075413/tpe0b778a4/database-ex96650978/tutorial.db'
IsReadOnly: 0
AutoCommit: 'on'

conn is an sqlite object with these properties:

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

Close the SQLite connection.

Create SQLite Connection Using New Database File

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/Bdoc24b_2855429_3075413/tpe0b778a4/database-ex61952421/mysqlite.db'
IsReadOnly: 0
AutoCommit: 'on'

conn is an sqlite object with these properties:

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

Close the SQLite connection.

Create Read-Only SQLite Connection

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/Bdoc24b_2855429_3075413/tpe0b778a4/database-ex41829813/tutorial.db'
IsReadOnly: 1
AutoCommit: 'on'

conn is an sqlite object with these properties:

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

Close the SQLite connection.

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

See Also

Topics