Main Content

SQLConnectionOptions

Define ODBC database connection options

Since R2024a

    Description

    Create connection options for an ODBC database connection.

    After you create an SQLConnectionOptions object, set the connection options, test the connection, and save the data source, you can create an ODBC database connection by using the saved data source. The connection options include the options required to make a database connection. You can also define additional connection options for a specific database driver.

    Creation

    Create an SQLConnectionOptions object using the databaseConnectionOptions function.

    Properties

    expand all

    All Databases

    Data source name, specified as a character vector or string scalar. You can use the data source name in the database function to create an ODBC database connection.

    Example: "MSSQLServer"

    Data Types: char | string

    Database vendor, specified as a character vector or string scalar. Set this property using the vendor input argument in the databaseConnectionOptions function.

    Example: "Microsoft SQL Server"

    Data Types: char | string

    ODBC driver location, specified as a character vector or string scalar. Specify ODBCDriver as one of the following:

    • On the Windows® platform, use the name of the driver.

    • On the macOS platform, use the absolute path of the driver.

    Example: "MariaDB ODBC 3.1 Driver" or "/Applications/MATLAB_R2024a.app/bin/maci64/libmaodbc.dylib"

    Data Types: char | string

    Common Properties for Microsoft SQL Server, MySQL, Oracle, and PostgreSQL Databases

    Database name on the server, specified as a character vector or string scalar.

    Example: "mydatabase"

    Data Types: char | string

    Database server name or address, specified as a character vector or string scalar.

    Example: "dbtb09"

    Data Types: char | string

    Server port number where the server is listening, specified as a numeric scalar. The default value is based on the database vendor:

    • Microsoft® SQL Server® — 1433

    • MySQL® — 3306

    • Oracle® — 1521

    • PostgreSQL — 5432

    Data Types: double

    Microsoft SQL Server Only

    Authentication type, specified as one of these values:

    • "Server"Microsoft SQL Server authentication

    • "Windows" — Windows authentication

    Specify the value as a character vector or string scalar.

    Oracle Database Only

    Driver type, specified as one of these values:

    • "thin" — Thin driver

    • "oci" — Windows authentication or OCI driver

    Specify the value as a character vector or string scalar.

    macOS Databases Only

    Driver manager for macOS platform, specified as "unixODBC" or "iODBC".

    Object Functions

    setoptionsSet JDBC or ODBC connection options
    rmoptionsRemove JDBC or ODBC connection options
    resetReset JDBC or ODBC connection options to defaults
    testConnectionTest JDBC or ODBC database connection
    saveAsDataSourceSave JDBC or ODBC data source

    Examples

    collapse all

    Create a data source that connects to a MySQL server on the Windows or macOS platform.

    On the Windows platform, use the databaseConnectionOptions function to create a data source that connects to a MySQL server.

    opts = databaseConnectionOptions('odbc','mysql')
    opts = 
     
    SQLConnectionOptions with properties:
     
                  DataSourceName: ""
                          Vendor: "MySQL"
     
                    DatabaseName: ""
                          Server: "localhost"
                      PortNumber: 3306
                      ODBCDriver: "MariaDB ODBC 3.1 Driver"

    Configure the data source by setting the ODBC connection options.

    opts = opts.setoptions('DataSourceName','mysql_odbc','DatabaseName',"toy_store","Server","dbtb09");
    opts.saveAsDataSource();

    Alternatively, use the databaseConnectionOptions function on the macOS platform to create a data source that connects to a MySQL server.

    opts = databaseConnectionOptions("odbc","MySQL") 
    opts =
    
    SQLConnectionOptions with properties:
     
                  DataSourceName: "mysql-server-test"
                          Vendor: "MySQL"
     
                    DatabaseName: "toy_store"
                          Server: "dbtb09"
                      PortNumber: 3306
                      ODBCDriver: "/Applications/MATLAB_R2024a.app/bin/maci64/libmaodbc.dylib"
                   DriverManager: "unixODBC"  

    Configure the data source by setting the ODBC connection options.

    opts = setoptions(opts,"DataSourceName","mysql-server-test", ...
        "DatabaseName","toy_store","Server","dbtb01")

    Version History

    Introduced in R2024a