isopen
Determine if MySQL native interface 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
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
MySQL native interface database connection, specified as a connection
object. Starting in R2024a, it is recommended that you use setSecret
and getSecret
to store and retrieve your credentials for databases that require authentication. For
more details, refer to this
example.
Version History
Introduced in R2020b
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.
Sélectionner un site web
Choisissez un site web pour accéder au contenu traduit dans votre langue (lorsqu'il est disponible) et voir les événements et les offres locales. D’après votre position, nous vous recommandons de sélectionner la région suivante : .
Vous pouvez également sélectionner un site web dans la liste suivante :
Comment optimiser les performances du site
Pour optimiser les performances du site, sélectionnez la région Chine (en chinois ou en anglais). Les sites de MathWorks pour les autres pays ne sont pas optimisés pour les visites provenant de votre région.
Amériques
- 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)