Main Content

close

Close database connection

Description

close(conn) closes and invalidates the database connection. Once closed, the connection cannot be reused for executing queries or other operations. To interact with the database again, you must create a new connection object.

conn can be any of the following database connection objects:

  • MySQL®

  • PostgreSQL®

  • DuckDB™

  • SQLite

  • ODBC

  • JDBC

example

Examples

collapse all

Create a MySQL® native interface connection to a MySQL database. Then, import data from the database into MATLAB® and perform simple data analysis. Close the database connection.

This example assumes that you are connecting to a MySQL database using the MariaDB® C Connector driver.

Connect to the database using the data source name, user name, and password.

datasource = "MySQLNative";
username = "root";
password = "matlab";

conn = mysql(datasource,username,password)
conn = 
  connection with properties:

                  DataSource: "MySQLNative"
                    UserName: "root"

  Database Properties:

                  AutoCommit: "on"
                LoginTimeout: 0
      MaxDatabaseConnections: 0

  Catalog and Schema Information:

              DefaultCatalog: "toy_store"
                    Catalogs: ["information_schema", "mysql", "performance_schema" ... and 3 more]
                     Schemas: []

  Database and Driver Information:

         DatabaseProductName: "MySQL"
      DatabaseProductVersion: "8.0.3-rc-log"
                  DriverName: "Mariadb Connector/C"
               DriverVersion: "3.2.5"

The property sections of the connection object are:

  • Database Properties — Information about the database configuration

  • Catalog and Schema Information — Names of catalogs and schemas in the database

  • Database and Driver Information — Names and versions of the database and driver

Import all data from the table inventoryTable into MATLAB using the sqlread function. Display the first three rows of data.

tablename = "inventoryTable";
data = sqlread(conn,tablename);
head(data,3)
    productNumber    Quantity    Price       inventoryDate    
    _____________    ________    _____    ____________________

          1            1700       15      23-Sep-2014 13:38:34
          2            1200        9      09-Jul-2014 02:50:45
          3             356       17      14-May-2014 11:14:28

Determine the highest product quantity from the table.

max(data.Quantity)
ans = 
9000

Close the database connection conn.

close(conn)

Create a PostgreSQL native interface connection to a PostgreSQL database. Then, import data from the database into MATLAB® and perform simple data analysis. Close the database connection.

This example assumes that you are connecting to a PostgreSQL database version 9.405 using the libpq driver version 10.12.

Connect to the database using the data source name, user name, and password.

datasource = "PostgreSQLDataSource";
username = "dbdev";
password = "matlab";

conn = postgresql(datasource,username,password)
conn = 
  connection with properties:

                  DataSource: "PostgreSQLDataSource"
                    UserName: "dbdev"

  Database Properties:

                  AutoCommit: "on"
                LoginTimeout: 0
      MaxDatabaseConnections: 100

  Catalog and Schema Information:

              DefaultCatalog: "toystore_doc"
                    Catalogs: "toystore_doc"
                     Schemas: ["pg_toast", "pg_temp_1", "pg_toast_temp_1" ... and 3 more]

  Database and Driver Information:

         DatabaseProductName: "PostgreSQL"
      DatabaseProductVersion: "9.405"
                  DriverName: "libpq"
               DriverVersion: "10.12"

The property sections of the connection object are:

  • Database Properties — Information about the database configuration

  • Catalog and Schema Information — Names of catalogs and schemas in the database

  • Database and Driver Information — Names and versions of the database and driver

Import all data from the table inventoryTable into MATLAB using the sqlread function. Display the first three rows of data.

tablename = "inventoryTable";
data = sqlread(conn,tablename);
head(data,3)
ans=3×4 table
    1    1700    14.5000    "2014-09-23 09:38:34"
    2    1200     9.0000    "2014-07-08 22:50:45"
    3     356    17.0000    "2014-05-14 07:14:28"

Determine the highest product quantity from the table.

max(data.quantity)
ans = 
9000

Close the database connection conn.

close(conn)

Create a transient, in-memory DuckDB™ database connection by using the duckdb function.

conn = duckdb()
conn = 
  connection with properties:

                    Database: "memory"

  Database Properties:

                    ReadOnly: false
                  AutoCommit: "on"

  Catalog and Schema Information:

              DefaultCatalog: "memory"
                    Catalogs: "memory"
                     Schemas: "main"

  Database and Driver Information:

         DatabaseProductName: "DuckDB"
      DatabaseProductVersion: "v1.3.2"

Verify that the connection is open by using the isopen function and confirm that the output is 1.

isopen(conn)
ans = logical
   1

Close the connection by using the close function. Check that the connection is closed by using the isopen function and verify that the output is 0.

close(conn);
isopen(conn)
ans = logical
   0

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

dbfile = fullfile(pwd,"tutorial.db");
conn = sqlite(dbfile);

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

Close the SQLite connection.

close(conn)

Connect to a Microsoft® SQL Server® database and 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 containing the imported data that results from executing the SQL SELECT statement.

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

Display the first three rows of data.

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.

max(data.unitCost)
ans =

    24

Close the database connection.

close(conn)

Input Arguments

collapse all

Database connection, specified as any of the following:

  • MySQL connection object created by using the mysql function.

  • PostgreSQL connection object created by using the postgresql function.

  • DuckDB connection object created by using the duckdb function.

  • SQLite connection object created by using the sqlite function.

  • ODBC connection object created by using the database function.

  • JDBC connection object created by using the database function.

Version History

Introduced in R2020b

expand all