isopen
Determine if database connection is open
Syntax
Description
Examples
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
Database connection, specified as any of the following:
MySQL
connectionobject created by using themysqlfunction.PostgreSQL
connectionobject created by using thepostgresqlfunction.DuckDB
connectionobject created by using theduckdbfunction.SQLite
connectionobject created by using thesqlitefunction.ODBC
connectionobject created by using thedatabasefunction.JDBC
connectionobject created by using thedatabasefunction.
Version History
Introduced in R2020bDetermine whether a DuckDB database connection is open by specifying a DuckDB
connection object when using the isopen
function.
MATLAB Command
You clicked a link that corresponds to this MATLAB command:
Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.
Web サイトの選択
Web サイトを選択すると、翻訳されたコンテンツにアクセスし、地域のイベントやサービスを確認できます。現在の位置情報に基づき、次のサイトの選択を推奨します:
また、以下のリストから Web サイトを選択することもできます。
最適なサイトパフォーマンスの取得方法
中国のサイト (中国語または英語) を選択することで、最適なサイトパフォーマンスが得られます。その他の国の MathWorks のサイトは、お客様の地域からのアクセスが最適化されていません。
南北アメリカ
- América Latina (Español)
- Canada (English)
- United States (English)
ヨーロッパ
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)