Documentation

close

Close database connection, SQLite connection, DatabaseDatastore, cursor, or resultset object

A database connection object creates a connection between an installed database and MATLAB®. For details, see Connecting to a Database. A SQLite connection object creates a connection to a SQLite database file using the MATLAB Interface to SQLite. For details, see Working with the MATLAB Interface to SQLite. A DatabaseDatastore object creates a connection to a type of datastore for working with large data. For details, see Working with a DatabaseDatastore. A cursor object stores imported data. For details, see Importing Data Using the fetch Function. A resultset object provides metadata about cursor objects. For details, see resultset.

Syntax

Description

example

close(object) closes the database and driver resource utilizer object to free up database and driver resources.

Examples

collapse all

Close the Database Connection Object

Using the native ODBC interface, connect to the database with the ODBC data source name dbtoolboxdemo with the user name admin and password admin.

conn = database.ODBCConnection('dbtoolboxdemo','admin','admin');

Close the database connection conn.

close(conn)

Close the SQLite Connection Object

Create a SQLite connection conn to the MATLAB Interface to SQLite using an existing database file tutorial.db.

dbfile = 'tutorial.db';

conn = sqlite(dbfile);

Close the SQLite connection conn.

close(conn)

Close the DatabaseDatastore Object

Using the native ODBC interface, connect to the database with the ODBC data source name dbtoolboxdemo with the user name admin and password admin.

conn = database.ODBCConnection('dbtoolboxdemo','admin','admin');

Create a DatabaseDatastore object dbds using the database connection conn and SQL query sqlquery. This SQL query retrieves all products from the product table productTable ordered by product number.

sqlquery = 'select * from productTable order by productNumber';

dbds = datastore(conn,sqlquery);

Close the DatabaseDatastore object dbds.

close(dbds)

Close the Cursor Object

Using the native ODBC interface, connect to the database with the ODBC data source name dbtoolboxdemo with the user name admin and password admin.

conn = database.ODBCConnection('dbtoolboxdemo','admin','admin');

Select data from productTable that you access using the database.ODBCConnection object conn. Assign the returned cursor object to the variable curs.

sqlquery = 'select * from productTable';
curs = exec(conn,sqlquery);

After you finish working with the cursor object, close it.

close(curs)

Close the database connection conn.

close(conn)

Close the Resultset Object

Connect to the database with the ODBC data source name dbtoolboxdemo with the user name admin and password admin.

conn = database('dbtoolboxdemo','admin','admin');

Select data from productTable that you access using the database connection object conn. Assign the returned cursor object to the variable curs.

sqlquery = 'select * from productTable';
curs = exec(conn,sqlquery);

Construct a resultset object rset.

rset = resultset(curs);

Close the resultset object rset.

close(rset)

After you finish working with the cursor object, close it.

close(curs)

Close the database connection conn.

close(conn)

Related Examples

Input Arguments

collapse all

object — Database and driver resource utilizerdatabase connection object | SQLite connection object | DatabaseDatastore object | cursor object | resultset object

Database and driver resource utilizer, specified as one of these objects. See the function in the last column of this table to create the respective object.

Object Argument NameObject DescriptionFunction

conn

Database connection object or native ODBC database connection object

database

conn

SQLite connection object

sqlite

dbds

DatabaseDatastore object

datastore

curs

Cursor object or native ODBC cursor object

exec

rset

Resultset object

resultset

Database connections, SQLite connections, DatabaseDatastore objects, cursors, and resultset objects remain open until you close them using the close function. Always close a cursor, DatabaseDatastore, database and SQLite connection, or resultset object when you finish using it. Close a cursor before closing the connection used for that cursor. Executing close with a DatabaseDatastore object releases the MATLAB resources associated with database connection and cursor objects.

    Note:   The MATLAB session closes open cursors, DatabaseDatastore objects, and connections when exiting, however, the database might not free up the cursors and connections.

Introduced before R2006a

Was this topic helpful?