isopen
Determine if PostgreSQL native interface database connection is open
Since R2020b
Syntax
Description
Examples
Determine If Database Connection Is Open
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
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 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
Input Arguments
conn
— PostgreSQL native interface database connection
connection
object
PostgreSQL native interface database connection, specified as a connection
object.
Version History
Introduced in R2020b
MATLAB 命令
您点击的链接对应于以下 MATLAB 命令:
请在 MATLAB 命令行窗口中直接输入以执行命令。Web 浏览器不支持 MATLAB 命令。
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list:
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- 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)