Connect to database
creates a database connection to a data source with a user name and password. The
database connection is a conn
= database(datasource
,username
,password
)connection
object. The data source
specifies whether the database connection uses an ODBC or JDBC driver.
creates a JDBC database connection to a database name with a user name, password,
and JDBC driver parameters as specified by multiple name-value pair
arguments.conn
= database(databasename
,username
,password
,Param1,ParamValue1,...,ParamN,ParamValueN
)
specifies options using one or more name-value pair arguments in addition to any of
the input argument combinations in previous syntaxes. For example, conn
= database(___,Name,Value
)conn =
database(datasource,username,password,'LoginTimeout',5);
creates an
ODBC or JDBC connection, as specified by the datasource
input
argument, with a login timeout of 5 seconds.
Connect to a Microsoft® SQL Server® database. Then, import data from the database into MATLAB®. Perform simple data analysis, and then close the database connection.
To create a database connection using a JDBC driver, you must configure a JDBC data source. For more information, see the configureJDBCDataSource
function.
This example assumes that you are connecting to a Microsoft SQL Server Version 11.00.2100 database using the Microsoft SQL Server Driver 11.00.5058.
Create a database connection to the JDBC data source MSSQLServerJDBCAuth
. This data source configures a JDBC driver to a Microsoft SQL Server database with Windows® authentication. Specify a blank user name and password.
datasource = "MSSQLServerJDBCAuth"; username = ""; password = ""; conn = database(datasource,username,password)
conn = connection with properties: DataSource: 'MSSQLServerJDBCAuth' UserName: '' Driver: 'com.microsoft.sqlserver.j ...' URL: 'jdbc:sqlserver://dbtb04:5 ...' Message: '' Type: 'JDBC Connection Object' Database Properties: AutoCommit: 'on' ReadOnly: 'off' LoginTimeout: 0 MaxDatabaseConnections: 0 Catalog and Schema Information: DefaultCatalog: 'toy_store' Catalogs: {'master', 'model', 'msdb' ... and 2 more} Schemas: {'db_accessadmin', 'db_backupoperator', 'db_datareader' ... and 14 more} Database and Driver Information: DatabaseProductName: 'Microsoft SQL Server' DatabaseProductVersion: '11.00.2100' DriverName: 'Microsoft JDBC Driver 4.0 ...' DriverVersion: '4.0.2206.100'
conn
has an empty Message
property, which indicates a successful connection.
The property sections of the conn
object are:
Database Properties
-- Information about the database configuration
Catalog and Schema Information
-- Names of catalogs and schemas in the database
Database 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 eight rows of data.
tablename = "inventoryTable";
data = sqlread(conn,tablename);
head(data)
ans=8×4 table
productNumber Quantity Price inventoryDate
_____________ ________ _____ _______________________
1 1700 20 '2014-12-01 08:50:15.0'
2 1200 9 '2014-07-08 22:50:45.0'
3 356 17 '2014-05-14 07:14:28.0'
4 2580 21 '2013-06-08 14:24:33.0'
5 9000 3 '2012-09-14 15:00:25.0'
6 4540 8 '2013-12-25 19:45:00.0'
7 6034 16 '2014-08-06 08:38:00.0'
8 8350 5 '2011-06-18 11:45:35.0'
Determine the highest product quantity in the table.
max(data.Quantity)
ans = 9000
Close the database connection.
close(conn)
Connect to the Microsoft® SQL Server® database. Then, import data from the database into MATLAB®. Perform simple data analysis. Close the database connection.
This example assumes that you are connecting to a Microsoft® SQL Server® Version 11.00.2100 database using the Microsoft® SQL Server® Driver 11.00.5058.
Create a database connection to a Microsoft® SQL Server® database with Windows® authentication. Specify a blank user name and password.
datasource = 'MS SQL Server Auth'; conn = database(datasource,'','')
conn = connection with properties: DataSource: '' UserName: '' Message: '' Type: 'ODBC Connection Object' Database Properties: AutoCommit: 'on' ReadOnly: 'off' LoginTimeout: 15 MaxDatabaseConnections: 0 Catalog and Schema Information: DefaultCatalog: 'toy_store' Catalogs: {'master', 'msdb', 'tempdb' ... and 1 more} Schemas: {'dbo', 'guest', 'INFORMATION_SCHEMA' ... and 3 more} Database and Driver Information: DatabaseProductName: 'Microsoft SQL Server' DatabaseProductVersion: '11.00.2100' DriverName: 'sqlncli11.dll' DriverVersion: '11.00.6518'
conn
has an empty Message
property, which indicates a successful connection.
The property sections of the conn
object are:
Database Properties
-- Information about the database configuration
Catalog and Schema Information
-- Names of catalogs and schemas in the database
Database and Driver Information
-- Names and versions of the database and driver
Import all data from the table inventoryTable
into MATLAB® using the select
function. Display the first three rows of data.
selectquery = 'SELECT * FROM inventoryTable';
data = select(conn,selectquery);
data(1:3,:)
ans = 3×4 table productNumber Quantity Price inventoryDate _____________ ________ _____ _________________________ 1 1700 20 '2014-12-01 08:50:15.000' 2 1200 9 '2014-07-08 22:50:45.000' 3 356 17 '2014-05-14 07:14:28.000'
Determine the highest product quantity in the table.
max(data.Quantity)
ans = 9000
Close the database connection.
close(conn)
Connect to the PostgreSQL database. Then, import data from the database into MATLAB®, perform simple data analysis, and then close the database connection. This example assumes that you are connecting to a PostgreSQL 9.4.5 database using the JDBC PostgreSQL Native Driver 8.4.
Connect to the database using the database name, user
name, and password. Use the JDBC driver org.postgresql.Driver
to
make the connection.
Use the URL defined by the driver vendor including your server
name host
, port number, and database name.
databasename = 'dbname'; username = 'username'; password = 'pwd'; driver = 'org.postgresql.Driver'; url = 'jdbc:postgresql://host:port/dbname'; conn = database(databasename,username,password,driver,url)
conn = connection with properties: DataSource: 'dbname' UserName: 'username' Driver: 'org.postgresql.Driver' URL: 'jdbc:postgresql://host: ...' Message: '' Type: 'JDBC Connection Object' Database Properties: AutoCommit: 'on' ReadOnly: 'off' LoginTimeout: 0 MaxDatabaseConnections: 8192 Catalog and Schema Information: DefaultCatalog: 'catalog' Catalogs: {'catalog'} Schemas: {'schema1', 'schema2', 'schema3' ... and 1 more} Database and Driver Information: DatabaseProductName: 'PostgreSQL' DatabaseProductVersion: '9.4.5' DriverName: 'PostgreSQL Native Driver' DriverVersion: 'PostgreSQL 8.4 JDBC4 (bui ...'
conn
has an empty Message
property,
which indicates a successful connection.
The property sections of the conn
object
are:
Database Properties
— Information
about the database configuration
Catalog and Schema Information
—
Names of catalogs and schemas in the database
Database and Driver Information
—
Names and versions of the database and driver
Import all data from the table inventoryTable
into MATLAB using
the select
function. Display the data.
selectquery = 'SELECT * FROM inventoryTable';
data = select(conn,selectquery)
ans = productnumber quantity price inventorydate _____________ ________ _____ _______________________ 1 1700 14.5 '2014-09-23 09:38:34.0' 2 1200 9.3 '2014-07-08 22:50:45.0' 3 356 17.2 '2014-05-14 07:14:28.0' ...
Determine the highest quantity in the table.
max(data.quantity)
ans = 9000
Close the database connection.
close(conn)
Connect to the Microsoft® SQL Server® database. Then, import data from the database into MATLAB®, perform simple data analysis, and then close the database connection.
This example assumes that you are connecting to a Microsoft® SQL Server® Version 11.00.2100 database using the Microsoft® SQL Server® JDBC Driver 4.0.2206.100.
Create a database connection to a Microsoft® SQL Server® database with Windows® authentication and a login timeout of 5 seconds. Specify a blank user name and password.
databasename = 'toy_store'; conn = database(databasename,'','','Vendor','Microsoft SQL Server', ... 'Server','dbtb04','AuthType','Windows','PortNumber',54317, ... 'LoginTimeout',5)
conn = connection with properties: DataSource: 'toy_store' UserName: '' Driver: 'com.microsoft.sqlserver.j ...' URL: 'jdbc:sqlserver://dbtb04:5 ...' Message: '' Type: 'JDBC Connection Object' Database Properties: AutoCommit: 'on' ReadOnly: 'off' LoginTimeout: 5 MaxDatabaseConnections: 0 Catalog and Schema Information: DefaultCatalog: 'toy_store' Catalogs: {'master', 'model', 'msdb' ... and 2 more} Schemas: {'db_accessadmin', 'db_backupoperator', 'db_datareader' ... and 15 more} Database and Driver Information: DatabaseProductName: 'Microsoft SQL Server' DatabaseProductVersion: '11.00.2100' DriverName: 'Microsoft JDBC Driver 4.0 ...' DriverVersion: '4.0.2206.100'
conn
has an empty Message
property, which indicates a successful connection.
The property sections of the conn
object are:
Database Properties
-- Information about the database configuration
Catalog and Schema Information
-- Names of catalogs and schemas in the database
Database and Driver Information
-- Names and versions of the database and driver
Import all data from the table inventoryTable
into MATLAB® using the select
function. Display the first three rows of data.
selectquery = 'SELECT * FROM inventoryTable';
data = select(conn,selectquery);
data(1:3,:)
ans = 3×4 table productNumber Quantity Price inventoryDate _____________ ________ _____ _______________________ 1 1700 20 '2014-12-01 08:50:15.0' 2 1200 9 '2014-07-08 22:50:45.0' 3 356 17 '2014-05-14 07:14:28.0'
Determine the highest quantity in the table.
max(data.Quantity)
ans = 9000
Close the database connection.
close(conn)
datasource
— Data source nameData source name, specified as a character vector or string scalar. Specify the name of an existing data source.
Example: "myDataSource"
Data Types: char
| string
databasename
— JDBC database nameJDBC database name, specified as a character vector or string scalar. Specify the name of your database to create a database connection using a JDBC driver.
The name differs for different database systems. For example,
databasename
is the SID or the service name when you
are connecting to an Oracle® database. Or, databasename
is the catalog
name when you are connecting to a MySQL® database.
For details about your database name, contact your database administrator or refer to your database documentation.
Data Types: char
| string
username
— User nameUser 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
— PasswordPassword 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
Param1,ParamValue1,...,ParamN,ParamValueN
— JDBC driver parametersJDBC driver parameters, specified as multiple name-value pair arguments. A
Param
argument is a character vector or string scalar
that specifies the name of a JDBC driver parameter. A
ParamValue
argument is a character vector, string
scalar, or numeric scalar that specifies the value of the JDBC driver
parameter.
Param Valid Values | Param Value Description | ParamValue Valid Values |
---|---|---|
"Vendor" | Database vendor |
If you are connecting to a database system not
listed here, use the |
"Server" | Database server name or address |
|
"PortNumber" | Server port number where the server is listening | Numeric scalar |
"AuthType" | Authentication type (required only for Microsoft® SQL Server®) |
|
"DriverType" | Driver type (required only for Oracle) |
|
Tip:
When creating a JDBC connection using the JDBC driver parameters, you can omit the following:
'Server'
parameter when connecting to a
database locally
'PortNumber'
parameter when connecting
to a database server listening on the default port (except
for Oracle connections)
Example: 'Vendor','Microsoft SQL
Server','Server','dbtb04','AuthType','Windows','PortNumber',54317
connects to a Microsoft
SQL Server database using a JDBC driver on a machine named
dbtb04
with Windows authentication and using port number
54317
.
Example: 'Vendor','MySQL','Server','remotehost'
connects
to a MySQL database using a JDBC driver on a machine named
remotehost
.
driver
— JDBC driver nameJDBC driver name, specified as a character vector or string scalar that refers to the name of
the Java® driver that implements the java.sql.Driver
interface. For details, see JDBC driver name and
database connection URL.
Data Types: char
| string
url
— Database connection URLDatabase connection URL, specified as a character vector or string scalar for the vendor-specific URL. This URL is typically constructed using connection properties such as server name, port number, and database name. For details, see JDBC driver name and database connection URL. If you do not know the driver name or the URL, you can use name-value pair arguments to specify individual connection properties.
Data Types: char
| string
Specify optional
comma-separated pairs of Name,Value
arguments. Name
is
the argument name and Value
is the corresponding value.
Name
must appear inside quotes. You can specify several name and value
pair arguments in any order as
Name1,Value1,...,NameN,ValueN
.
'LoginTimeOut',5,'ErrorHandling','report'
specifies waiting for 5
seconds to connect to a database before throwing an error and displaying any error
messages at the command line.'AutoCommit'
— Auto-commit transactions'on'
(default) | 'off'
Auto-commit transactions, specified as the comma-separated pair
consisting of 'AutoCommit'
and one of these
values:
'on'
— Database transactions are
automatically committed to the database.
'off'
— Database transactions must
be committed to the database manually.
Example: 'AutoCommit','off'
'LoginTimeout'
— Login timeout0
(default) | positive numeric scalarLogin timeout, specified as the comma-separated pair consisting of
'LoginTimeout'
and a positive numeric scalar. The
login timeout specifies the number of seconds that the driver waits
while trying to connect to a database before throwing an error.
To specify no login timeout for the connection attempt, set the value
to 0
.
When login timeout is unsupported by the database, the value is
-1
.
Example: 'LoginTimeout',5
Data Types: double
'ReadOnly'
— Read-only database data'off'
(default) | 'on'
Read-only database data, specified as the comma-separated pair
consisting of 'ReadOnly'
and one of these
values:
'on'
— Database data is
read-only.
'off'
— Database data is
writable.
Example: 'ReadOnly','on'
'ErrorHandling'
— Error handling'store'
(default) | 'report'
Error handling, specified as the comma-separated pair consisting of
'ErrorHandling'
and one of these values:
'store'
— Store an error message in
the Message
property of the
connection
object.
'report'
— Display an error message
at the command line.
conn
— Database connectionconnection
objectDatabase connection, returned as a connection
object.
The JDBC driver name and database connection URL take different forms for different databases. For details, consult your database driver documentation.
Database | JDBC Driver Name and Database URL Example Syntax |
---|---|
IBM® Informix® | JDBC driver: Database URL: |
Microsoft SQL Server 2005 | JDBC driver: Database URL: |
MySQL | JDBC driver:
Database URL:
For MySQL Connector 8.0 and later: JDBC driver:
For previous versions of MySQL Connector: JDBC
driver:
Database URL:
To
insert or select characters with encodings that are not default,
append the value
The
trailing |
Oracle oci7 drivers | JDBC driver: Database URL: |
Oracle oci8 drivers | JDBC driver: Database URL: Database URL: |
Oracle 10 Connections with JDBC (Thin drivers) | JDBC driver: Database URL: |
Oracle Thin drivers | JDBC driver: Database URL: Database URL: |
PostgreSQL | JDBC driver: Database URL: |
PostgreSQL with SSL Connection | JDBC driver: Database URL: The
trailing |
Teradata® | JDBC driver:
Database URL:
|
If you specify a data source name in the datasource
input
argument that appears on both ODBC and JDBC data source lists, then the
database
function creates an ODBC database connection.
In this case, if you must create a JDBC database connection instead, append
_JDBC
to the name of the data source.
The database
function connects to a database
using the command line. To connect to a database and explore its data
in a visual way, use the Database
Explorer app.
database.ODBCConnection
syntax has been removedErrors starting in R2020b
The database.ODBCConnection
syntax has been removed. Use the
syntaxes of the database
function instead. Some
differences between the workflows require updates to your code.
In prior releases, you created a connection to a database by using the
database.ODBCConnection
syntax. For example:
conn = database.ODBCConnection(datasource,username,password);
Now use the database
syntax instead.
conn = database(datasource,username,password);
You have a modified version of this example. Do you want to open this example with your edits?