Hauptinhalt

isopen

Determine if database connection is open

Description

i = isopen(conn) returns 1 if the database connection is open and 0 if it is closed or invalid.

conn can be any of the following connection objects:

  • MySQL®

  • PostgreSQL®

  • DuckDB™

  • SQLite

  • ODBC

  • JDBC

example

Examples

collapse all

Connect to a MySQL® database using the MySQL native interface and verify the database connection. Then, import data from the database into MATLAB® using the database table productTable. Determine the highest unit cost among the retrieved products in the table. Close the database connection and ensure that the connection is closed.

Create a MySQL native interface database connection using a data source, user name, and password. The MySQL database contains the table productTable.

datasource = "MySQLNative";
username = "root";
password = "matlab";
conn = mysql(datasource,username,password);

Determine if the database connection is open. The isopen function returns the logical 1, which means the database connection is open.

i = isopen(conn)
i = logical
   1

Select all the 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.

sqlquery = "SELECT * FROM productTable ORDER BY productNumber";
data = fetch(conn,sqlquery);

Display the first three rows of data.

head(data,3)
ans=3×5 table
    1    400345    1001    14    "Building Blocks"
    2    400314    1002     9       "Painting Set"
    3    400999    1009    17             "Slinky"

Determine the highest unit cost in the table.

max(data.unitCost)
ans = 
24

Close the database connection.

close(conn)

Determine if the database connection is closed. The isopen function returns the logical 0, which means the database connection is closed. If the database connection is invalid, the isopen function returns the same result.

i = isopen(conn)
i = logical
   0

Connect to a PostgreSQL database using the PostgreSQL native interface and verify the database connection. Then, import data from the database into MATLAB® using the database table productTable. Determine the highest unit cost among the retrieved products in the table. Close the database connection and ensure that the connection is closed.

Create a PostgreSQL native interface database connection using a data source, user name, and password. The PostgreSQL database contains the table productTable.

datasource = "PostgreSQLDataSource";
username = "dbdev";
password = "matlab";
conn = postgresql(datasource,username,password);

Determine if the database connection is open. The isopen function returns the logical 1, which means the database connection is open.

i = isopen(conn)
i = logical
   1

Select all the 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.

sqlquery = "SELECT * FROM productTable ORDER BY productNumber";
data = fetch(conn,sqlquery);

Display the first three rows of data.

head(data,3)
ans=3×5 table
    1    400345    1001    14    "Building Blocks"
    2    400314    1002     9       "Painting Set"
    3    400999    1009    17             "Slinky"

Determine the highest unit cost in the table.

max(data.unitcost)
ans = 
24

Close the database connection.

close(conn)

Determine if the database connection is closed. The isopen function returns the logical 0, which means the database connection is closed. If the database connection is invalid, the isopen function returns the same result.

i = isopen(conn)
i = logical
   0

Create a transient, in-memory DuckDB™ database connection.

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 database connection is open by using the isopen function. A value of 1 indicates that the connection is open.

isopen(conn)
ans = logical
   1

Close the connection.

close(conn);

Connect to an SQLite 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 the SQLite connection conn to the existing SQLite database file tutorial.db. The database file contains the table productTable. The SQLite connection is an sqlite object.

dbfile = "tutorial.db";
conn = sqlite(dbfile);

Determine if the database connection is open. The isopen function returns the numeric scalar 1, which means the database connection is open.

i = isopen(conn)
i = logical
   1

Select all the 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.

sqlquery = "SELECT * FROM productTable ORDER BY productNumber";
data = fetch(conn,sqlquery);

Display the first three rows of data.

head(data,3)
    productNumber    stockNumber    supplierNumber    unitCost    productDescription
    _____________    ___________    ______________    ________    __________________

          1            400345            1001            14       "Building Blocks" 
          2            400314            1002             9       "Painting Set"    
          3            400999            1009            17       "Slinky"          

Determine the highest unit cost in the table.

max(data.unitCost)
ans = int64

24

Close the database connection.

close(conn)

Determine if the database connection is closed. The isopen function returns the numeric scalar 0, which means the database connection is closed. If the database connection is invalid, the isopen function returns the same result.

i = isopen(conn)
i = logical
   0

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 =

     []

Determine if the database connection is open. The isopen function returns the numeric scalar 1, which means the database connection is open.

i = isopen(conn)
i =

     1

Select all the 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)

Determine if the database connection is closed. The isopen function returns the numeric scalar 0, which means the database connection is closed. If the database connection is invalid, the isopen function returns the same result.

i = isopen(conn)
i =

     0

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