Main Content

datastore

(Not recommended) Create datastore to access collection of data in a database

datastore is not recommended. Use databaseDatastore instead.

Description

This datastore function creates a DatabaseDatastore object. You can use this object to read large volumes of data in a relational database.

A DatabaseDatastore is one of the available datastore types. You can create other types of datastores using the MATLAB® function datastore. After creating any datastore, you can analyze data by writing custom functions to run MapReduce using the mapreduce function. For details, see Getting Started with MapReduce.

example

dbds = datastore(conn,sqlquery) creates a DatabaseDatastore object dbds using the database connection conn. This datastore contains query results from the executed SQL query sqlquery.

Examples

collapse all

Create a database connection conn using the ODBC driver. This code assumes that you are connecting to a MySQL® database with the data source named MySQL, user name username, and password pwd. MySQL contains the table named productTable with 15 product records.

 conn = database('MySQL','username','pwd');

Create a DatabaseDatastore object dbds using the database connection conn and SQL query sqlquery. This SQL query retrieves all products from the product table productTable.

sqlquery = 'SELECT * FROM productTable';

dbds = datastore(conn,sqlquery)
dbds = 

  DatabaseDatastore with properties:

       Connection: [1×1 database.odbc.connection]
            Query: 'SELECT * FROM productTable'
    VariableNames: {1×5 cell}
         ReadSize: 10000

datastore executes the SQL query sqlquery and creates a cursor object with the resulting data. dbds contains these properties:

  • connection object

  • Executed SQL query

  • Column names of the executed SQL query

  • Number of rows to read from the SQL query results

Display the database connection property Connection.

dbds.Connection
ans = 

  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: 'toy_store'
                    Catalogs: {'information_schema', 'toy_store'}
                     Schemas: {}

  Database and Driver Information:

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

The Message property is blank when the database connection is successful.

Close the DatabaseDatastore and database connection.

close(dbds)

Input Arguments

collapse all

Database connection, specified as an ODBC connection object or JDBC connection object created using the database function.

SQL statement, specified as a character vector or string scalar.

For information about the SQL query language, see the SQL Tutorial.

Example: SELECT * FROM invoice selects all columns and rows from the invoice table.

Data Types: char | string

Output Arguments

collapse all

Datastore containing data in database, returned as a DatabaseDatastore object.

Version History

Introduced in R2014b