Main Content

connection

Relational database ODBC connection

Description

Create a database connection using an ODBC driver. For details about ODBC drivers and the alternative JDBC drivers, see Choose Between ODBC and JDBC Drivers.

You can use the connection object to connect to various databases using different drivers that you install and administer. For details, see Connect to Database.

Creation

Create a connection object using the odbc or database function.

Properties

expand all

Connection Properties

This property is read-only.

Data source name for ODBC connection, specified as a character vector. DataSource is the name you provide for your data source when you create a data source using the Microsoft® ODBC Administrator.

The data source name is an empty character vector when the connection is invalid.

Example: 'MS SQL Server'

Data Types: char

This property is read-only.

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

Example: 'username'

Data Types: char

This property is read-only.

Database connection status message, specified as a character vector. The status message is empty when the database connection is successful. Otherwise, this property contains an error message.

Example: 'ODBC Driver Error: [Micro ...'

Data Types: char

This property is read-only.

Database connection type, specified as the value 'ODBC Connection Object' that means a database connection created using an ODBC driver.

Data Types: char

Database Properties

Flag to autocommit transactions, specified as 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'

Read-only database data, specified as one of these values:

  • 'on' — Database data is read-only.

  • 'off' — Database data is writable.

Data Types: char

This property is read-only.

Login timeout, specified as 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.

When no login timeout for the connection attempt is specified, the value is 0.

When login timeout is not supported by the database, the value is -1.

Data Types: double

This property is read-only.

Maximum database connections, specified as a positive, numeric scalar.

The value is 0 when there is no upper limit to the maximum number of database connections.

When the maximum number of database connections is not supported by the database, the value is -1.

Data Types: double

Catalog and Schema Information

This property is read-only.

Default catalog name, specified as a character vector.

When a database does not specify a default catalog, the value is an empty character vector ''.

Example: 'catalog'

Data Types: char

This property is read-only.

Catalog names, specified as a cell array of character vectors.

When a database does not contain catalogs, the value is an empty cell array {}.

Example: {'catalog1', 'catalog2'}

Data Types: cell

This property is read-only.

Schema names, specified as a cell array of character vectors.

When a database does not contain schemas, the value is an empty cell array {}.

Example: {'schema1', 'schema2', 'schema3'}

Data Types: cell

Database and Driver Information

This property is read-only.

Database product name, specified as a character vector.

When the database connection is invalid, the value is an empty character vector ''.

Example: 'Microsoft SQL Server'

Data Types: char

This property is read-only.

Database product version, specified as a character vector.

When the database connection is invalid, the value is an empty character vector ''.

Example: '11.00.2100'

Data Types: char

This property is read-only.

Driver name of an ODBC driver, specified as a character vector.

When the database connection is invalid, the value is an empty character vector ''.

Example: 'sqlncli11.dll'

Data Types: char

This property is read-only.

Driver version of an ODBC driver, specified as a character vector.

When the database connection is invalid, the value is an empty character vector ''.

Example: '11.00.5058'

Data Types: char

Object Functions

closeClose and invalidate database and driver resource utilizer
commitMake database changes permanent
executeExecute SQL statement using relational database connection
fetchImport data into MATLAB workspace from execution of SQL statement
isopenDetermine if database connection is open
rollbackUndo database changes
executeSQLScriptExecute SQL script on database
selectExecute SQL SELECT statement and import data into MATLAB
sqlfindFind information about all table types in database
sqlinnerjoinInner join between two database tables
sqlouterjoinOuter join between two database tables
sqlreadImport data into MATLAB from database table
sqlwriteInsert MATLAB data into database table
sqlupdateUpdate rows in database table
updateReplace data in database table with MATLAB data

Examples

collapse all

First, create an ODBC connection to the MySQL® database. Then, import data from the database into MATLAB® and perform simple data analysis. Close the database connection. The code assumes that you are connecting to a MySQL database version 5.5.46 using the MySQL ODBC 5.3 ANSI Driver.

Connect to the database using the data source name, user name, and password.

datasource = 'dsname';
username = 'username';
password = 'pwd';

conn = database(datasource,username,password)
conn = 

  connection with properties:

                  DataSource: 'MySQLdb'
                    UserName: 'username'
                     Message: ''
                        Type: 'ODBC Connection Object'
  Database Properties:

                  AutoCommit: 'on'
                    ReadOnly: 'off'
                LoginTimeout: 0
      MaxDatabaseConnections: 0

  Catalog and Schema Information:

              DefaultCatalog: 'catalog'
                    Catalogs: {'catalog1', 'catalog2'}
                     Schemas: {}

  Database and Driver Information:

         DatabaseProductName: 'MySQL'
      DatabaseProductVersion: '5.5.46-0+deb7u1'
                  DriverName: 'myodbc5a.dll'
               DriverVersion: '05.03.0004'

conn has an empty Message property, which indicates a successful connection.

The property sections of the connection 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 product quantity from the table.

max(data.Quantity)
ans =

        9000

Close the database connection conn.

close(conn)

Alternative Functionality

You can connect to an SQLite database file by creating the sqlite object. This connection uses the MATLAB interface to SQLite that does not require installing or administering a database or driver. For details, see Interact with Data in SQLite Database Using MATLAB Interface to SQLite.

Version History

Introduced before R2006a