Documentation

This is machine translation

Translated by Microsoft
Mouseover text to see original. Click the button below to return to the English verison of the page.

Note: This page has been translated by MathWorks. Please click here
To view all translated materals including this page, select Japan from the country navigator on the bottom of this page.

close

Close and invalidate database and driver resource utilizer

Syntax

close(object)

Description

example

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

Examples

collapse all

First, connect to the Microsoft® SQL Server® database. Verify the database connection. Then, import data from the database into MATLAB®. Determine the highest unit cost among the retrieved products in the table. Close the database connection.

Create an ODBC database connection to a Microsoft® SQL Server® database with Windows® authentication. Specify a blank user name and password. The database contains the table productTable.

datasource = 'MS SQL Server Auth';
conn = database(datasource,'','');

Check the database connection. If the Message property is empty, the connection is successful.

conn.Message
ans =

     []

Select all data from productTable and sort it by the product number. data is a table that contains the imported data from executing the SQL SELECT statement.

selectquery = 'SELECT * FROM productTable ORDER BY productNumber';
data = select(conn,selectquery);

Display first three rows of data.

data(1:3,:)
ans =

  3×5 table array

    productNumber    stockNumber    supplierNumber    unitCost    productDescription
    _____________    ___________    ______________    ________    __________________

    1                4.0035e+05     1001              14          'Building Blocks' 
    2                4.0031e+05     1002               9          'Painting Set'    
    3                  4.01e+05     1009              17          'Slinky'          

Determine the highest unit cost in the table.

max(data.unitCost)
ans =

    24

Close the database connection.

close(conn)

Create a SQLite connection using the MATLAB® interface to SQLite and the existing database file tutorial.db, which resides in the current working folder.

dbfile = fullfile(pwd,'tutorial.db');

conn = sqlite(dbfile);

Close the SQLite connection.

close(conn)

Using a JDBC driver, create a database connection to a Microsoft® SQL Server® database with Windows® authentication. Specify a blank user name and password. The code assumes that you are connecting to database toy_store, database server dbtb04, and port number 54317.

conn = database('toy_store','','','Vendor','Microsoft SQL Server', ...
    'Server','dbtb04','PortNumber',54317,'AuthType','Windows');

Create a DatabaseDatastore object using the database connection and SQL query. This SQL query retrieves all data from the table.

sqlquery = 'select * from airlinesmall';

dbds = databaseDatastore(conn,sqlquery);

Close the DatabaseDatastore object.

close(dbds)

First, connect to the Microsoft® SQL Server® database. Verify the database connection. Then, import data from the database into MATLAB®. Determine the highest unit cost among the retrieved products in the table. Close the database cursor and database connection.

Create an ODBC database connection to a Microsoft® SQL Server® database with Windows® authentication. Specify a blank user name and password. The database contains the table productTable.

datasource = 'MS SQL Server Auth';
conn = database(datasource,'','');

Check the database connection. If the Message property is empty, the connection is successful.

conn.Message
ans =

     []

Select all data from productTable and sort it by the product number. curs is the cursor object that contains the executed SQL query.

sqlquery = 'SELECT * FROM productTable ORDER BY productNumber';
curs = exec(conn,sqlquery);

Import data from the executed SQL query and display the first three rows.

curs = fetch(curs);
curs.Data(1:3,:)
ans =

  3×5 table

    productNumber    stockNumber    supplierNumber    unitCost    productDescription
    _____________    ___________    ______________    ________    __________________

    1                4.0035e+05     1001              14          'Building Blocks' 
    2                4.0031e+05     1002               9          'Painting Set'    
    3                  4.01e+05     1009              17          'Slinky'          

Determine the highest unit cost in the table.

data = curs.Data;
max(data.unitCost)
ans =

    24

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

close(curs)

After you close the cursor object, MATLAB® deletes the object. Use the clear function to remove the curs variable from the MATLAB® workspace.

curs
clear curs
curs = 

  handle to deleted cursor

Close the database connection.

close(conn)

Input Arguments

collapse all

Database and driver resource utilizer, specified as one of these objects.

Object Argument NameObject NameObject DescriptionObject Creation Function

conn

connection

Create a connection between installed database and MATLAB®. For details, see Connecting to Database.

database

conn

sqlite

Create connection to SQLite database file using the MATLAB interface to SQLite. For details, see Working with MATLAB Interface to SQLite.

sqlite

dbds

DatabaseDatastore

Create connection to a type of datastore for working with large data.

databaseDatastore

curs

cursor

Store imported data.

exec

Note

resultset has been removed.

  • connection objects, sqlite objects, DatabaseDatastore objects, and cursor objects remain open until you close them using the close function. Always close these objects when you finish using them.

  • Close a cursor object before closing the connection used for that cursor object.

  • Executing close with a DatabaseDatastore object releases the MATLAB resources associated with connection and cursor objects.

Note

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

Introduced before R2006a

Was this topic helpful?