close
Close MySQL native interface database connection
Syntax
Description
Examples
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 configurationCatalog and Schema Information— Names of catalogs and schemas in the databaseDatabase 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)
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.
Seleziona un sito web
Seleziona un sito web per visualizzare contenuto tradotto dove disponibile e vedere eventi e offerte locali. In base alla tua area geografica, ti consigliamo di selezionare: .
Puoi anche selezionare un sito web dal seguente elenco:
Come ottenere le migliori prestazioni del sito
Per ottenere le migliori prestazioni del sito, seleziona il sito cinese (in cinese o in inglese). I siti MathWorks per gli altri paesi non sono ottimizzati per essere visitati dalla tua area geografica.
Americhe
- América Latina (Español)
- Canada (English)
- United States (English)
Europa
- 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)