mysql - Create MySQL native interface database connection - MATLAB (original) (raw)

Create MySQL native interface database connection

Since R2020b

Syntax

Description

`conn` = mysql([datasource](#mw%5Ff0fca8da-51fe-4f03-9986-5cde8bbed95e%5Fsep%5Fmw%5Fa2f973e8-1e16-42fd-9c7f-03c3bb265185),[username](#mw%5Ff0fca8da-51fe-4f03-9986-5cde8bbed95e%5Fsep%5Fbtcmi%5F9-username),[password](#mw%5Ff0fca8da-51fe-4f03-9986-5cde8bbed95e%5Fsep%5Fbtcmi%5F9-password)) creates a MySQL® native interface database connection using the specified data source, user name, and password. conn is a connection object.

example

`conn` = mysql([username](#mw%5Ff0fca8da-51fe-4f03-9986-5cde8bbed95e%5Fsep%5Fbtcmi%5F9-username),[password](#mw%5Ff0fca8da-51fe-4f03-9986-5cde8bbed95e%5Fsep%5Fbtcmi%5F9-password),[Name,Value](#namevaluepairarguments)) creates a MySQL native interface database connection using the specified user name and password, with additional options specified by one or more name-value pair arguments. For example, "Server","dbtb00" specifies the database server name asdbtb00.

example

Examples

collapse all

Connect to MySQL Database Using MySQL Native Interface

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:

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.

Close the database connection conn.

Connect to MySQL Database Using MySQL Native Interface and Additional Options

Create a MySQL® native interface connection to a MySQL database using name-value pair arguments. 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 version 5.7.22 using the MySQL Connector/C++ driver version 8.0.15.

Connect to the database using the user name and password shown. Specify the database server name dbtb01, database name toystore_doc, and port number 3306 by setting the corresponding name-value pair arguments.

username = "root"; password = "matlab";

conn = mysql(username,password,'Server',"dbtb01", ... 'DatabaseName',"toystore_doc",'PortNumber',3306)

conn = connection with properties:

                Database: "toystore_doc"
                UserName: "root"

Database Properties:

              AutoCommit: "on"
            LoginTimeout: 0
  MaxDatabaseConnections: 151

Catalog and Schema Information:

          DefaultCatalog: "toystore_doc"
                Catalogs: ["information_schema", "mysql", "performance_schema" ... and 3 more]
                 Schemas: []

Database and Driver Information:

     DatabaseProductName: "MySQL"
  DatabaseProductVersion: "5.7.22"
              DriverName: "MySQL Connector/C++"
           DriverVersion: "8.0.15"

The property sections of the connection object are:

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)

ans=3×4 table productNumber Quantity Price inventoryDate
_____________ ________ _____ _____________________

      1            1700      14.5     "2014-09-23 09:38:34"
      2            1200         9     "2014-07-08 22:50:45"
      3             356        17     "2014-05-14 07:14:28"

Determine the highest product quantity from the table.

Close the database connection conn.

Input Arguments

collapse all

datasource — Data source name

character vector | string scalar

Data source name, specified as a character vector or string scalar. Specify the name of an existing data source.

Example: "myDataSource"

Data Types: char | string

username — User name

character vector | string scalar

User name required to access the database, specified as a character vector or string scalar. If no user name is required, specify an empty value "".

Data Types: char | string

password — Password

character vector | string scalar

Password required to access the database, specified as a character vector or string scalar. If no password is required, specify an empty value "".

Data Types: char | string

Name-Value Arguments

Specify optional pairs of arguments asName1=Value1,...,NameN=ValueN, where Name is the argument name and Value is the corresponding value. Name-value arguments must appear after other arguments, but the order of the pairs does not matter.

Before R2021a, use commas to separate each name and value, and enclose Name in quotes.

Example: conn = mysql(username,password,"Server","dbtb01","PortNumber",3306,"DatabaseName","toystore_doc") creates a MySQL native interface database connection using the database serverdbtb01, port number 3306, and database nametoystore_doc.

Server — Database server name

"localhost" (default) | string scalar | character vector

Database server name or address, specified as the comma-separated pair consisting of 'Server' and a string scalar or character vector.

Example: "dbtb00"

Data Types: char | string

PortNumber — Port number

3306 (default) | numeric scalar

Port number, specified as the comma-separated pair consisting of'PortNumber' and a numeric scalar.

Example: 3306

Data Types: double

DatabaseName — Database name

"" (default) | string scalar | character vector

Database name, specified as the comma-separated pair consisting of'DatabaseName' and a string scalar or character vector. If you do not specify a database name, the mysql function connects to the default database on the database server.

Example: "toystore_doc"

Data Types: char | string

Version History

Introduced in R2020b