Main Content

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

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

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. This property is hidden from the display.

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

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

isopenDetermine if SQLite connection is open
closeClose SQLite connection
sqlreadImport data into MATLAB from SQLite database table
fetchImport data into MATLAB workspace using SQLite connection
sqlwriteInsert MATLAB data into SQLite database table
commitMake changes to SQLite database file permanent
executeExecute SQL statement using SQLite database connection
rollbackUndo changes to SQLite database file
sqlupdateUpdate rows in SQLite database table

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/Bdoc23b_2361005_1127066/tpb67eab4b/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/Bdoc23b_2361005_1127066/tpb67eab4b/database-ex61952421/mysqlite.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 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/Bdoc23b_2361005_1127066/tpb67eab4b/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